Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(20 pts) Convert the following if statement into a switch statement. if (answer

ID: 2268054 • Letter: #

Question

(20 pts) Convert the following if statement into a switch statement.

if (answer == ‘y’) {
printf(“Let’s do it ”);
printf(“Be ready at 5:00pm ”);
countYes++;
} else if (answer == ‘n’) {
printf(“May be next time ”);
countNo++;
} else if (answer == ‘u’) {
printf(“Still undecided? ”);
} else {
printf(“This is not a valid options ”);
}

2. (20 pts) Give the output of the following fragment. Provide a fully detailed explanation.

int delta = 26;
switch (delta/5) {
case 1: printf(“delta is small”);
case 5: printf(“delta is midsize”);
case 10: printf(“delta is large”);
default: printf(“delta can be anything”);
}

3. (20 pts) Give the output of the following program fragments.
a. Program 1
int value = 7;
do {
printf(“%d “,value);
value *= 2;
} while (value < 225);

b. Program 2
int alpha = 10, beta = 10;
for (alpha = 0; alpha < 5; alpha++) {
for (beta = 100; beta < 300; beta = beta + 100)
printf(“%d”, alpha);
printf(“ ”);
}

4. (20 pts) Convert the following for loop into a while loop.
for (row = 50; row >= 1; row-- )
if (row % 2 == 0)
printf(“Even iteration #%d ”, row);
else
printf(“Odd iteration #%d ”, row);


5. (20 pts) Give the value of x and y in each of the following cases, after the execution of both
statements. (2pts)
Program 1:
int x = 0, y = 0;
y = x = 11, 12, 13, 14;

Program 2:
int x = 0, y = 0;
y = x = (11, 12, 13, 14);

Explanation / Answer

(1)

switch(answer)

case 'y':
     printf(“Let’s do it ”);
         printf(“Be ready at 5:00pm ”);
          countYes++;

case ‘n’:
       printf(“May be next time ”);
        countNo++;
case ‘u’:
            printf(“Still undecided? ”);

default:
      printf(“This is not a valid options ”);

(2)

Given that delta value is 26. In switch case value is 26/5 which is equal to 5.Then the case 5 will be printed.Hence

output is     delta is midsize

(3)

(a)

The output of this program is error because in while function lt not declared.

(b) it is also error because of above reason lt is not declared.

(4)

int row=50;

while(row){
if (row % 2 == 0)
printf(“Even iteration #%d ”, row);
else
printf(“Odd iteration #%d ”, row);

row--

}

(5)

program 1:

x=11

y=11

program 2:

x=14

y=14