Thread: calculation from rows and columns

  1. #1
    Unregistered
    Guest

    Angry 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

    then add all columns

    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. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    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. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest
    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. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    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. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Addition of the rows and columns in an array.
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-14-2006, 09:00 PM
  3. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  4. printing rows and columns
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 08-03-2003, 05:42 PM
  5. sizeof rows (not columns)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 04:45 AM