# Simple C++ help...

Printable View

• 10-06-2006
wcdturbo
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; }```
• 10-06-2006
anon
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];`
• 10-06-2006
swoopy
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];`
• 10-06-2006
wcdturbo
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.
• 10-07-2006
Salem
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.