Answer to Quiz 3(a) Tue 4 Sep 2001
1. Consider the statement i = j += k for some ints i, j, and k.
Write a sequence of instructions that do the same as this
statement WITHOUT using the operators += and -- :
k = k-1;
j = j+k;
i = j;
2. Analyze the following program :
-------------------------------------
include<stdio.h> | steps in | values after each step |
| |------------------------|
int main(void) | while | i | p |
{ |===================================|
int n = 100; | | | |
int i = 0, p = 1; | 0 | 0 | 1 |
| | | |
while (p < n) | 1 | 1 | 2 |
{ | 2 | 2 | 4 |
p *= 2; | 3 | 3 | 8 |
i++; | 4 | 4 | 16 |
} | 5 | 5 | 32 |
printf("%d=2^%d", p, i); | | | |
return 0; -------------------------------------
}
Complete the table at the right of the program
with values for i and p after each step in the
execution of the while loop.
Then answer the following:
(a) How many times is the while loop executed? 5
(b) What does the program print? 32=2^5