Consider the program below which does some simple aritmetic operations and print
ID: 3551270 • Letter: C
Question
Consider the program below which does some simple aritmetic operations and prints the results. Fill in the missing statements and trace the output.
#include "iostream"
int main ()
{
// Declare 4 integer variables, w, x, y, and z, with initial
// Values of 5, 4, 8 and 2 respectively.
//********your code goes here
int result;
// What value will be stored in result after each of the
// following statements are executed? write your answer at
// the comment line on the right of each statement.
result = x + y % w / z; // result is______
result = y / x * 2; // result is______
result = (y - z) / (z % w); // result is_______
cout << result << endl; // _______will be printed
system("pause");
return 0;
}
Detailed computation (including the memory drawing) for
result = x + y % w / z
Detailed computation (including the memory drawing) for
result = y / x * 2;
Detailed computation (including the memory drawing) for
result = (y - z) / (z % w)
Explanation / Answer
In general a compiler uses infix to postfix conversion and then computes any expression.
in theis program the missing statements are :
int w = 5,x = 4,y = 8,z = 2;
result = x + y % w / z; // result is______ 5
1st : y % w = 8 % 5 = 3(quotient)
2nd 3 / z = 3 / 2 = 1(integer)
3rd : x + 1 = 5
result is modified to store 5
result = y / x * 2; // result is______ 1
the computation is started from right always
1st : X * 2 = 8
2nd : y / 8 = 1
result is modified to store 1
result = (y - z) / (z % w); // result is_______ 3
1st : z % w = 2 % 5 = 2
2nd : y - z = 8 - 2 = 6
3rd : 6 / 2 = 3
result is modified to store 5
cout << result << endl; // _______will be printed 3
since the last value stored in result is 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.