4.3 More while examples

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Output: Enter first positive integer: 9 Enter second positive integer: 7 GCD is: 1 ... Enter first positive integer: 15 Enter second positive integer: 10 GCD is: 5 ... Enter first positive integer: 99 Enter second positive integer: 33 GCD is: 33 ... Enter first positive integer: 500 Enter second positive integer: 500 GCD is: 500 Refer to the GCD code above. Assume user input of numA = 15 and numB = 10. What is the value of numB after the first iteration of the while loop?

10

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Output: Enter first positive integer: 9 Enter second positive integer: 7 GCD is: 1 ... Enter first positive integer: 15 Enter second positive integer: 10 GCD is: 5 ... Enter first positive integer: 99 Enter second positive integer: 33 GCD is: 33 ... Enter first positive integer: 500 Enter second positive integer: 500 GCD is: 500 Refer to the GCD code above. Assume user input of numA = 15 and numB = 10. For the GCD program, what is the value of numA before the first loop iteration?

15

#include <iostream> #include <string> using namespace std; /* Program that has a conversation with the user. Uses if-else statements and a random number (sort of) to mix up the program's responses. */ int main() { int randNum0_3; // Random number 0 to 3 string userText; // User input cout << "Tell me something about yourself." << endl; cout << "You can type \"Goodbye\" at anytime to quit." << endl << endl << "> "; getline(cin, userText); while (userText != "Goodbye") { randNum0_3 = userText.length() % 4; // "Random" num. %4 ensures 0-3. if (randNum0_3 == 0) { cout << endl << "Please explain further." << endl << endl << "> "; } else if (randNum0_3 == 1) { cout << endl << "Why do you say: \"" << userText << "\"?" << endl << endl << "> "; } else if (randnum0_3 == 2) { cout << endl << "I don't think that's right." << endl; << endl << "> "; } else { cout << endl << "Uh-oh, something went wrong. Try again." << endl << endl; } getline(cin, userText); } cout << endl << "It was nice talking with you. Goodbye." << endl; return 0; } How many loop iterations will execute if the user plans to type "I'm hungry", "You are weird", "Goodbye", and "I like you".

2

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Output: Enter first positive integer: 9 Enter second positive integer: 7 GCD is: 1 ... Enter first positive integer: 15 Enter second positive integer: 10 GCD is: 5 ... Enter first positive integer: 99 Enter second positive integer: 33 GCD is: 33 ... Enter first positive integer: 500 Enter second positive integer: 500 GCD is: 500 Refer to the GCD code above. Assume user input of numA = 15 and numB = 10. How many loop iterations will the algorithm execute?

2

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Consider the example above. For the first input sequence, what is the final value of numValues?

4

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Consider the example above. How many actual (non-sentinel) values are given in the first input sequence?

4

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Output: Enter first positive integer: 9 Enter second positive integer: 7 GCD is: 1 ... Enter first positive integer: 15 Enter second positive integer: 10 GCD is: 5 ... Enter first positive integer: 99 Enter second positive integer: 33 GCD is: 33 ... Enter first positive integer: 500 Enter second positive integer: 500 GCD is: 500 Refer to the GCD code above. Assume user input of numA = 15 and numB = 10. What is numB after the second iteration of the while loop?

5

Type the program's output. #include <iostream> using namespace std; int main() { int currValue; int maxValue; cin >> currValue; maxValue = currValue; while (currValue > 0) { if (currValue > maxValue) { maxValue = currValue; } cin >> currValue; } cout << "Max value: " << maxValue; return 0; } Input is: 2 34 12 0

Max value: 2

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Consider the example above. Suppose the first input was 0. Would valuesSum / numValues be 0? Yes or No.

No

