Thread: qsort problem.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    qsort problem.

    Hello,

    I have a problem with qsort. I let my program generate 50x40 arrays and after i used qsort. The result is not 50x40 anymore. It become a

    here is my code
    Code:
    #include <iostream>
    #include <ctime> 
    #include <cstdlib> 
    #include <iomanip>
    using namespace std;
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    
     
    int main()
    {
        const int row=50;
        const int column=40;
        int table[row][column];
        int rnum;
        int t[row];
     
        srand(time(0)); 
        rnum = (rand() % 100) + 1; 
    
        for(int r=0; r<row; r++)//row
        { 
            for(int c=0; c<column; c++)
                table [r][c] = (rand()%100) + 1;
        }
     
        for(int r=0; r<row; r++)//row
        { 
            for(int c=0; c<column; c++) //column
     
            {
                cout << setw(3) << table[r][c] << ' '; //display table
     
            }
            cout << endl;
        } 
          qsort(table, 50 * 40, sizeof(int), compare);
    
     
        cout << "your new sorted table is: \n";
    
        for(int n=0; n<row; n++)//row
        { 
            for(int m=0; m<column; m++) //column
     
            {
                cout << setw(3) << table[n][m] << ' '; //display table
     
            }
        }
    
    
    
        
     
        
     
            system("pause");
            return 0;
     
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by witt
    I have a problem with qsort.
    Is there any reason why you don't use std::sort?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    well I dont even know how to use it or even what it meant sorry

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    std::qsort(table, 5 * 4, sizeof(int), compare); like this?

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    well it still not working
    Last edited by witt; 11-15-2011 at 09:55 PM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Since this is C++, forget about qsort. In C++ we use std::sort.
    However, regardless of what you use, you have to understand and convey to us, what it means to be sorting a 2D array.

    And no sorting cannot change the dimensions of an array. Your assumption that this is happening is wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You can sort each row, and then sort the columns... or you can define some ordering (zig-zag) and sort it that way. What are you trying to do?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort problem (ish)
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 07-18-2005, 05:45 AM
  2. problem with qsort()
    By rajatkochhar in forum C Programming
    Replies: 1
    Last Post: 06-01-2005, 12:48 AM
  3. qsort problem
    By studentc in forum C Programming
    Replies: 3
    Last Post: 05-24-2004, 02:09 PM
  4. qsort problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-07-2002, 04:31 PM