-1

What do I need to add to get the output to display to the user? I am not sure what printf statement I need to add to make it work. I know how to display text to the user but not how to display the results of the calculations.

        #include <stdio.h>
        #define NORMAL_ROD 10
        #define SENTINEL 0


           int main(void)
{
    //Variable declarations
    int normal_count = 0;           
    int normal_total = 0;          
    int long_count = 0;
    int long_total = 0;
    int short_count = 0;
    int short_total = 0 ;
    double normal_average;          
    double long_average;
    double short_average;
    double overall_average;
    int overall_count =0;
    int overall_total= 0;



    int rodLength = 1;


    printf("Rod Manufacturing Quality Control\n");


    while(1)
    {
        printf("Input Rod Length: ");   
        scanf("%i", &rodLength);        
        if(rodLength == SENTINEL) break;

        overall_count = overall_count + 1;
        overall_total = overall_total + rodLength;

        if(rodLength == NORMAL_ROD)
        {
            normal_count = normal_count + 1;
            normal_total = normal_total + rodLength;
        }
        else if(rodLength > NORMAL_ROD)
        {
            long_count = long_count +1;
            long_total = long_total + rodLength;
        }
        else
        {
            short_count = short_count +1;
            short_total = short_total = rodLength;
        }
    }

    // Average calcs
    normal_average = (double)normal_total / normal_count;
    short_average = (double)short_total / short_count;
    long_average = (double)long_total / long_count;
    overall_average = (double)overall_total / overall_count;
    /*
    ...
    ...
    ...
    */

    // Print output
    printf("Statistics:\n");
    printf("Desc        Number        Total        Average\n");



    return 0;
}

/*

Output here!

*/
Bob Johnson
  • 11
  • 1
  • 1
  • 1

3 Answers3

2

To print the results you can use "format specifiers" like in scanf().

When you use

scanf("%d", &age);

Here %d is format specifier for integer. You can similarly use format specifiers for printf() as well.

For example:

If you have your age stored in an integer variable age then you can print it like this:

printf("Age is: %d",age);

Here printf() prints whatever is inside " "(first Argument) to the monitor. While doing so %d is replaced by the value of age.

So if age is equal to 25, then what will be displayed is as below:

Age is 25

%d is Format specifier for INTERGER variables. Each data type have their own format specifiers.

One more ex:

printf("Age is %d , weight is %d", age, weight);

So basically you need to use format specifier in the printf() statement to print values stored in variables. And if you have your results stored in variables, you can use those variable names with correct format specifiers.

You can google, "format specifiers in C" to find out format specifiers of all data types used in C.

sps
  • 2,720
  • 2
  • 19
  • 38
  • @chux I meant to use "format specifiers" in `printf()` to print value stored in variables. After reading the question I got the impression that the one who posted the question was not sure how to print the value stored in variables. However, your point sounds valid. I have removed the "same" word, as I too agree that may be misleading. – sps Feb 18 '15 at 20:40
0

You don't say what number corresponds to each description. Anyway to show a double use printf("%f", doubleValue);

// Print output
printf("Statistics:\n");
printf("   Average %f\n",normalAverage );

Analogously use %s for strings, %c for characters, or %d for integers

RafaelCaballero
  • 1,555
  • 2
  • 17
  • 24
0
printf("%f", normal_average);  /*double */
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
ares777
  • 3,590
  • 1
  • 22
  • 23