#include <iostream> #include <string> using namespace std; /* Program that has a conversation with the user. Uses if-else statements and a random number (sort of) to mix up the program's responses. */ int main() { int randNum0_3; // Random number 0 to 3 string userText; // User input cout << "Tell me something about yourself." << endl; cout << "You can type \"Goodbye\" at anytime to quit." << endl << endl << "> "; getline(cin, userText); while (userText != "Goodbye") { randNum0_3 = userText.length() % 4; // "Random" num. %4 ensures 0-3. if (randNum0_3 == 0) { cout << endl << "Please explain further." << endl << endl << "> "; } else if (randNum0_3 == 1) { cout << endl << "Why do you say: \"" << userText << "\"?" << endl << endl << "> "; } else if (randnum0_3 == 2) { cout << endl << "I don't think that's right." << endl; << endl << "> "; } else { cout << endl << "Uh-oh, something went wrong. Try again." << endl << endl; } getline(cin, userText); } cout << endl << "It was nice talking with you. Goodbye." << endl; return 0; } What will be printed if the user types "Ouch"?

Please explain further.

#include <iostream> #include <string> using namespace std; /* Program that has a conversation with the user. Uses if-else statements and a random number (sort of) to mix up the program's responses. */ int main() { int randNum0_3; // Random number 0 to 3 string userText; // User input cout << "Tell me something about yourself." << endl; cout << "You can type \"Goodbye\" at anytime to quit." << endl << endl << "> "; getline(cin, userText); while (userText != "Goodbye") { randNum0_3 = userText.length() % 4; // "Random" num. %4 ensures 0-3. if (randNum0_3 == 0) { cout << endl << "Please explain further." << endl << endl << "> "; } else if (randNum0_3 == 1) { cout << endl << "Why do you say: \"" << userText << "\"?" << endl << endl << "> "; } else if (randnum0_3 == 2) { cout << endl << "I don't think that's right." << endl; << endl << "> "; } else { cout << endl << "Uh-oh, something went wrong. Try again." << endl << endl; } getline(cin, userText); } cout << endl << "It was nice talking with you. Goodbye." << endl; return 0; } What will be printed if the user types in "Bye"?

What else can you share?

#include <iostream> using namespace std; // Output GCD of user-input numA and numB int main() { int numA; // User input int numB; // User input cout << "Enter first positive integer: "; cin >> numA; cout << "Enter second positive integer: "; cin >> numB; while (numA != numB) { // Euclid's algorithm if (numB > numA) { numB = numB - numA; } else { numA = numA - numB; } } cout << "GCD is: " << numA << endl; return 0; } Consider the example above. What would happen if this list was input: 10 1 6 3 -1 a. Output would be 5 b. Output would be 4 c. Error

a

#include <iostream> #include <string> using namespace std; /* Program that has a conversation with the user. Uses if-else statements and a random number (sort of) to mix up the program's responses. */ int main() { int randNum0_3; // Random number 0 to 3 string userText; // User input cout << "Tell me something about yourself." << endl; cout << "You can type \"Goodbye\" at anytime to quit." << endl << endl << "> "; getline(cin, userText); while (userText != "Goodbye") { randNum0_3 = userText.length() % 4; // "Random" num. %4 ensures 0-3. if (randNum0_3 == 0) { cout << endl << "Please explain further." << endl << endl << "> "; } else if (randNum0_3 == 1) { cout << endl << "Why do you say: \"" << userText << "\"?" << endl << endl << "> "; } else if (randnum0_3 == 2) { cout << endl << "I don't think that's right." << endl; << endl << "> "; } else { cout << endl << "Uh-oh, something went wrong. Try again." << endl << endl; } getline(cin, userText); } cout << endl << "It was nice talking with you. Goodbye." << endl; return 0; } Which if-else branch will execute if the user types "Goodbye"? Valid answers are branch 0, 1, 2, 3, or none.

none


Kaugnay na mga set ng pag-aaral

Operational: Cyber Security 5.1 & 5.2 REVIEW QUESTIONS & VOCABULARY

View Set

AP Macroeconomics Module 11: Interpreting Real Gross Domestic Product

View Set

Mgmt Info Systems: Exam 1 Review

View Set

Five Principles for Communication

View Set

Ch 6 The Business Plan: Visualizing the Dream

View Set

Language Arts 09, Section - 4 Modules - Adi Final

View Set

Embryology (Gray's Anatomy Review & Lippincott & BRS)

View Set