adding up array column total

This is a discussion on adding up array column total within the C Programming forums, part of the General Programming Boards category; Hi There, I'm having problems adding up column totals using arrays. I've got it to add up the row totals ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    adding up array column total

    Hi There,

    I'm having problems adding up column totals using arrays. I've got it to add up the row totals and display at the end of the row.

    Here is my code so far

    Code:
    #include <stdio.h>
    #include <string.h>
    
    const int maxrows=10;
    const int maxcols=12;
    //const int maxchar=25;
    
    int num[maxrows][maxcols];
    int total,arraytotal;
    static char name[maxrows][maxcols];
    int dummyage;
    //static char name[maxrows][maxchar];
    static char dummy[maxcols];
    
    FILE *fp;   /*identify the file by pointer reference */
    
    main()
    {
       int i=0;
       if((fp = fopen("sales10.txt","r"))==NULL)       /*open data file for reading */
          printf("error opening file\n");
       else
       {
          do
          {
             printf("\n\t\t");
             fscanf(fp,"%s",&dummy);
             strcpy(name[i],dummy);
             printf("\n%8s",name[i]);
             total =0;
                         
             for(int j=0;j<maxcols;j++)
             {
                fscanf(fp,"%d",&num[i][j]);
                printf("%5d",num[i][j]);
                total=total+num[i][j];         /*calculate row sums*/
             }
                
             printf("%8d",total);             /*display row sums*/ 
             i++; 
            
          }while((strcmp(dummy,"ZZZZZ")!=0)and(i<maxrows));
          
          printf("\n\n%5d",num[6][11]);
          fclose(fp);
          getchar();
       }
    }
    What i need it to do is, add up the columns and display it at the bottom of each column similar to how the row totals display

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    To track the column totals you're going to need an array that holds the total for each column as you go through the file processing rows. As you process each line in the file you add the column value you are at to the existing total in that column's position in this new array (this array should of course be initialized to 0). At the end of processing all the rows from the file your array should contain the totals for the individual columns and you can then display the values in this array.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to my 2D Array
    By qwertysingh in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 01:16 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Adding array contents
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 08:24 AM

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