Homework Assignment #5

1. Let Si be a statement and Ci be a condition. i, j, k, kind are integer variables. For each of the following statements, draw a flowchart using the basic one-entry-one-exit control structures.

(a) if (C1)

if (C2)

for (i = 0; i<100; i++) S1;

else

for (j = 100; j>0; j--) S2;

else

for (k = 0; k<i+j; k++) S3;

(b) do {

while (C1) S1;

S2;

} while (C2);

(c) switch (kind) {

case 1: {

while (C1) {S1; S2;}

break;

}

case 2: {

for (i=0; i<100; i+=kind) S3;

break;

}

case 3:

do {S4;} while (C2);

}

2. Consider the following  program segment:

 int a; int i, j, k; 　 for (i=0; i<5; i++)   for (j=0; j<4; j++)     for (k=0; k<3; k++) a[i][j][k] = i + j + k;

Suppose the starting address of array a is 0X22FEF0. Write the value or address of each of the following expressions. Explain your answers.

(a) a+2

(b) *(a+2)+3

(c) **(a+3)+1

(d) *(*(a+3)+1)+1

(e) *(*(*(a+4)+3)+2)

3. For each of the following program segments, answer how many loop iterations are executed. Explain your answers.

(a)

 for (i=0; i<100; i=i+3)   for (j=i; j>=0; j--) {     a = a + i + j; }

(b)

 i= 100; j = 10; k = 15; while (i>=j+k) {   i = i - 4;   j = j + 3;   k = k - 5; }

(c)

 i = 500; j = 300; do {   if (i

4. For the following restaurant order procedure, (a) draw a flowchart to show the procedure, (b) modify the flowchart of (a) using only the basic one-entry-one-exit control structures.