Thread: Need an easier way to do Array addition.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Need an easier way to do Array addition.

    I know this program works but is there a way to shorten the code rather than run a sum for each row and column and get to accomplish the display I have accomplished?

    Code:
    #include <stdio.h>
    #define ROWS 5
    #define COLS 5
    
     int main()
    {
     int x,y;
     int sumrow0, sumrow1, sumrow2, sumrow3, sumrow4;
     int sumcol0, sumcol1, sumcol2, sumcol3, sumcol4;
     
     int val[ROWS][COLS]={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 (x=0; x<ROWS; x++)
     {
         printf("\n");
         for (y=0; y<COLS; y++)
         printf("%5d", val[x][y]);
         }
         
     sumrow0 = val[0][0]+val[0][1]+val[0][2]+val[0][3]+val[0][4];
     sumrow1 = val[1][0]+val[1][1]+val[1][2]+val[1][3]+val[1][4];
     sumrow2 = val[2][0]+val[2][1]+val[2][2]+val[2][3]+val[3][4];
     sumrow3 = val[3][0]+val[3][1]+val[3][2]+val[3][3]+val[3][4];
     sumrow4 = val[4][0]+val[4][1]+val[4][2]+val[4][3]+val[4][4];
     
     sumcol0 = val[0][0]+val[1][0]+val[2][0]+val[3][0]+val[4][0];
     sumcol1 = val[0][1]+val[1][1]+val[2][1]+val[3][1]+val[4][1];
     sumcol2 = val[0][2]+val[1][2]+val[2][2]+val[3][2]+val[4][2];
     sumcol3 = val[0][3]+val[1][3]+val[2][3]+val[3][3]+val[4][3];
     sumcol4 = val[0][4]+val[1][4]+val[2][4]+val[3][4]+val[4][4];
     
     printf("\nRow totals: %5d %2d %2d %2d %2d\n", sumrow0, sumrow1, sumrow2, sumrow3, sumrow4);
     printf("Column Totals: %2d %2d %2d %2d %2d", sumcol0, sumcol1, sumcol2, sumcol3, sumcol4); 
         return 0;
         }
    Last edited by 1BadRbt; 12-18-2006 at 12:29 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    int sumrow[5];

    Then use a for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    can you show me an example I already turned in the assignment cvause that sloppy crapola I put up fulfilled the assignment.

    Just curious I guess since it is 2:45am I may be a lot confused.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Code:
    int sumrow[5] = {0}, sumcol[5] = {0};
    
    for (x=0; x<ROWS; x++)
      for (y=0; y<COLS; y++) {
        sumrow[x] += val[x][y];
        sumcol[y] += val[x][y];
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM