Question

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

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 = ______________

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please find the errors of the following code a) int x[5]; int k = 10; for...
Please find the errors of the following code a) int x[5]; int k = 10; for (k = 0; k <= 5; k++) { x[k] = k -1; } b) int x[5]; for (k = 1; k <= 5; k++) { cout << x[k-1] << endl; } c) int x[5]={1,2,3,4,5}, y[5]; y = x; for (k = 0; k < 5; k++) { cout << y[k] << endl; } d) int size; cin >> size; int myArr[size];
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark...
There are 7 syntax errors and 3 semantic errors in the following code segment. Please mark them in the code and explain the reason briefly in place. #include<iostream> using namespace std; int main() { cout<<”Please input the radius (in integer): ”>>endl; int 1var = 10, var=20; double area = 40; cin<< var; //get an input of the radius and store in var float continue; float const pi = 2.64; pi += 0.5; do { cout<<”The radius is ”<<”var”<<endl; //print the...
(C++ program) Use the code segment below to answer the two questions that follow. … int...
(C++ program) Use the code segment below to answer the two questions that follow. … int size = 0; cout << “Enter size: “; cin >> size; int sizeCode = size / 10; if (sizeCode == 0)    cout << “extra small”; else if(sizeCode == 1 )    cout << “small”; else if (sizeCode == 2)    cout << “medium”; else if (sizeCode == 3)    cout << “large”; else    cout << “extra large”; //end if … What would...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
When the user enters 2 the following program segment should print the message “You’ve selected to...
When the user enters 2 the following program segment should print the message “You’ve selected to get an A”, however it always prints “You’ve selected to get a C”. Rewrite the code segment to behave properly, i.e., print the proper message: int selection; cout << “Please enter the grade you would like to receive “ << endl; cout << “1 = C, 2 = A”; cin >> selection; if (selection = 1) cout << “You’ve selected to get a C“...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT