Thread: adding up array column total

  1. #1
    Registered User
    Join Date
    Jan 2010

    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

    #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 */
       int i=0;
       if((fp = fopen("sales10.txt","r"))==NULL)       /*open data file for reading */
          printf("error opening file\n");
             total =0;
             for(int j=0;j<maxcols;j++)
                total=total+num[i][j];         /*calculate row sums*/
             printf("%8d",total);             /*display row sums*/ 
    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
    Northern Virginia/Washington DC Metropolitan Area
    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, 09:24 AM