0

Hi, i'd like to ask for some help about a problem i got... So i got this code right now:
Code:

    const int rows = 5;
    const int cols = 3;
    int tocke = 0;

    int tekmovalci[rows][cols]={{7, 6, 9},
                                {8, 7, 8},
                                {6, 9, 10},
                                {7, 7, 9},
                                {8, 8, 10}};

    for(int r=0; r<rows; r++){
            cout << r+1 << ". ";
            for(int c=0; c<cols; c++){
                    cout << tekmovalci[r][c] << " ";
                    tocke += tekmovalci[r][c];        
            }
            cout << tocke << endl;
            tocke = 0;       
    }

and the 'int tocke' represents the sum of all numbers in a row, and now i'd like to sort the output by the value of sum.

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

2
Contributors
7
Replies
8
Views
4 Years
Discussion Span
Last Post by Ancient Dragon
0

One way to solve it is to keep track of the original row numbers and the sum for each row, then when the rows are sorted the program will remember the original row numbers. Expand the array by two columns to do that

int tekmovalci[rows][cols+2]={{7, 6, 9,0,22},
                                {8, 7, 8,1,23},
                                {6, 9, 10,2,25},
                                {7, 7, 9,3,23},
                                {8, 8, 10,4,26}};

Next, sort the array by the last column (total). When you print it out use the next-to-last column as the row number instead of the loop counter.

Sorting would be a lot easier if you used a structure to contain all the values then have an array of structures.

Edited by Ancient Dragon

0

thanks for the reply but i have to get the sum by summing the 3 numbers in a row, not by putting it in the array itself...

0

If you want the array sorted by the sum value of each line then you will have to put the sum values into the array. Instead of hard-coding them like I did you can create a loop, calculate the sum and save the sum in the last column of each row. You could put the sums in another array, but that would make sorting even more difficult and complex.

Your program must do this in three stages
1. In a loop, sum each row and store the result in the array itself or in another array
2. Sort the array using any one of several sorting algorithms.
3. Print the array

You can't do all the above in the same loop, its not that simple.

Edited by Ancient Dragon

0

OK, i change my code a bit according to how it sohuld be anyway... so i got a 2d array which is filled by random numbers...

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;

} 

and i managed to sort only the sums for each row...heres my code for that:

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;
}

Now is there a way i can just add those numbers from first array to these already sorted sums? or how can i do it as im really lost atm...
Any code samples would be appreciated... or a step by step guide :)

Thank you in advance

0

When you swap rows in a sorted array you also have to swap all the colums in the same row of the original array so that the rows in both arrays remain in the same position. There is an easier way to do it, but get what you have working first so that you will understand the easier way.

Edited by Ancient Dragon

0

there is nothing wrong with my current code,..the sum is not a part of the first array... i just stored those different sums to an array... so i could sort them from amx to min.

...But i still need to sort the first array...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.