integer subtotals

mitziro
mitziro used Ask the Experts™
on
problem.
need to read numerical data from text file.
Maybe using
  while(fscanf(fp,"%d%d%d",&num1,&num2,&num3)!=EOF)
eg.  num1  num2      num3     num4
       2     3   *    4    =   12      
       2     6   *    4    =   24
       --
       4     6   *    3    =   18
       4     6   *    7    =   42
       --
       5     6   *    3    =   18
       5     5   *    2    =   10
       --      
as you can see num2 is multiplied by num3 to give
an answer of num4.
Using num1 to control the addition I need to sub total
the num4s ie.12+4=36, 18+42=60, 18+10=28.
and display each sub total between the different
num1s. Then display the grand total of all sub totals
at the end.
Can anyone help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Would something like:

int num1,num2,num3,num4;

while(fscanf(fp,"%d%d%d",&num1,&num2,&num3)!=EOF)
{   num4=num2*num3;
    printf("%d %d * %d = %d\n",num2,num2,num3,num4);
    fscanf(fp,"%d%d%d",&num1,&num2,&num3);
    num4+=num2*num3;
    printf("%d %d * %d = %d\n",num1,num2,num3,num2*num3);
    printf("%d\n",num4);
}


Do what you're after?

If not, please clarify.

Author

Commented:
sorry I may not have been very clear in  my question.
The num1s are just markers. When a change in marker
value is detected then that should trigger a sub total.
(num2*num3)
When num1s have been read in as say any value, when
a num1 value is detected greater than the one previously
read then because the value of num1 changes it should
trigger a control break.
Bear in mind that the amount and value of num1s could be
any.
Commented:
OK, how about:

int onum1,num1,num2,num3,num4,first;

onum1=0;
num4=0;
first=1;
while(fscanf(fp,"%d%d%d",&num1,&num2,&num3)!=EOF)
{   if(!first && onum1!=num1)
    {   printf("%d\n",num4);
        num4=0;
    }
    printf("%d %d * %d = %d\n",num2,num2,num3,num2*num3);
    num4+=num2*num3;
    onum1=num1;
    first=0;
}

Before you print the results for the line, check if num1 has changed (making an exception for the first encounter). If so, show the total and reinitialize num4.
After printing the line, accumulate in num4, save the "current" num1 and turn the first exception flag off.
obg

Commented:
int sum, num1, num2, num3, num4, tot, i;
i = sum = tot = 0;
while (fscanf(fp, "%d %d %d", &num1, &num2, &num3) != EOF) {
   if (num1 != i && sum != 0) {
     printf("Sum for %d = %d\n", i, sum);
     tot += sum;
     sum = 0;
   }
   i = num1;
   num4 = num2 * num3;
   printf("%d   %d * %d = %d\n", num1, num2, num3, num4);
   sum += num4;
}
tot += sum;
printf("Sum for %d = %d\nTotal = %d\n", i, sum, tot);

... Well, it turned out almost the same I guess.
Nothing has happened on this question in more than 13 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by imladris.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial