Link to home
Start Free TrialLog in
Avatar of mitziro
mitziro

asked on

integer subtotals

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?
Avatar of imladris
imladris
Flag of Canada image

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.

Avatar of mitziro
mitziro

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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