[HELP] 2d Array sort row accoridng to sum of their numbers...

Pages: 12
Well i have made a 2d Array and filled it with random numbers...
and while printing(cout) it out i added a sum of numbers(i each row) at the end.
Now i would like to sort the output(cout) according to the sum...

here is my current code:
 ``12345678910111213141516171819202122232425262728`` ``````int main() { const int rows = 5; const int cols = 3; int rowsum = 0; int tekmovalci[rows][cols]; srand ( time(NULL) ); for (int r=0; r

Here is a sample what i would like to make:

this is the current output:
1. 7 6 9 22
2. 8 7 8 23
3. 6 9 10 25
4. 7 7 9 23
5. 8 8 10 26

and i want it like this:
5. 8 8 10 26
3. 6 9 10 25
2. 8 7 8 23
4. 7 7 9 23
1. 7 6 9 22

SO, can anyone help me with this??
Anyone?? :(
closed account (D80DSL3A)
You will want to save the values of rowsum, so you can base some kind of sort routine on the values.

Right now you are calculating each sum then overwriting it with the next.
Can you write any kind of sort function that would work on a regular array?
I managed to do it for the sums only, but i have no idea how to do it for the rows of first array :S

 ``123456789101112131415161718192021222324`` ``````int x = rows; int mesta[x]; int sestevek = 0; for(int a=0; a
closed account (D80DSL3A)
You are storing the sums in an array (mesta?) and you have sorted these values?
Yes, but thats basically useless for what i wanna get i think, as i want to sort the rows of the first array according to the sums that i have sorted in the array "mesta"

...so any solution? :S
closed account (D80DSL3A)
Right, but that's hardly useless.

What if the problem was you have 2 arrays
 ``12`` ``````string names[N]; int ages[N];``````

The names correspond with the ages.
Sort the names array in order of the corresponding ages.

How would you solve that problem?
i would call the number from sumaaray lets say 234 and compare it to each rowsum of first array, and when it matches i would print it out...

but i really have no idea how to write that :S am relativelly new to c++ :S
any help with the code itself? like a sample on two random arrays, please?
closed account (D80DSL3A)
The key is to sort the 2 arrays in parallel.

For the names and ages problem:
As you are sorting the ages array, every time you swap elements in the ages array make the same swap in the names array.
This will keep the names and ages associated.

As you are sorting the rowsums[] array, every time you swap elements in the rowsums[] array make the same swap of rows in the 2d array.

Hint: The 1st half of it looks like line 5. The 2nd half looks like line 21.

Any chance you see it?
Last edited on
ok,i get that but how do i swap the wholes rows? :S
-can oyu please write me a working sample of how you sort two arrays like u mention? so i can see and figure it out... and make it work for me...?

closed account (D80DSL3A)
No. Think about my last post harder first. It's right in front of you.

Which line in the code you gave swaps elements in the mesta array?
use the swap() function to swap each of the elements in row nStartIndex
with those in row nSmallestIndex, just like you are doing with the elements of mesta.
Last edited on
Well i tried to do the age and name thing(i used index number instead of names), but it just sorts both arrays descending, i dont get to make it so it keeps age with the index :S

 ``12345678910`` ``````int index[5]={1, 4, 3, 2, 5}; int age[5]={20, 22, 54, 23, 85}; std::sort(index, index+5, std::greater()); std::sort(age, age+5, std::greater()); for(int i=0; i<5; i++){ cout << index[i] << " " << age[i] << endl; } ``````

well, i cant get it to work and am figuring it out for hours now :S
closed account (D80DSL3A)
Yes, you sorted them separately.
I'm talking about sorting them together, using your sort function, not std::sort().

 ``123456789101112131415`` ``````for(int nStartIndex=0; nStartIndex < N; nStartIndex++){ int nSmallestIndex = nStartIndex; for(int nCurrentIndex = nStartIndex + 1; nCurrentIndex < x; nCurrentIndex++){ if(ages[nCurrentIndex] < ages[nSmallestIndex]) nSmallestIndex = nCurrentIndex; } swap(ages[nStartIndex], ages[nSmallestIndex]);// ages elements swapped swap(index[nStartIndex], index[nSmallestIndex]);// index elements swapped cout << index[nStartIndex] << " " << endl; sestevek = 0; }``````

That should do it.

Last edited on
Thanks for the code, but in your version it printsout only the names array, i need both to be printed in alligned collumns..

i tried to do it, but then it destroy the sort :S

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include using namespace std; int main() { int index[5]={1, 4, 3, 2, 5}; int age[5]={20, 22, 54, 23, 85}; int sestevek = 0; for(int nStartIndex=0; nStartIndex < 5; nStartIndex++){ int nSmallestIndex = nStartIndex; for(int nCurrentIndex = nStartIndex + 1; nCurrentIndex < 5; nCurrentIndex++){ if(age[nCurrentIndex] < age[nSmallestIndex]) nSmallestIndex = nCurrentIndex; } swap(age[nStartIndex], age[nSmallestIndex]);// age elements swapped swap(index[nStartIndex], index[nSmallestIndex]);// index elements swapped cout << index[nStartIndex] << " " << age[nStartIndex] << endl; sestevek = 0; } system("PAUSE"); return 0; } ``````

I am probably printing it our wrong,... so what should i change to print both arrays sorted according to eachother? :S
closed account (D80DSL3A)
I don't understand.
I ran this code and got:
 ``` 1 20 4 22 2 23 3 54 5 85 ```

That output is perfect. The parallel sort worked (you setup a case which is only one swap away from sorted already)

EDIT: So, that worked. Now, switch back to the mesta problem and swap row elements instead of index alongside mesta.
Last edited on
OK, ye i managed to make it work for anme sand age, thanks..

now i tryed to put together the mesta (sum of number in each row), and each row itself.. but i get an error when swaping a 2d array ...

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````... const int rows = 5; const int cols = 3; int rowsum = 0; int tekmovalci[rows][cols]; srand ( time(NULL) ); for (int r=0; r mesta[nSmallestIndex]) nSmallestIndex = nCurrentIndex; } swap(mesta[nStartIndex], mesta[nSmallestIndex]); swap(tekmovalci[nStartIndex], tekmovalci[nSmallestIndex]); cout << tekmovalci[nStartIndex] << mesta[nStartIndex] << endl; sestevek = 0; } ``````

how can i fix so it will sort and print rows of that 2d array? :S
Last edited on
closed account (D80DSL3A)
You can't swap the entire rows at once like that. You need to swap element by element. Use a for loop. tekmovalci is a 2D array.

As for printing the array, why not do that later, after the sorting is complete?
Last edited on
i have to use a for loop round the swap line
'swap(tekmovalci[nStartIndex], tekmovalci[nSmallestIndex]);'

or do i have to somehow "transform" the 2d array tekmovalci so "1d" or w/e?
closed account (D80DSL3A)
Yes, a for loop around the swap line. No, you don't need to transform the array to 1D.

Think about the names, ages problem. names were string objects so they can be directly swapped, but what if we were stuck with char arrays and not allowed to use strcpy()?

You would have to swap the names character by character.
Last edited on
Ye, the program translates like this, but i get some random junk numbers when i put that swap in for loop... well here's what i got:

 ``123456789101112131415161718`` ``````for(int nStartIndex=0; nStartIndex < x; nStartIndex++){ int nSmallestIndex = nStartIndex; for(int nCurrentIndex = nStartIndex + 1; nCurrentIndex < x; nCurrentIndex++){ if(mesta[nCurrentIndex] > mesta[nSmallestIndex]) nSmallestIndex = nCurrentIndex; } swap(mesta[nStartIndex], mesta[nSmallestIndex]); //swap(tekmovalci[nStartIndex], tekmovalci[nSmallestIndex]); for(int e=0; e
Pages: 12