Thanks! Exercise 3.2 Exercise 3.2.1. Revise the program so that it uses a while
ID: 3860992 • Letter: T
Question
Thanks!
Exercise 3.2
Exercise 3.2.1. Revise the program so that it uses a while loop. You can either continue to use the do_more variable, or you can instead do away with this variable and use the break keyword to exit as appropriate.
Exercise 3.2.2. After you’ve played the game a few times (or even if you haven’t), you should have some idea of the optimum player’s strategy. Can you summarize this strategy in pseudocode?
Exercise 3.2.3. Assuming that you’ve completed Exercise 3.2.2, you should be able to write a program that implements the optimum strategy. Assume that you, the user, have picked a number in secret. The program should go into a loop in which it asks YOU (the user) whether the guess is too high, too low, or just right. The program should then use the high/low answer to refine its guess and make another. Note that you are on your honor to answer honestly. Tip: To make the program easy to write, use an arbitrary system of numbers to signal the answer: 1=too high, 2=too low, 3=success. This should be printed in the prompt:
Tell me how I did. 1=too high, 2=too low, 3=success.
Exercise 3.2.4. This exercise is more of a math question than a programming question, but it’s still interesting. For any game in which there are n possible choices, what is the minimum number of guesses needed to ensure success in the game? (For example, if 7 is this number, it would mean that by following optimum strategy, you’ll never need more than 7 guesses.)
You should be Introducing Ra Numbers controls the able t see what's going here. A Boolean variable, do whether by action of the loop. Therefore, the program can easily terminate it the setting do more to If not this just reports guessing guess was too high, too low, or just right, and then (if appro- continues to prompt the user. cpp #includeExplanation / Answer
/*
Please find modified lines for which comments are mentioned
do while : it is a looping condition which runs by default for the first time and checks whether the condition is getting satisfied or not from second time onwards
while : Unless the condition is satisfied, it will not execute the block inside it
*/
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int n=0;
int target = rand() % 50 + 1;
while(true) // modified line
{
cout << "Enter your guess: ";
cin >> n;
if(n==0){
cout << "Bye!";
break; // modified line
}else if (n>target){
cout << "Guess is too high" << endl;
}else if (n<target){
cout << "Guess is too low" << endl;
}else{
cout << "You Win!";
cout << "Answer is " << n << endl;
break; // modified line
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.