Question

Write a C++ code for this question- Write a program with a method that plays the...

Write a C++ code for this question- Write a program with a method that plays the guess a number game. The program should allow the user to pick a number between 1 and 1000 in his head. The method should guess the user's number in a minimal amount of attempts. The method should ask the user "is the number greater than or less than the number    " and the program gives a particular number. The user in some way just inputs (higher or lower) or (greater than or less than). There also has to be a way for the user to say the number has just been guessed. Of course, the user cannot answer incorrectly or change the number midstream. Note - if written efficiently, the method should be able to guess any number in 10 or less attempts. (This question has me at a loss for words and I have no idea how to do this. Please try to do it as basic as possible so I can study and learn from this! I greatly appreciate the help!)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <iostream>

using namespace std;

// defining method guess

// with two parameters low and high

int guess(int low, int high){

// delcaring string

string reply;

// if high is greater than low

  if(high>=low){

// finding mid number of high and low

    int avg = (high+low)/2;

// asking user whether it is the number or not

    cout << "Is " << avg << " the guessed number? (yes/no)" << endl;

    cin >> reply;

// if it is the number then returning number

    if(reply.compare("yes")==0) return avg;

    // if mid number is not the number

if(reply.compare("no")==0){

// asking user whether the number is greater or lesser

      cout << "Is the number `greater` or `lesser` than the number " << avg << endl;

cin >> reply;

// if greater then calling method guess

// with new arguements

      if (reply.compare("greater")==0) return guess(avg, high);

      // else calling function with lesser than arguements

return guess(low, avg);

    }

  }

  return -1;

}

int main() {

cout << "Pick a number between 1 and 1000 in your head." << endl;

int num = guess(1,1000);

cout << "Number is " << num << endl;

}

SAMPLE OUTPUT: (i guessed 400)

unknown.png?width=385&height=472

Add a comment
Know the answer?
Add Answer to:
Write a C++ code for this question- Write a program with a method that plays the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

  • C++, data structure Write a program that plays a game called "guess the state, guess the...

    C++, data structure Write a program that plays a game called "guess the state, guess the capital". How do you play? The program randomly selects a state or a state capital. The program asks the player to guess the state's capital (or the capital's state). The program reads the user's guess; and, tells the user if its guess is right, or if the guess is wrong, tells the user that its guess is wrong and displays the correct answer. Before...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT