I need help writing a program that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The computer program will make guesses and the user will tell the program to guess higher or lower. A sample run of the program should look something like this, with the user inputs in bold: Think of a number between 1 and 100. Is it 50? (h/l/c): h Is it 75? (h/l/c): h Is it 88? (h/l/c): l Is it 81? (h/l/c): c Great! Do you want to play again? (y/n): y Think of a number between 1 and 100. Is it 50? (h/l/c): l Is it 25? (h/l/c): h Is it 37? (h/l/c): c

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need help writing a program that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The computer program will make guesses and the user will tell the program to guess higher or lower.

A sample run of the program should look something like this, with the user inputs in bold:

Think of a number between 1 and 100.
Is it 50?  (h/l/c): h
Is it 75?  (h/l/c): h
Is it 88?  (h/l/c): l
Is it 81?  (h/l/c): c
Great! Do you want to play again? (y/n): y
Think of a number between 1 and 100.  
Is it 50?  (h/l/c): l
Is it 25?  (h/l/c): h
Is it 37?  (h/l/c): c
Great! Do you want to play again? (y/n): n

Notice that the user inputs the characters ‘h’, ‘l’, and ‘c’ for higher, lower, and correct, respectively.

Every time the program makes a guess, it should guess the midpoint of the remaining possible values.  Consider the first example above, in which the user has chosen the number 81:

On the first guess, the possible values are 1 to 100. The midpoint is 50, so that's the computer's first guess.

The user responds by saying 'h' for “higher”

On the second guess the possible values are 51 to 100. The midpoint is 75.

The user responds by saying 'h' for “higher”

On the third guess the possible values are 76 to 100. The midpoint is 88.

The user responds by saying 'l' for “lower”

On the fourth guess the possible values are 76 to 87. The midpoint is 81.

The user responds 'c' for “correct”

Requirements

The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main() function, your solution should be heavily structured with functions. To receive credit, your main function must look like this:

int main()
{
do
{
  playOneGame();
} while (shouldPlayAgain());

return 0;
}

You must also have the following functions specified below:

The playOneGame() function has a return type of void. It implements a complete guessing game on the range of 1 to 100.  

The shouldPlayAgain() function has a bool return type. It prompts the user to determine if the user wants to play again, reads in a character, returns true if the character is a ‘y’, and otherwise returns false.

In addition, implement the helper functions getUserResponseToGuess(), and getMidpoint(). They should be called inside your playOneGame() function.

getUserResponseToGuess. This function takes a parameter guess of type int.  It prompts the user with the phrase “is it guess? (h/l/c): “  and returns a char. The char should be one of three possible values: ‘h’, ‘l’, or ‘c’.  For example, if parameter guess is 75, the prompt would be "is it 75? (h/l/c):" It has the following signature:

char getUserResponseToGuess(int guess)

getMidpoint. This function takes two int parameters, and returns the midpoint of the two integers. It has the following signature:

int getMidpoint(int low, int high)

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY