Question

In: Computer Science

Define three integer variables (number1, number2, and number3). We also define two other storage spaces for...

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.

Solutions

Expert Solution

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.



Related Solutions

1. We have the data as follows. There are three independent variables and three dependent variables...
1. We have the data as follows. There are three independent variables and three dependent variables (You may use the following table to solve this problem) x y 3 11 5 6 7 4 Total 15 21 a) Calculate b1 and b0, and write the equation of the least squares line. b) Determine the values of SSE and SST. c) Calculate the standard error. d) Find the rejection point for the t statistic at α = .05 and test H0:...
Two fair dies are rolled; one coloured blue and the other coloured black. We define the...
Two fair dies are rolled; one coloured blue and the other coloured black. We define the following random variables: X = The number shown on the blue die Y = 0 if the number on both dies are the same Y = 1 if the number on the blue die is greater than the number on the black die Y = 2 if the number on the black die is greater than the number on the blue die a. Write...
Develop a LP problem with at least 7 constraints (including two integer variables) to solve it...
Develop a LP problem with at least 7 constraints (including two integer variables) to solve it using any solver. we have to create a problem. any problem should be fine. please take any problem.
When you determine if there is an association between two variables, it is also important for...
When you determine if there is an association between two variables, it is also important for you to determine how strong or weak that association is. This is why, when you have data for two quantitative variables, you calculate what is called the coefficient for correlation. Instructions Suppose you are determining the association between the weight of a car and the miles per gallon that the car gets. Answer the following questions in a Word document: define correlation and explain...
You are given two integer arrays a and b of the same length. Let's define the...
You are given two integer arrays a and b of the same length. Let's define the difference between a and b as the sum of absolute differences of corresponding elements: difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length - 1]| You can replace one element of a with any other element of a. Your task is to return the minimum possible difference between a and b that can be achieved by...
Three fair coins are tossed simultaneously. We define the events A: get at least two heads...
Three fair coins are tossed simultaneously. We define the events A: get at least two heads B: get two tails C: get at most two heads i)         Write down the sample space S. ii)        Calculate P(A), P(B), P(C).    iii)       Calculate P(A ∩ B), P(A ∩ C), P(B ∩ C).                            iv)       Calculate P(A Ս B), P(A Ս C), P(B Ս C).                v)        Which of the following pairs of events are independent and why? A and B A and C vi)       Which of the following pairs of events...
You have a function taking in two integer variables, x and y. You want to subtract...
You have a function taking in two integer variables, x and y. You want to subtract x from y, but you want to make sure the result does not overflow. Write some lines of code (assuming x and y are already defined integers with values in them) to check if the result would be an overflow if x was subtracted from y. Do this without allowing an overflow to happen.
Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables...
Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables x and y, initialize them with different values. b. Use static memory allocation, declare px and py as address of x and y separately. c. Print out x, y, px, py, &x, &y, *px, *py.   d. Let py = px, and *py = 100 e. Print out x, y, px, py, &x, &y, *px, *py. g. Print out *px++, x, px 2) Dynamic allocation...
Instructions: Be sure to define all parameters and random variables used. Also, you must provide any...
Instructions: Be sure to define all parameters and random variables used. Also, you must provide any code you use (final computed values are not sufficient). I strongly recommend that you create Jupyter Notebooks for this assignment, embedding your written comments in Markdown cells. 4. A researcher conducts an experiment in a residence for senior citizens to investigate the effect of floor type on the risk of fall-related injury. For 24 randomly selected individuals in the facility, she records the type...
Instructions: Be sure to define all parameters and random variables used. Also, you must provide any...
Instructions: Be sure to define all parameters and random variables used. Also, you must provide any code you use (final computed values are not sufficient). I strongly recommend that you create Jupyter Notebooks for this assignment, embedding your written comments in Markdown cells. 6. A biologist wishes to examine the collision avoidance behaviour of bullfrogs housed in experimental boxes. He placed an object in the upper visual field of each frog and then moved the object progressively closer until the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT