help, bubble sort function

This is a discussion on help, bubble sort function within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use a bubble sort in my function, but I keep getting a message saying "Invalid types 'int[int]' ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    17

    Question help, bubble sort function

    I'm trying to use a bubble sort in my function, but I keep getting a message saying "Invalid types 'int[int]' for array subscripts"
    So it won't compile. At least the switching of the second array won't compile.
    My program is to sort arrays with id numbers and SAT scores, maintaining the match up of the two arrays.
    Here is my function:

    Code:
    void bsort (int scores [], int idnumbers, int n)
    {     int temp, temp2;
          bool swapped;
          do {      
                    swapped = false;
                    for (int pos = 0; pos < n - 1; pos++) {
                        if (scores [pos] > scores [pos + 1]) {
                             
                             temp = scores [pos];
                             scores [pos] = scores [pos + 1];
                             scores [pos + 1] = temp;
                             
                             temp2 = idnumbers [pos];
                             idnumbers [pos] = idnumbers [pos + 1];
                             idnumbers [pos + 1] = temp2;
                             
                             swapped = true;
                        }
                    }
          }
          while (swapped);
          return;
    Can anyone tell me what I am doing wrong? Usually I can see the problem easily but not this time. Thanks, I appreciate it.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i havent looked at the logic, but notice how idnumbers is an 'int' but your using it as if it were an array.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Oh! I didn't see that, thanks it works now

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Actually it compiles but doesn't sort...

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Actually it compiles but doesn't sort...
    It seems to work for me. What are the contents of your scores and idnumbers arrays?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    I think there's something to do with the rest of the program:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    ifstream file_in;
    const int SIZE = 25;
    void readdata (int [], int [], int &);
    void printdata (int [], int [], int);
    void lsort (int [], int [], int);
    void bsort (int [], int [], int);
    int main () {
         int n, ans, ans2;
         int idnumbers [SIZE];
         int scores [SIZE];
         
         file_in.open ("program6in.dat");
         
          readdata (idnumbers, scores, n);
          cout << "The 2 arrays:" << endl;
          printdata (idnumbers, scores, n);
          lsort (idnumbers, scores, n);
          cout << "The linear sorted arrays:" << endl;
          printdata (idnumbers, scores, n);
          
          cout << "The Bubble sorted arrays: " << endl;
          printdata (idnumbers, scores, n);
          
          system ("pause");
          return 0;
    }
         
         
         
    
    void readdata (int idnumbers [], int scores [], int &n) 
    {
         file_in >> n;
         for (int count = 0; count < n; count++) {
              file_in >> idnumbers [count];
              file_in >> scores [count];
         }
         return;
    }
    
    void printdata (int idnumbers [], int scores [], int k) 
    {
         for (int count = 0; count < k; count++) {
              cout << idnumbers [count] << " ";
              cout << scores [count] << endl;
         }
         return;
    }
    
    void lsort (int idnumbers [], int scores [], int n) 
    {
         int temp, temp2;
         
         for (int pass = 0; pass < n - 1; pass++)
              for (int count = pass + 1; count < n; count++) {
                   if (idnumbers [count] > idnumbers [pass]) {
                        temp = idnumbers [count];
                        idnumbers [count] = idnumbers [pass];
                        idnumbers [pass] = temp;
                        
                        temp2 = scores [count];
                        scores [count] = scores [pass];
                        scores [pass] = temp2;
                   }
              } 
         return;
    } 
    
    void bsort (int scores [], int idnumbers [], int n)
    {     int temp, temp2;
          bool swapped;
          do {      
                    swapped = false;
                    for (int pos = 0; pos < n - 1; pos++) {
                        if (scores [pos] > scores [pos + 1]) {
                             
                             temp = scores [pos];
                             scores [pos] = scores [pos + 1];
                             scores [pos + 1] = temp;
                             
                             temp2 = idnumbers [pos];
                             idnumbers [pos] = idnumbers [pos + 1];
                             idnumbers [pos + 1] = temp2;
                             
                             swapped = true;
                        }
                    }
          }
          while (swapped);
          return;
    }
    And this is my output:

    Code:
    The 2 arrays:
    123 1450
    124 2400
    321 1200
    246 1100
    345 820
    The linear sorted arrays:
    345 820
    321 1200
    246 1100
    124 2400
    123 1450
    The Bubble sorted arrays:
    345 820
    321 1200
    246 1100
    124 2400
    123 1450
    Press any key to continue
    . . .

    They are identical, I don't know why
    The numbers on the left are supposed to be in descending order, the ones on the right, ascending, and always matched up with each other

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cout << "The Bubble sorted arrays: " << endl;
    > printdata (idnumbers, scores, n);
    Well the bubble sort works, you just never called bsort(). And in your linear sort function:
    Code:
    >               if (idnumbers [count] > idnumbers [pass]) {
    This is reversed and should be less than:
    Code:
                   if (idnumbers [count] < idnumbers [pass]) {
    Last edited by swoopy; 04-30-2007 at 09:34 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The clue being notice how the linear sort function sorted in descending order:
    The linear sorted arrays:
    345 820
    321 1200
    246 1100
    124 2400
    123 1450

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Thank you all, I didn't realize that I didn't call the function at all, can't believe I didn't see that
    I reversed the less than sign, so it works good now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21