Question

In: Computer Science

(Please answer in C++ ) This approach is similar to Task 2B. The only difference is,...

(Please answer in C++ )
This approach is similar to Task 2B. The only difference is, the computer will generate a random integer in the range as a guess.
In this strategy, a guess is a random int in a given range. Hence, for the same range and same answer, the number of guesses will be different. For example, suppose the range is in [0, 10] and the answer is 7. These are possible outputs.
The following running takes 8 tries to get the answer.
Enter left end in range: 0
Enter right end in range: 10
User has an int in [0, 10]. Computer will guess.

guess #1: 10. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1

guess #2: 1. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2

guess #3: 9. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1

guess #4: 2. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2

guess #5: 4. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2

guess #6: 8. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1

guess #7: 5. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2

guess #8: 7. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 3
Congratulations! The answer is 7.

In the following running, it takes only three guesses to get the answer.
Enter left end in range: 0
Enter right end in range: 10
User has an int in [0, 10]. Computer will guess.

guess #1: 6. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 2

guess #2: 8. How is my guess?
1. too big 2. too small 3. just right
Enter only 1, 2, or 3: 1

guess #3: The answer must be 7.

Solutions

Expert Solution

ANSWER:-

GIVEN:-

// including required headers
#include <iostream>
using namespace std;

int main()
{
// variables to store left and right boundaries
int left,right;
  
// prompt for left end range
cout << "Enter left end in range: ";
// reading value left
cin >> left;
// prompt for right end range
cout << "Enter right end in range: ";
// reading value right
cin >> right;
  
// Printing the user input left and right
cout << "User has an int in ["<<left<<", "<<right<<"].\n";
// Printing Computer Guesses
cout << "Computer will guess.\n";

// variables to store count of Guesses ,Option and Computer guesses
int Guesses = 0,Option = 0, Computer_guess = 0;
  
// A infinite while loop
while(true)
{
// incrementng Guesses count
Guesses ++;
// generating a random number in range of left and right
Computer_guess = (rand()%(right - left + 1)) + left;
// Printing Computer guess
cout << "guess #" << Guesses <<": " << Computer_guess << "\n" ;
// Printing options
cout << "How is my guess? 1. too big 2. too small 3. just right\n";
// prompt for option entry
cout << "Enter only 1, 2, or 3: ";
// reading Option from user
cin >> Option;
  
// If User Option is 1
if(Option == 1)
{
// As the generated value is too big
// change right value
right = Computer_guess - 1;
}
// If User Option is 2
else if(Option == 2)
{
// As the generated value is too small
// change left value
left = Computer_guess + 1;
}
// If User Option is 3
else if(Option == 3)
{
// Printing the Congratulation Message
cout << "Congratulations! The answer is " << Computer_guess <<".";
// breaking the loop
break;
}
}
return 0;
}
//code ended here

PROGRAM SCREEN SHOT:-

OUTPUT:-


Related Solutions

In C++ Task 2B: User picks an integer in a range and computer guesses using approach...
In C++ Task 2B: User picks an integer in a range and computer guesses using approach similar to binary-search Enter the left end and right end of a range of integers. User chooses an integer in that range. Computer makes a guess that equals the mid of the range. User gives a feedback for each guess: 1 for too big, 2 for too small and 3 for just right. When the feedback is 1 (too big), the computer throws away...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List L; // Creates an empty list L of polynomials public Polynomials ( ) { … } // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) { … } // Inserts polynomial p into L public void Insert (Polynomial p) {...
Please answer question 2b. If you are writing by hand please make handwriting as readable as...
Please answer question 2b. If you are writing by hand please make handwriting as readable as possible. 2. Lithography: (a) Polymers containing aromatic rings are more likely to absorb UV photons, leading to chain scission. Would this make them negative or positive resists for lithography. (2 points) (b) Explain briefly how lift off works. (2 points) (c) Explain why chips are exposed with a stepper (so only a small field of view is illuminated at a time) in deep UV...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order by using a function. Then sort the array(list) Use a function to display the menu. Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum,...
Please answer only d-j Q Corporation and R Inc. are two companies with very similar characteristics....
Please answer only d-j Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between the two companies is that Q Corporation is an unlevered firm, and R Inc. is a levered firm with debt of $3.5 million and cost of debt of 10%. Both companies have earnings before interest and taxes (EBIT) of $1.5 million and a marginal corporate tax rate of 35%. Q Corporation has a cost of capital of 15%. a. What...
The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being...
The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being that the Lucas Numbers start with L0 = 2 and L1 = 1 as opposed to Fibonacci’s F0 = 0 and F1 = 1. Concretely, they are defined by L0 = 2, L1 = 1, and Ln := Ln−1 + Ln−2 for n > 1. Write a function in C++ that takes an integer argument N and returns the sum of the first N...
Task # 1. For the homogeneous autocatalytic reaction A + B → 2B, where –rA =...
Task # 1. For the homogeneous autocatalytic reaction A + B → 2B, where –rA = kCACB, aconversion rate of A of X = 0.44. The velocity constant is k = 10-3  m3 / (mol s). The feed stream of 0.1 m3 / s contains A with CA0  = 90 mol / m3 and B with CB0 = 10 mol / m3. It is considered whichreactor configuration that will be most suitable 1.1 The reaction must take place in a flow reactor....
Consider the reaction A+2B⇌CA+2B⇌C whose rate at 25 ∘C∘C was measured using three different sets of...
Consider the reaction A+2B⇌CA+2B⇌C whose rate at 25 ∘C∘C was measured using three different sets of initial concentrations as listed in the following table: Trial [A][A] (MM) [B][B] (MM) Rate (M/sM/s) 1 0.50 0.050 1.5×10−2 2 0.50 0.100 3.0×10−2 3 1.00 0.050 6.0×10−2 Part A What is the rate law for this reaction? Express the rate law symbolically in terms of kkk, [A][A], and [B][B]. View Available Hint(s)
********PLEASE ANSWER************* there are 31.4g sample of an unknown hydrocarbon sample containing only C, H, and...
********PLEASE ANSWER************* there are 31.4g sample of an unknown hydrocarbon sample containing only C, H, and O. if those 31.4g are burned completely in O2 and gives 92.4g of CO2 and 27.0g of H2O, what is the empirical formula of the unknown compound?
Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between...
Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between the two companies is that Q Corporation is an unlevered firm, and R Inc. is a levered firm with debt of $3.5 million and cost of debt of 10%. Both companies have earnings before interest and taxes (EBIT) of $1.5 million and a marginal corporate tax rate of 35%. Q Corporation has a cost of capital of 15%.                                       a.   What is Q Corporation’s...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT