LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2010, 03:03 PM   #1
dmckay
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 0
C programming, 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
 
Old 01-25-2010, 03:14 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
So, what is the expected behavior and what is the actual one ?
 
Old 01-25-2010, 03:27 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I'll assume this is a homework post and advise anyone else helping to provide concepts and guidance rather than replacement code.

Quote:
Originally Posted by dmckay View Post
//const int maxchar=25;
...
static char name[maxrows][maxcols];
...
//static char name[maxrows][maxchar];
Why did you get rid of maxchar and/or please describe more about the shape of the data on which you want to work.

My guess would be you have up to maxrows rows of data and each row has one name made of up to maxchar characters and the name is followed on the row by up to maxcols numbers.

If that is not the shape, what is?

There are two basic approaches to column totals in this situation (code size vs. data size tradeoff).

You need only one variable for row totals because you are done with each row before you start the next.

You could manage with only one variable for column totals if you arrange for it to be done with each column before starting the next. That requires a second two level nested loop to start after your existing two level nested loop is done (more extra code).

You could manage with just a one level loop of extra code to print the column totals if you accumulate those column totals in an array rather than in a scalar (more data, less code).

Take your choice. Try coding it.

Last edited by johnsfine; 01-25-2010 at 03:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Latex: adding column names to Table of contents Carunkumar Linux - Software 3 09-03-2009 02:04 PM
OpenOffice Spreadsheet - Total All "Numerical" Values in a Column dudeman41465 Linux - Software 1 05-01-2009 09:21 AM
Adding fields and doing grand total rainmk Linux - Newbie 7 03-06-2008 07:00 PM
Total Linux Programming Newbie binarybob0001 Programming 4 04-01-2005 03:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration