please someone else anwswer this it has been answered twice by the same person a
ID: 3827439 • Letter: P
Question
please someone else anwswer this it has been answered twice by the same person and is incorrect IN C++ (microsoft visual studios 2013) Write a program (with explanation //comments) 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
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void guessNumber (int secretNum) {
int guess;
cout<<"Enter the guess between 1 and 1000: ";
cin >> guess;
while(guess!= secretNum) {
if(guess < secretNum) {
cout<<"It is lower number"<<endl;
}
else {
cout<<"It is higher number"<<endl;
}
cout<<"Enter the guess between 1 and 1000: ";
cin >> guess;
}
cout<<"Congrats!. You did it"<<endl;
}
int main()
{
srand (time(NULL));
int randNum = rand() % 1000 + 1;
guessNumber(randNum);
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the guess between 1 and 1000: 500
It is higher number
Enter the guess between 1 and 1000: 400
It is lower number
Enter the guess between 1 and 1000: 450
It is lower number
Enter the guess between 1 and 1000: 480
It is lower number
Enter the guess between 1 and 1000: 490
It is higher number
Enter the guess between 1 and 1000: 485
It is lower number
Enter the guess between 1 and 1000: 487
Congrats!. You did it
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.