In: Computer Science
Define three integer variables (number1, number2, and number3). We also define two other storage spaces for storing sum as well as averageValue. The storage space (sum) is used to store the sum of three integer numbers. The storage space (averageValue) is used to store the average of the three numbers.
***************************************************************************************/
#include <stdio.h> // standard input and output functions library
#include <stdlib.h> // include library to execute command (system ("pause"); )
/*
The purpose of this program is to calculate the average of three
numbers
*/
int main ( void )
{ // Program start here
int number1, number2, number3, averageValue;
int sum ;
number1= 35; // assign value to number1
number2= 45; //assign value to the number2
number3 = 55; // assign value to the number3
sum = number1+ number2+ number3; // add all three numbers
averageValue = sum / 3 ;
fprintf(stdout, "\n\n\t\tValue of first number
is:\t%d", number1);
// %d is format specifier to represent number1
fprintf(stdout, "\n\n\t\tValue of second number
is:\t%d", number2);
// %d is format specifier to represent number2
fprintf(stdout, "\n\n\t\tValue of third number
is:\t%d", number3);
// %d is format specifier to represent number3
/*
Print sum and average values of these numbers
*/
fprintf(stdout, "\n\n\t\tSum of three Numbers
is:\t%d", sum);
// prints the sum of three numbers
fprintf (stdout, "\n\n\t\tAverage of three numbers
is:\t%d", averageValue);
// prints the average of three numbers
fprintf(stdout,"\n\n"); // bring the cursor on the new
line
system("pause"); // this statement holds the display until any key is pressed
} // end of program
Type the source code given above. The output of the
application is given below:
(Change the background of the display to white – this saves
black ink for printing)
The output of your program must look as shown
above.
Calculate sum and average manually (calculator) for these three
numbers (26, 35 and 45);
Sum : ________________
Average : ________________
What is the difference in actual value as well as the value calculated by C application?
If the two values are different, why are they different?
Make the relevant changes in the program so that the
values displayed are correct.
Show what changes are made in the program.
Exercise: 2:
Objective:
Purpose of this assignment is to learn use of reading and writing
data into specific format.
Use of fprintf or printf statements
a. Use of escape sequence in C Programming
b. Use of different variables
c. Use of format specifier
/****************************************************************************************
Name : Enter your Name here and Student # : Student number
Date:
*****************************************************************************************/
#include <stdio.h> // Directive to include the standard input/output library
int main (void)
{ // Program begins here
//Define data type variable here
int books_quantity;
float book_price;
float total_amount;
char name_initial;
// Print my name and student number
fprintf(stdout, "\n\t\t\t\tName:\t\t\\tYour Name\n\t\t\t\tStudent Number:\t\\tYour student Number");
// Now print message to enter quantity of books
printf ("\n Enter the quantity of books:\t\t");
// read the quantity of books
scanf("%d", &books_quantity);
// print the message to enter price of each book
fprintf(stdout, "\n\n Enter the price per book:\t\t$");
// read the price per book
scanf ("%f", &book_price);
// print the message to enter the initial of my first name
fprintf (stdout, "\n\n Enter the initial of your first name:\t\t");
// Before reading a character ensure that keyboard
buffer does not have any
// redundant characters
fflush (stdin);
// Now read character representing your first name's initial
scanf("%c",&name_initial); // Calculate the total amount of books
total_amount = book_price * books_quantity; // Now Display all values
printf ("\n\n Quantity of Books:\t\t%d",
books_quantity);
printf ("\n\n Price per book:\t\t$%.2f", book_price);
fprintf (stdout, "\n\n Total Amount of all the Books: $%.2f",
total_amount);
fprintf(stdout, "\n\n Initial of First Name:\t\t %c",
name_initial);
// flush any redundant character in the keyboard buffer
fflush(stdin); // hold the display
getchar(); // All ends well so return 0
return 0;
} // main block ends here
Attach your output here after building the application
and execute the program – attach the output of your application
below:
Now comment out the first fflush(stdin); (or remove
the fflush(stdin) )statement in the application. Rebuild the code
and execute the application. Input the data and attach the output
of your application below:
What do you observe? What happened when fflush(stdin);
was commented/removed?
Remove comment (//) (or add the first fflush(stdin);
statement) from the first fflush(stdin); statement. Comment out the
second fflush(stdin); (or remove statement (the statement just
before the getchar() statement).
Rebuild your application, execute the application and enter the
relevant inputs. What is your observation and explain? Attach your
output now.
Exercise 1:
Average of 3 numbers(26,35 and 45) is 35.33
but as calculated by C program it is: 35 only
because variable "averageValue" is defined as "int" type so it will
consider digits before point only. It will not take into account
values after decimal point.
The int serves a different purpose. Numbers without fractional
parts or any need for a decimal point can be used as int. Thus, the
int type holds only whole numbers.
Solution for both the values to be same:
double averageValue = (double)sum / 3 ;
Define averageValue as double and typecast sum as "double" data
type.
Exercise 2:
The function fflush(stdin) is used to flush/clear the output buffer of the stream. It returns zero, if successful otherwise, returns EOF and feof error indicator is set.
Question: What do you observe? What happened when fflush(stdin);
was commented/removed?
Ans: The code above takes only single input for variable "char
name_initial" and gives the same result for the second input for
"char name_initial" at line number 44(scanf("%c",
&name_initial); // Calculate the total amount of books). Reason
is because as the char is already stored in the buffer i.e. stream
is not cleared yet as it was expecting blank char. So, to handle
this situation fflush(stdin) is used.
Question: Remove comment (//) (or add the first fflush(stdin); statement) from the first fflush(stdin); statement. Comment out the second fflush(stdin); (or remove statement (the statement just before the getchar() statement).
Ans:
fflush(stdin) before getchar() holds the display and helps program
not terminating immediately.