Bubble sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sheepman
    New Member
    • Jun 2007
    • 24

    Bubble sort

    I've been working on this since Wednesday and am at a total loss. I need to bubble sort a randomly generated list of numbers. I got the numbers in about 15 minutes. The remainder of the 3 days have been spent on trying to sort it. Generating the random numbers and sorting them has to be done with two seperate functions outside of main().

    Here is my function to generate my numbers:

    Code:
    void randFunc() {
    int firstArray [100];//Declares an array of 100 items
    	int min = -50;
    int max = 50;
    int randNum;
    for (randNum = 0; randNum < 100; randNum++) {
    firstArray[randNum] = rand()%(max - min + 1) + min;
    cout << setw (5) << firstArray[randNum] ;
    }//Close for
     
    }//close randfunc
    The above was working when called from main().

    My problems I'm sure are from a weak understanding of how to call functions and call arrays with functions. At this point I'm absoluting just guessing, with no rhyme or reason to changes in code. I'm simply throwing things against the wall to see if anything sticks.


    Now here is my poor attempt at trying to sort it or at least the current version. My errors are down to three, that's better than the 17 I started with.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> 
    #include <ctime>
    #include <iomanip>
     
    using namespace std;
    int randFunc();
    //int firstArray[100];
     
    int main () {
     
    //Generate random variable seed
    srand ((unsigned)time(0));//Seed random number generator
     
    // char yourFilename [80];//User supplied file name
     
    //Call random variable generator
    int randFunc(); 
    //Prompt user for name of file in which to store array
    cout << " Enter name the name of your file where you would like your array sorted array stored " << endl;
    // cin << yourFileName; 
    return (0);
    }//close main
     
    int randFunc(int firstArray[100])
    {
     
    //int firstArray [100];
    //Declares an array of 100 items
     
    int min = -50;
    int max = 50;
    int randNum;
    for (randNum = 0; randNum < 100; randNum++)
    {
     
    firstArray[randNum] = rand()%(max - min + 1) + min;
    cout << setw (5) << firstArray[randNum] ;
    }//Close for
     
    return firstArray[100];
    }//close randfunc
     
    void bubbleSort (int firstArray [100])
    {
    int swap;
    for(int pass = 0; pass < 100; pass++)
    {
    for (int randNum = 0; randNum < 99 - pass; randNum++)
    {
    if (firstArray[randNum] > firstArray[randNum + 1])
    {
    	swap = firstArray[randNum];
    	firstArray [randNum] = firstArray [randNum + 1];
    	firstArray[randNum + 1] = swap;
    	cout << firstArray[randNum];
    	cout << endl;
    }
    }
    }
    }
    Eventually I have to write this to the screen and a file, formatted in 10 rows of 10. Also done with another function. Thats why there is some extra stuff in main that I'm not using yet.

    Yes this is homework, I'm not this sadistic normally!
    Last edited by Sheepman; Jul 6 '07, 08:58 PM. Reason: typo
  • Sheepman
    New Member
    • Jun 2007
    • 24

    #2
    I actually got all the errors to clear but now the sorted numbers don't print to the screen. I'm not even sure they are being sorted. Just the prompt for the file name appear which should come after the sorting.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Sheepman
      I actually got all the errors to clear but now the sorted numbers don't print to the screen. I'm not even sure they are being sorted. Just the prompt for the file name appear which should come after the sorting.
      Here is an article explaining the bubble sort.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You have several errors.

        1) You did not declare an array to sort.
        2) You did not call the function to populate the array. What you believed to be the call was a function prototype.
        3) You re-invented the wheel by coding a sort instead of using the sort in the C++ Standard Library.

        Compare your code to the version below which works.

        [code=cpp]
        #include <iostream>
        #include <fstream>
        #include <cstdlib>
        #include <ctime>
        #include <iomanip>
        #include <algorithm>

        using namespace std;
        int randFunc(int*);
        int firstArray[100];

        int main () {

        //Generate random variable seed
        srand ((unsigned)time (0));//Seed random number generator

        // char yourFilename [80];//User supplied file name

        //Call random variable generator
        randFunc(firstA rray);
        //Prompt user for name of file in which to store array
        cout << " Enter name the name of your file where you would like your array sorted array stored " << endl;
        // cin << yourFileName;

        //sort the array
        sort(&firstArra y[0], &firstArray[100]);

        for (int i = 0; i <100; ++i)
        {
        cout << firstArray[i] << endl;
        }

        return (0);
        }//close main

        int randFunc(int firstArray[100])
        {

        //int firstArray [100];
        //Declares an array of 100 items

        int min = -50;
        int max = 50;
        int randNum;
        for (randNum = 0; randNum < 100; randNum++)
        {

        firstArray[randNum] = rand()%(max - min + 1) + min;
        cout << setw (5) << firstArray[randNum] ;
        }//Close for

        return firstArray[100];
        }//close randfunc
        [/code]

        Comment

        • Sheepman
          New Member
          • Jun 2007
          • 24

          #5
          Originally posted by weaknessforcats
          You have several errors.

          1) You did not declare an array to sort.
          2) You did not call the function to populate the array. What you believed to be the call was a function prototype.
          3) You re-invented the wheel by coding a sort instead of using the sort in the C++ Standard Library.

          Compare your code to the version below which works.

          [code=cpp]
          #include <iostream>
          #include <fstream>
          #include <cstdlib>
          #include <ctime>
          #include <iomanip>
          #include <algorithm>

          using namespace std;
          int randFunc(int*);
          int firstArray[100];

          int main () {

          //Generate random variable seed
          srand ((unsigned)time (0));//Seed random number generator

          // char yourFilename [80];//User supplied file name

          //Call random variable generator
          randFunc(firstA rray);
          //Prompt user for name of file in which to store array
          cout << " Enter name the name of your file where you would like your array sorted array stored " << endl;
          // cin << yourFileName;

          //sort the array
          sort(&firstArra y[0], &firstArray[100]);

          for (int i = 0; i <100; ++i)
          {
          cout << firstArray[i] << endl;
          }

          return (0);
          }//close main

          int randFunc(int firstArray[100])
          {

          //int firstArray [100];
          //Declares an array of 100 items

          int min = -50;
          int max = 50;
          int randNum;
          for (randNum = 0; randNum < 100; randNum++)
          {

          firstArray[randNum] = rand()%(max - min + 1) + min;
          cout << setw (5) << firstArray[randNum] ;
          }//Close for

          return firstArray[100];
          }//close randfunc
          [/code]
          Thank you very much for the response. I believe have have to reinvent the wheel for the sort as that was specifically stated in my assignment.

          I know my biggest problem is with calling for the information I need. That's the part I really don't understand. I've read ever tutorial on line I can find plus my textbook. The syntax kills me. As you mentioned in your first two comment. I think I'm calling something when I'm not.

          Would it be possible for you to show just the set up for the calling of the data. Not the statements that do the work. Since my second post yesterday I've worked my way back up to 5 errors. All revolving around , this isn't declared, that isn't declared, you can't change an int to a void. I totally confused on calling functions and arrays!

          One last question at some point should I be expecting a lightbulb type moment? Because right now I feel like I'm stumbling around blindfoldd in a cave.

          Again thanks for your help

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Sheepman
            One last question at some point should I be expecting a lightbulb type moment? Because right now I feel like I'm stumbling around blindfoldd in a cave.
            Yes. I had one. Took two years. Lost most of my hair. There is no learning of C++ without the necessary tears. These tears occur at 2:15am as you are slamming your keyboard into your monitor so mad with frustration you are ready to pitch the lot in the trash. This has happened to all of us. That's why we are so smug about it.

            Now to your code:
            1) you define variables before you use them.
            2) variables passed to functions are copied. The function uses the copy. There are exceptions to this, but this is where to start.
            3) for a function to change a variable in the calling function, you need to pass the address of the variable and not the variable itself. The address is passed using a pointer variable.

            [code=cpp]
            void Init(int* arr, int numelements)
            {
            //code here to intialize the 100 ints.
            //the int* is the address of array[0].
            //here arr[0] is array[0] in main(), arr[1] is array[1], etc.

            }
            void MySort(int* arr, int numelements)
            {
            //here you sort the array.
            //here arr[0] is array[0] in main(), arr[1] is array[1], etc.
            }
            int main()
            {
            int array[100]; //defines 100 ints named array[0], array[1], etc

            Init(array, 100); //calls the Init function. Look above main()

            MySort(array, 100); //sort the array using MySort function. Look above main()
            }
            [/code]

            The secret to using arrays as function arguments is to be clear in your mind that the name of the array is the address of element 0. So:
            [code=cpp]
            char stuff[10]; //stuff is address of stuff[0]. stuff[0] is a char so stuff is a char*
            //you use stuff as the address of the array
            //the function would need a char* argument.
            [/code]

            Comment

            Working...