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 = ______________
The final value of x and y is
a.
x= -2
y= 0
Explanation:
x=10 y=0
while(x>y) => (10>0) TRUE
x=x-3 = 10-3 = 7
while(x>y) => (7>0) TRUE
x=x-3 = 7-3 = 4
while(x>y) => (4>0) TRUE
x=x-3 = 4-3 = 1
while(x>y) => (1>0) TRUE
x=x-3 = 1-3 = -2
final value of x=2 and y=0
b.
x= 8
y= 8
Explanation:
x=8 y=8
while(x>y) => (8>8) FALSE
final value of x=8 and y=8
c.
x= 5
y= 5
Explanation:
x=17 y=5
while(x>y) => (17>5) TRUE
x=x-3 = 17-5 = 14
while(x>y) => (14>0) TRUE
x=x-3 = 14-5 = 11
while(x>y) => (11>5) TRUE
x=x-3 = 11-5 = 8
while(x>y) => (8>0) TRUE
x=x-3 = 8-5 = 5
while(x>y) => (5>5) FALSE
final value of x=8 and y=8
Get Answers For Free
Most questions answered within 1 hours.