Thread: calculation from rows and columns

1. calculation from rows and columns

I am new to C, I have a problem that I can't figure it out.

I need the user to input three times 2 type of data: net and rate

I have to calculate tax=net x rate
amountdue=net + tax

I have to put all this data in a table form

net rate tax amount due

xxxx xxxx xxxx xxxxxx
xxxx xxxx xxxx xxxxxx
xxxx xxxx xxxx xxxxxx

I have to use scanf

how can I get the program to remember all inputs and put them in a table form with the right calculation

This is what i did so far but it is not working:

#include <stdio.h>
int main(void)
{
float net;
float rate;
float tax,amtdue;
int k;
char c1[130];
char c2[130];

tax=net*rate;
amtdue=net+tax;

for (k=0;k<3;k++)
{

printf("Input Net: ");

sscanf(c1,"%f", &net);

printf("Input Rate: ");
sscanf(c2,"%f", &rate);

printf("\nnet rate tax amtdue\n");

printf("%0.2f %0.3f ", net,rate);
printf("%0.2f \$%0.2f\n", tax,amtdue);
}
return 0;
}

thanks............
JJ

2. >sscanf()
You need scanf() to get input from the user.

>tax = net * rate;
>amtdue = net + tax;
Do the calculations when you've got the numbers from the user, not before!

3. thanks hammer,

but i want to print three rows worth of data in one shot not as the user input the data, then I won't have the result in a table format.

4. Originally posted by Unregistered
thanks hammer,

but i want to print three rows worth of data in one shot not as the user input the data, then I won't have the result in a table format.
Then you'll need to store the data as it's entered, ready for later use. Best use an array for this (there are other ways, but they'll be for a later lesson, I expect).

Here's an example program using two array of floats. I give it to you as an example of how to fill and print from arrays, I'll leave it to you to adapt your code to store data in a similar way.

Code:
```#include <stdio.h>

int main(void)
{
float f[3], h[3];
int i;

for (i = 0; i < 3; i++)
{
f[i] = 99.0 / (i+1);
h[i] = 99.0 * (i+3);
}

printf ("Values:\n");
for (i = 0; i < 3; i++)
printf ("%f %f\n", f[i], h[i]);

return 0;
}

/*
Program output:

Values:
99.000000 297.000000
49.500000 396.000000
33.000000 495.000000

*/```

5. this is what i did so far, i did not bather with the calculation yet
I just want to print what the user typed three times

but it is not working i get:

net rate tax amtdue
1.000 1.000
1.000 1.000
1.000 1.000

1s is not what the user typed!!!

#include <stdio.h>
int main(void)
{
float net, rate;
float tax,amtdue;
float a[4],b[4],c[4];
float aa[4],bb[4],cc[4];
int k;

for (k=0;k<3;k++)
{
printf("\nInput Net: ");
a[k]=scanf("%f", &net);

printf("Input Rate: ");
aa[k]=scanf("%f", &rate);
}
tax=net*rate;
amtdue=net+tax;

printf("\nnet rate tax amtdue\n");

for (k=0;k<3;k++)
printf("%0.2f %9.3f\n", a[k],aa[k]);

return 0;
}

6. scanf returns the number of items converted, or EOF. Either way you don't get what you want stored in the array. Since scanf was converting one item and succeeded, it stored 1 in a[k] and aa[k] which was then converted to a float. This explains the 1.000 output. Try this instead:
Code:
```for (k=0;k<3;k++)
{
printf("\nInput Net: ");
scanf("%f", &net);
a[k] = net;

printf("Input Rate: ");
scanf("%f", &rate);
aa[k] = rate;
}```
-Prelude

7. Awesome

It works!!!!! I've been working on this problem for 2 days now!!

thanks Prelude

now I have to figure out how to add each column

I don't mind suggestions!!!!!

8. >I don't mind suggestions!!!!!
Get all of the input and calculate the results as you go, placing the values in a 2D array. When you're done taking input, print out the contents of the array. For the total, keep an extra variable to add it all to and print that last. Here's a working example you can use as a template:
Code:
```#include <stdio.h>

int main ( void )
{
int i;
double net,
rate,
allTotal = 0.0;
double table[3][4];

for ( i = 0; i < 3; i++ ) {
printf ( "Enter net and rate: " );
if ( scanf ( "%lf %lf", &net, &rate ) == 2 ) {
table[i][0] = net;
table[i][1] = rate;
table[i][2] = net * rate;
table[i][3] = net + ( net * rate );
allTotal += table[i][3];
}
}
for ( i = 0; i < 3; i++ ) {
printf ( "%.2f\t%.2f\t%.2f\t%.2f\n", table[i][0],
table[i][1], table[i][2], table[i][3] );
}
printf ( "All Total: %.2f\n", allTotal );
return 0;
}```
I must stress that error handling is very important, I didn't include any so I'm setting a bad example, but adding it would detract from the concept of the processing. Checking return values from user input is a must in any program though, always include it. Notice how I checked that scanf succeeded, the user is a moron and you have to code for that.

-Prelude