Basic statements and expressions. Trace (show a \"memory snapshot\") the followi
ID: 3791789 • Letter: B
Question
Basic statements and expressions. Trace (show a "memory snapshot") the following statements by hand: int f=4, g=2, h; double r=2.S, q. h = f + r/g++; q = h%g*2; r-f(g 7)*q-h; Translate the following into equivalent C statements: Declare an integer variable named tickets and assign to it a number between 1 and 10. Double the current value of tickets (use multiplication). Declare a double variable named cost and assign the cost of a recent ticket you purchased for some event. Assume that the following statement has been executed: int boxes = 12; The statement which would correctly display the value of boxes is (circle the correct answer): a. printf("%d", boxes); printf("%d", &boxes;); scanf("%d", &boxes;); printf(''%f", boxes);Explanation / Answer
1.
On tracing the following statements by hand the output will be as follows:
int f = 4, g = 2, h;
double r = 2.5,q;
h = f+r/g;
g++;
q= h%g*2;
r = f - (g-7)*q-h;
Output: h = 4+2.5/2 = 4 + 1 = 5
g++ = 3
q = h%g*2 = 5%3*2 = 2*2 = 4.
r = f-(g-7)*q-h = 4 – (3-7)*4-5
= 4 – (-4)*4 -5
= 4 – (-16)-5
= 4 + 16 – 5 = 15
Since, it is required to show the memory snapshot, it means that it is required to show the value of each variable which represent a kind of memory. First, f and g are initialized to 4 and 2 respectively, then r is initialized to 2.5. Then, according to the operations, the value of each variable is updated.
2.
The equivalent C statements are as follows:
a.
To declare an integer variable named tickets and assign it to a number between 1 and 10, the statement will be:
int tickets = 7;
b.
The C statement to double the current value of the tickets (using multiplication) is as follows:
tickets = tickets*2;
c.
To declare a double variable named cost and assign the cost of a recent ticket you purchased for some event, the statement is as follows:
double cost;
cost = ticket;
3.
The statement which is given is: int boxes = 12;
The statement that correctly displays the value of the boxes is: printf(“%d”, boxes);
The correct option is a)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.