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

1. Given the following segment of code: (If there is nothing output, write None

ID: 3680686 • Letter: 1

Question

1.   Given the following segment of code: (If there is nothing output, write None.)

int x;

int y;

cin >> x;

cin >> y;

while (x > y)

{

    x -= 3;

    cout << x << " ";

}

cout << endl;

      a.    What are the output and final values of x and y when the input is 10 for x and 0 for y? [2, 2, 2]

            Output

                                                                                                                                                                        x = ______________

                                                                                                                                                                        y = ______________

      b.   What are the output and final values of x and y when the input is 8 for x and 8 for y? [2, 2, 2]

            Output

                                                                                                                                                                        x = ______________

                                                                                                                                                                        y = ______________

      c.    What are the output and final values of x and y when the input is 17 for x and 5 for y? [2, 2, 2]

            Output

                                                                                                                                                                        x = ______________

                                                                                                                                                                        y = ______________

2.   What are the values of count and num after the following code is executed? [2, 2]

int count = 5;

int num = 3;

do

{

    if (count % 2 != 0)

        num -= 4;

    else

        num += 2;

    count++;

}

while (count < 9);

      count = ______________

num = ______________

3.   What are the output and final values of i and val? [2, 2, 2]

                                                                    

int i = 7;

int val = 0;

for (i = 20; i >= 8; i -= 4)

{

    cout << i << " ";

    val += i;

}

cout << val << endl;

            Output

                                                                                                                                                                        i = ______________

                                                                                                                                                                        val = ______________

Explanation / Answer

1.

a.
output:   
x = 7, 4, 1, -2 (-2 is the final value of x)
y = 0

b.
output:   
x = 8 (while loop will terminates due to condition is false)
y = 8

c.
output:   
x = 14, 11, 8, 5 (5 is the final value of x)
y = 5

-----------------------------------------------------------------
2.

count = 9
num = -1

-----------------------------------------------------------------

3.

i = 20, 16, 12, 8 (for loop values, 8 is the final value)
val = 56