Question

In: Computer Science

C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In...

C++ Programming level 1

using step number 2 is a must

Guess the Number

Introduction In this assignment you will create a program that will simulate a number guessing game.

Skills: random, while-loop, Boolean flags

Algorithm to win

The guess the number game has an algorithm to lead to the winning value. The way it works is say you have the set of numbers 1 to 100. You always begin by choosing the half-way point, then a hint will be provided if you do not guess correctly. Based on that hint, you shift either the beginning or end point. For instance, for 1 to 100, select 50. Let’s say the system says “higher,” then you set your potential range from 51 to 100. Next, we would choose the half-way point between these two values, so 75. Let’s say that now we are told, “lower.” Our new range becomes 51 to 74. The half-way point would be 62, let’s say that this is our number the game would end.

main.cpp

Your program should follow these steps and generate similar output. Do not change the design and follow naming conventions:

1) Create two constants with appropriate name: smallest value set to 1, largest value set to 100.

2) Create the mt19937 engine and initialize it with the random device as shown in the Boolean lectures.

3) Create a uniform int distribution of type integer and set it to generate values between the constants for smallest and largest values.

4) Create a boolean flag variable called continue game, set it to true.

5) Create the main while loop of the game, and leave the condition as the continue game variable

6) Inside the loop, generate a random number and store it in a variable

7) Declare and initialize a variable for number of guesses, set it to 0

8) Prompt the user for a number as show in the example, you must use the constants to display the output for the numbers, do not use literals in the string

9) Get the initial guess from the user and store it in a variable called guess.

10) Create a Boolean value, is guess correct, and set it to determine if the guess is correct

11) Create another while loop that will loop while the guess is not correct

12) Inside this loop increment the number of guesses (choose the appropriate increment type)

13) Create a string variable, hint, and use the Ternary operator to either store “Lower” if the guess is higher than the random number, or “Higher” otherwise.

14) Display the Hint, and prompt the user again to enter another guess

15) Update the is correct guess variable.

16) Outside the innermost loop, display the Number of guesses it took

17) Prompt the user if they wish to play again and update the continue game Boolean flag.

18) Outside the outermost loop, display a message Thanking the player.

19) Look at the output

Test Run: Your code should look exactly as the one below:

Guess a number between 1 and 100: 50

Hint: Higher

Guess a number between 1 and 100: 75

Hint: Higher

Guess a number between 1 and 100: 82

Hint: Higher

Guess a number between 1 and 100: 90

Hint: Higher

Guess a number between 1 and 100: 95

Hint: Higher

Guess a number between 1 and 100: 99

Hint: Lower

Guess a number between 1 and 100: 97

Number of guesses: 6

Do you want to play again (y/n) ? n

Thank you for playing.

Solutions

Expert Solution

#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){

    int startNum = 1,endNum = 100;//declaration of start,end of the range.
    bool play = true;//intially play is true.because user needs to play game at least one time.
    char option;//will takes and store wheather game will continue or not.
    while(play){
        int num = -1,guess = -1,chances = 0;
        num = rand()%endNum + 1;//randomly getting the number between 1 to 100.
        //loop runs until user guess the number.
        do{
            cout<<"Guess the number between 1 and 100:";
            cin>>guess;
            //if guess is wrong then we nedd to display hint and increment the no of chances of the user.
            if(guess != num){
                cout<<(guess>num?"Higher":"Lower")<<endl;
                chances++;
            }
            
        }while(guess != num);//come outs from loop if guessed num and num both are same.
        //displays the no of chances and asks user to continue or not.
        cout<<"Number of guesses:"<<chances<<endl;
        cout<<"Do you want to play again(y/n)?";
        cin>>option;
        //if user option is n.then play becomes false then game will ends
        if(option == 'n'){
            play = false;
        }
    }
    cout<<"Thank you for playing."<<endl;
    return 0;
}


/*
output

Guess the number between 1 and 100:10
Lower
Guess the number between 1 and 100:50
Higher
Guess the number between 1 and 100:42
Number of guesses:2
Do you want to play again(y/n)?y
Guess the number between 1 and 100:50
Lower
Guess the number between 1 and 100:42
Lower
Guess the number between 1 and 100:68
Number of guesses:2
Do you want to play again(y/n)?y
Guess the number between 1 and 100:64
Higher
Guess the number between 1 and 100:42
Higher
Guess the number between 1 and 100:20
Lower
Guess the number between 1 and 100:30
Lower
Guess the number between 1 and 100:35
Number of guesses:4
Do you want to play again(y/n)?y
Guess the number between 1 and 100:35
Higher
Guess the number between 1 and 100:64
Higher
Guess the number between 1 and 100:22
Higher
Guess the number between 1 and 100:10
Higher
Guess the number between 1 and 100:1
Number of guesses:4
Do you want to play again(y/n)?y
Guess the number between 1 and 100:0
Lower
Guess the number between 1 and 100:20
Lower
Guess the number between 1 and 100:30
Lower
Guess the number between 1 and 100:40
Lower
Guess the number between 1 and 100:50
Lower
Guess the number between 1 and 100:80
Higher
Guess the number between 1 and 100:70
Number of guesses:6
Do you want to play again(y/n)?n
Thank you for playing.




*/

Related Solutions

Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at Merks Sales are paid based on an annual salary rather than an hourly wage. However, some employees are paid weekly while others are paid every other week (biweekly). Weekly employees receive 52 paychecks; biweekly employees receive 26 paychecks. The payroll manager wants a program that displays two amounts: an employee’s weekly gross pay and his or her biweekly gross pay. Complete an IPO chart...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers. Classes in C++ and Java can represent anything in the real world. This assignment is to write a compiler for Z++ programming language. The Z++ Programming Language Your program will test if an expression entered by the application user...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
Create an application that makes the user guess a number. The user must be allowed tries....
Create an application that makes the user guess a number. The user must be allowed tries. You must have a loop, user input (Scanner), and a constructor. (JAVA)
USING C PROGRAMMING ***CODE MUST BE MODULARIZED** Instructions: Create a program that will collect multiple receipts...
USING C PROGRAMMING ***CODE MUST BE MODULARIZED** Instructions: Create a program that will collect multiple receipts from multiple classrooms to accumulate total cookie sales.   Once all receipts have been processed, the program must show what classroom is won and the total amount of cookies they sold. The classrooms that are participating are from the:                 2nd Floor: 201, 202, 203, 204, 205, 206, 207, 208                 3rd Floor: 301,302, 303, 304, 305, 306, 307, 308, 309                 4th Floor: 401,...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Introduction: One of the advantages of using Loops is to reduce the amount of programming effort...
Introduction: One of the advantages of using Loops is to reduce the amount of programming effort especially when repeating the code is required. To demonstrate this advantages, the code below shows the PasswordGeneration class. The purpose of this program is to generate an ad hoc 12 letters long password upon a selection of a character from a string that contains a pool of letters. /*simple Java program that generate a 12 characters long passwords * author : Dr. Modafar Ati...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT