Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the program you wrote in Task 1 to update the function startGuessTheNumberGame to implement Guess the Number game, which works as follows: The program

Modify the program you wrote in Task 1 to update the function startGuessTheNumberGame to implement Guess the Number game, which works as follows: The program asks the user to enter a secret integer between 1 and 10000 as shown in Figure 2. Figure 2 Then program will try to guess the number and print the guess on the screen. After that the user will need to select one of 3 options available; confirm to the program that the guess is correct, or that the secret number is larger than the guess, or that the secret number is smaller than the guess as shown in Figure 3. Figure 3 Page 6 of 10 If the user chooses 1, the game ends, and the program will show to the users how many guesses did it take to find their secret number as explained in task 4, then it will return to the main menu. If the user chooses 2 or 3, the program will make another guess and display the 3 options again, and so on until it finds the secret number. If the user enters any other number, the program will display an error message "Invalid choice." and redisplay the guess and wait for a valid choice. To implement this game, you are going to implement an algorithm called Binary Search. Binary Search is an algorithm that can search for a number among a group of sorted numbers in a very fast manner. Consider the following example that searches among the numbers 1 to 8. Assume 3 is the secret number we want to find as shown in Figure 4. Figure 4 First, the program will guess the middle number in the range 1-8, in this case 4. Then, the user will respond to the program that their secret number is less than 4. Now that the program knows that the secret number is less than 4, it will refine its search range to exclude all the numbers greater than or equal to 4. This results in discarding half the numbers. The new range will be 1-3 as shown in Figure 5. Figure 5 Now, the program will make another guess, similar to the previous one, it will guess the Page 7 of 10 middle number in the range, in this case 2. The user will respond to the program that their secret number is greater than 2. Now that the program knows that the secret number is greater than 2, it will refine its search range to exclude all the numbers less than or equal to 2. This results in discarding half the numbers. The new range will be 3-3 as shown in Figure 6. Figure 6 ; Repeating the steps again: The program will make another guess. Since the range contains one number only it will be the guess, in this case 3. The user will respond to the program that the guess is correct. The program will print to the user the number of guesses, which is 3 (first guess was 4, second was 2, and the last one was 3 - so 3 in total). The following pseudo code describes the binary search algorithm:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago