Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C language Task 3 Modify the program you wrote in Task 1 to update the function startGuess TheNumberGame to implement Guess the Number game, which

image text in transcribedimage text in transcribed

image text in transcribed

C language

Task 3 Modify the program you wrote in Task 1 to update the function startGuess TheNumberGame 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. Enter a secret number between 1 and 10000: Figure? 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. Enter a secret number between 1 and 10000: 350 entered 350 My guess is: 5000 Please choose one of the following 3 options: 1. My guess is correct. 2. The secret number is larger than 5000. 3. The secret number is smaller than 5000. The user Figwe If the user chooses 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. 1 2 3 4 5 6 78 1 1 Figure 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. 23 4 5 67 8 00 1 1 1 Figures Now, the program will make another guess, similar to the previous one, it will guess the 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. 45 678 1 2 3 1 1 Figure 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 - 5o 3 in total). The following pseudo code describes the binary search algorithm: step 1: START step 2: secret Number = 3 step 3: low = 1, high = 8 step 4: repeat the following as long as low s high: step 5: mid = (low + high) / 2 step 6: if (secret Number == mid): step 7: print "Secret number found!" step 8: else if (secret Number > mid): step 9: low = mid + 1 step 10: else step 11: high = mid-1 step 12: END

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

More Books

Students also viewed these Databases questions