Thread: Simple C++ help...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Simple C++ help...

    Hey everyone.

    I need to write some code that takes a two deimensional array, makes it a single dimension array and then sorts in ascending order. I need some help with the flatten part and here is what I have so far.

    This code compiles and executes fine via Dev C++
    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    using namespace std;
    int main()
    {
    const int NROWS = 4;
    const int NCOLS = 5;
    int i, j, s;
    int first[NROWS][NCOLS] = {16,22,99,4,18,-258,4,101,5,98,105,6,15,2,45,33,88,72,16,3};
    int SORT [19]; //Array of numbers 
    int sort; //Temp array number
    // Insert the elements from the 2-dimmension in a single
    for (i = 0; i < NROWS; i++)
        {  //beginning of outer loop
             cout << endl;     //start each row on a new line
             for (j = 0; j < NCOLS; j++)
             {   //beginning of inner loop
                 SORT[0]=first[i][j];
                 cout << sort << endl;
                 }    //end of inner loop
                 }    //end of outer loop
                 cout << endl;
                 return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hmm, the two-dimensional array has 20 ints (4*5), but the onedimensional has room for 19? It should be int SORT[NROWS*NCOLS].

    Code:
    SORT[0]=first[i][j];
    Why are you putting everything to SORT[0], shouldn't this be something like
    Code:
    SORT[i*j+j] = first[i][j];

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by anon
    Why are you putting everything to SORT[0], shouldn't this be something like
    Code:
    SORT[i*j+j] = first[i][j];
    Or maybe:
    Code:
    SORT[i*NCOLS+j] = first[i][j];

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    Well, I made the changes you guys suggested but I still get crap on the output.

    I re-wrote it for a single already to see what I get and this code works perfectly..
    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    using namespace std;
    
    int bubbleSort( int[], int);
    
    int main()
    {
    
    
    //const int NROWS = 4;
    //const int NCOLS = 5;
    //int first[NROWS][NCOLS] = {16,22,99,4,18,-258,4,101,5,98,105,6,15,2,45,33,88,72,16,3};
    //int i, j;
    
    
    const int ARR1 = 20;
    int arr[ARR1]= {16,22,99,4,18,-258,4,101,5,98,105,6,15,2,45,33,88,72,16,3};
    int i, j;
    
    bubbleSort(arr, ARR1);
    
    for (i = 0; i < ARR1; i++)
        {
             cout << endl;
    
                 for( i = 0; i < ARR1; i++)
                      cout << " " << arr[i];
         }
                 return 0;
    }
    
    int bubbleSort(int num[], int numel)
    {
     int i, j, temp;
    
     for ( i = 0; i < (numel - 1); i++)
         {
           for ( j = 1; j < numel; j++)
           {
            if (num[j] < num[j-1])
            {
             temp = num[j];
             num[j] = num[j - 1];
             num[j-1] = temp;
             }
             }
             }
             }
    I have the 2-dimension part commented out so this code works, it is just with a single dimension.

    I played around with it a little but still can get the double to flatten into a single and do the sort from there.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://c-faq.com/aryptr/ary2dfunc2.html
    bubbleSort( &first[0][0], NROWS * NCOLS );

    The correct way would be to copy it to a proper 1-D array, sort it, then copy it back again.

    If you've got the actual sorting working, then post your array copying code again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM