[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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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<rows; r++){
    cout << r+1 << ". ";
    
    for(int c=0; c<cols; c++){
            
    int randomint;
    randomint = rand() % 100;
    tekmovalci[r][c]=randomint;
    cout << tekmovalci[r][c] << " ";
    rowsum += tekmovalci[r][c];
    
    }
    cout << rowsum << endl;
    rowsum = 0;
}  





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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int x = rows;
int mesta[x];
int sestevek = 0;    
for(int a=0; a<x; a++){
for(int d=0; d<cols; d++){
        mesta[a]= (sestevek += tekmovalci[a][d]);
        }
        //cout << mesta[a] << " ";
        sestevek = 0;
}

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]);
        cout << mesta[nStartIndex] << " " << endl;
        sestevek = 0;
}
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
1
2
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.

For your problem:
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.

EDIT: We are talking about 1 line of code being added to your sort function!
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

1
2
3
4
5
6
7
8
9
10
int index[5]={1, 4, 3, 2, 5};
int age[5]={20, 22, 54, 23, 85};

std::sort(index, index+5, std::greater<int>());
std::sort(age, age+5, std::greater<int>());

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().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>





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 ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
...

const int rows = 5;
const int cols = 3;
int rowsum = 0;  
int tekmovalci[rows][cols];
   
srand ( time(NULL) ); 
 

for (int r=0; r<rows; r++){
    cout << r+1 << ". ";
    
    for(int c=0; c<cols; c++){
            
    int randomint;
    randomint = rand() % 11;
    tekmovalci[r][c]=randomint;
    cout << tekmovalci[r][c] << " ";
    rowsum += tekmovalci[r][c];
    
    }
    cout << rowsum << endl;
    rowsum = 0;
    
    
    
}

...

//AND THE SORT

int x = rows;
int mesta[x];
int sestevek = 0;
    
for(int a=0; a<x; a++){
        for(int d=0; d<cols; d++){
                mesta[a]= (sestevek += tekmovalci[a][d]);
        }
        //cout << mesta[a] << " ";
        sestevek = 0;
}


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]);
        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:

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