Thread: Array counting Rows and Columns:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Array counting Rows and Columns:

    I am trying to now add the columns and rows and you can see in the print statement. Any advice?

    Code:
    #include <stdio.h>
    
    #define NUMROWS 5
    #define NUMCOLS 5
    
    int main(int argc, const char * argv[])
    {
        int i,j;
        
        
        int val [NUMROWS][NUMCOLS] = {8,3,9,0,10,
                                      3,5,17,1,1,
                                      2,8,6,23,1,
                                      15,7,3,2,9,
                                      6,14,2,6,0};
       
        for (i = 0; i < NUMROWS; i++)
        {
            printf("Enter Row %d:", i+1);
                                          
        for (j = 0; j < NUMCOLS; j++) 
                               
            printf("%2d ", val[i][j]);
            printf("\n");
        }
        
        printf("___________________________\n\n");
        printf("Row Totals:\n");
        printf("Column Totals:\n");
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I see you iterating through the rows and columns to print. I don't want to give away the whole answer, so hopefully this nudges you in the right direction.

    I would store the row totals and column totals in two separate arrays:
    Code:
    int row_totals[NUMROWS];
    // same for cols
    Then, you just iterate through each row, adding adding all the column values in that row to the right element in the row total array. So row_totals[0] will have the sum for row[0]. Do the equivalent for columns, and when you're done, iterate through each one and print it out.

    Remember to initialize your row_total and column_total arrays to all zero before you start summing, otherwise they will have garbage values and your totals will be wrong.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One easy way to do this is to add one extra row and one extra column, to the array, and use it for the totals.

    Code:
                  
    1   2   3  4  10 //10 is the extra column which has the tally for row 0
    2   1   3  4  10 //has tally for row 1
    3   4   1  2  10 //has tally for row 2
    4   3   2  1  10
    10 10  10 10  80  //has tally for all the columns 0-3
    and tally for ^^ ALL the rows, and all the columns
    You don't print up these extra tally columns or rows, you just use them for checking and convenience.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Please tell me there is a easier way to handle my function adding rows and columns. I played with it and could only come up with several for statements or doing like I did. The program works like I need it to. It just seems cumbersome.
    Code:
    #include <stdio.h>
    
    #define NUMROWS 5
    #define NUMCOLS 5
    
    void sumrows (int a[][NUMCOLS]);
    
    int main(int argc, const char * argv[])
    {
        int i, j;
    
        int val [NUMROWS][NUMCOLS] = {8,3,9,0,10,
                                      3,5,17,1,1,
                                      2,8,6,23,1,
                                      15,7,3,2,9,
                                      6,14,2,6,0};
        
        for (i = 0; i < NUMROWS; i++)
        {
            printf("Enter Row %d:  ", i+1);
            
            for (j = 0; j < NUMCOLS; j++)
                
                printf("%2d ", val[i][j]);
            printf("\n");
        }
        
        printf("_____________________________\n");
        
        sumrows(val);
        
        return 0;
    }
    
    void sumrows(int val[][NUMCOLS])
    {
        int row1,row2,row3,row4,row5;
        int coll1,coll2,coll3,coll4,coll5;
        
        row1 = val[0][0] + val[0][1]+val[0][2]+val[0][3]+val[0][4];
        row2 = val[1][0] + val[1][1]+val[1][2]+val[1][3]+val[1][4];
        row3 = val[2][0] + val[2][1]+val[2][2]+val[2][3]+val[2][4];
        row4 = val[3][0] + val[3][1]+val[3][2]+val[3][3]+val[3][4];
        row5 = val[4][0] + val[4][1]+val[4][2]+val[4][3]+val[4][4];
        
        coll1 = val[0][0] + val[1][0]+val[2][0]+val[3][0]+val[4][0];
        coll2 = val[0][1] + val[1][1]+val[2][1]+val[3][1]+val[4][1];
        coll3 = val[0][2] + val[1][2]+val[2][2]+val[3][2]+val[4][2];
        coll4 = val[0][3] + val[1][3]+val[2][3]+val[3][3]+val[4][3];
        coll5 = val[0][4] + val[1][4]+val[2][4]+val[3][4]+val[4][4];
        
        printf("Row Totals:    %2d %2d %2d %2d %2d\n",row1,row2,row3,row4,row5);
        printf("Columns Totals:%2d %2d %2d %2d %2d",coll1,coll2,coll3,coll4,coll5);
        
        
    
    
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is an easier way. You use loops.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You used 'i' and 'j' to loop through the array when you printed it. Now use 'i' and 'j' to loop through the array as you sum it up.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I tired the nested loop method before I asked last time but I was having issues, so I decided to manually punch in the numbers. Now that I am using nested loops the answer is 28 which is the sum of the last row. Therefore it is not storing the values even though I am using pointers. I know the columns are wrong, but I am still working on the rows at the moment and I'll solve that issue later. Any advice to point me in the right direction?

    Thank you very much for this help. You guys really are amazing.

    Code:
    #include <stdio.h>
    
    #define NUMROWS 5
    #define NUMCOLS 5
    
    void sumrows (int a[][NUMCOLS],int *sum_rows, int *sum_cols);
    
    int main(int argc, const char * argv[])
    {
        int i, j;
        int sum_rows, sum_cols;
        
        int val [NUMROWS][NUMCOLS] = {8,3,9,0,10,
                                      3,5,17,1,1,
                                      2,8,6,23,1,
                                      15,7,3,2,9,
                                      6,14,2,6,0};
        
        for (i = 0; i < NUMROWS; i++)
        {
            printf("Enter Row %d:  ", i+1);
            
            for (j = 0; j < NUMCOLS; j++)
                
                printf("%2d ", val[i][j]);
            printf("\n");
        }
        
        printf("_____________________________\n");
        
        sumrows(val, &sum_rows, &sum_cols);
        
        return 0;
    }
    
    void sumrows(int val[][NUMCOLS], int *sum_rows, int *sum_cols)
    {
        int i,j;
        
        for (i = 0; i < NUMROWS; i++) {
            
            *sum_rows = 0;
            
            for (j = 0; j < NUMCOLS; j++)
                
                *sum_rows += val[i][j];
            }
        for (i=0; i < NUMROWS; i++) {
            
            *sum_cols = 0;
            
            for (j=0; j < NUMCOLS; j++) 
                
                *sum_cols += val [i][j];
        }
        
       
        
        printf("Row Totals:      %d  \n", *sum_rows);
        printf("Columns Totals:%d   ", *sum_cols);
        
        
        
        
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have the right logic on lines 19 to 27. Use that kind of logic, in sumrows().

    After the inner for loop, you'll print out the sum for the row (individual row, if that's what you want).

    Use another pair of nested for loops, to print out the column totals, (easiest). In setting up the for loops for the column totals, it's easiest to switch positions of the inner and outer for loop. That is, in the row totals:
    Code:
    for(each row) {
       for(each value (or column), in the row )
           tally the row total
       print the row total
    }
    
    //Then for the column totals:
    for(each column value) {
       for(each row)
          tally the column
       print the column total
    }

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 and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  3. Addition of the rows and columns in an array.
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-14-2006, 09:00 PM
  4. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  5. calculation from rows and columns
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 07:44 PM