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

Pages: 12
You got the indexes backwards in the swap() call.
EDIT: How many elements are in a row? rows???

Note: Your code is swapping the columns, not the rows.
Last edited on
there are 3 elements in each row (5rows)
1 2 3
5 6 8
3 6 8
4 5 1
3 4 7

Ok, how backwards... i swap the [nStartINdex] and [e] ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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<rows; e++){
                swap(tekmovalci[nStartIndex][e], tekmovalci[nSmallestIndex][e]);
        }
        cout << tekmovalci[nStartIndex][cols] << mesta[nStartIndex] << endl;
        sestevek = 0;
}


can u post a vorking version, as im kinda confused atm :S
You should be able to finish this.
I shall repeat:
How many elements are in a row? rows???


Line 16 will give garbage values. tekmovalci[nStartIndex][cols] is out of range.
OK, thank you, i managed to solve it. :) Thanks a lot!
Last edited on
You're welcome.
I hope you see how to solve problems like this now.
That's why I wouldn't just cave in and give the solution.
Em, i found one more thing i was trying just now,... i cant find the spot to put in a 'cout' for the row counter, so there is in front of every row (1., 2., 3., 4., 5.)
like this:
3. 2 5 8 15
1. 5 3 4 12
4. 7 2 1 10
...etc...
i tried to put in an extra for loop only for the counter but in showes the 1. -5. between each numbers :S
Last edited on
You need to display what the row # was before sorting right?
Like in your 1st post?

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

Create another array for the row numbers and sort it alongside the others.
Last edited on
But that means i have to use this numbering array as the numberng system for the first output aswell, right?

because in the first part i just use to pint out and integer from the for loop and add ed +1 to start counting from 1 - 5 ...
That's right. The row numbers will sort just like rowsums and the row elements do. Use these for the 1st number on each line.

Initially int rowNums[5] = {1,2,3,4,5};
Last edited on
Topic archived. No new replies allowed.
Pages: 12