HELP 2d array sort rows by its sum value

This is a discussion on HELP 2d array sort rows by its sum value within the C++ Programming forums, part of the General Programming Boards category; Hi, i'd like to ask for some help about a problem i got... So i got this code right now: ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    HELP 2d array sort rows by its sum value

    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
    Last edited by Fromar123; 12-02-2012 at 05:49 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    Something went wrong with your code formatting: you might want to post again with the corrected code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    ok, i fixed it kinda, thanks for the notice , but ye, looking for a solution now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting rows in a 2D array
    By Kyoukoku in forum C Programming
    Replies: 12
    Last Post: 09-22-2010, 10:26 AM
  2. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 11:04 AM
  3. adding up the rows of a 2D array
    By youngvito in forum C Programming
    Replies: 31
    Last Post: 06-11-2009, 01:06 PM
  4. Specify number of rows in an array??
    By niponki in forum C Programming
    Replies: 5
    Last Post: 07-31-2005, 05:41 AM
  5. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21