Its a guess game with 4 rounds.. i just need to find the average number of guesses. I DO NOT need the number of guesses for each round which i already have.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
srand(time(0));
int start;
int end;
cout<<"Enter the starting value for what your guess can be between ";
cin>>start;
cout<<"Enter the ending value for your guess can be between ";
cin>>end;
int range;
range=(start-end);
int secretNumber = (rand() % range) + start;
int NumberOfGuesses = 0;
int NumberOfGames = 0;
int AvgGuesses;
int guess;
do {
cout<<"Round "<<NumberOfGames+1<<" "<<endl;
++NumberOfGames;
do
{
cout << "Enter a guess: ";
cin >> guess;
++NumberOfGuesses;
if (guess > secretNumber)
{
cout << "Too high!" << endl << endl;
}
elseif (guess < secretNumber)
{
cout << "Too low!" << endl << endl;
}
} while (guess != secretNumber);
if (guess == secretNumber)
{
cout << "You win! You got it in " << NumberOfGuesses << " guesses" << endl;
secretNumber = (rand() % range) + start;
NumberOfGuesses=0;
}
}while (NumberOfGames<4);
AvgGuesses=NumberOfGuesses/NumberOfGames;
cout<<"Average number of guesses "<<AvgGuesses;
}
Well I'm embarrassed to say this but this took me a lot longer than I thought it would (The average part) but I managed to do it so here's the your program with an average guesses calculation. Please feel free to ask any questions you want. You were just putting the average part in the wrong spot, plus you have to treat it like a sum variable.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio>
usingnamespace std;
int main()
{
randomize();
int start;
int end;
cout<<"Enter the starting value for what your guess can be between ";
cin>>start;
cout<<"Enter the ending value for your guess can be between ";
cin>>end;
int range;
range=(end-start);
int secretNumber = (rand() % range) + start;
int TotalNumberOfGuesses = 0;
int NumberOfGuesses = 0;
int NumberOfGames = 0;
int AvgGuesses = 0;
int guess = 0;
do {
clrscr();
cout<<"Round "<<(NumberOfGames)+1<<" "<<endl;
++NumberOfGames;
NumberOfGuesses=0;
do
{ ++NumberOfGuesses;
cout << "Enter a guess: ";
cin >> guess;
if (guess > secretNumber)
{
cout << "Too high!" << endl << endl;
}
elseif (guess < secretNumber)
{
cout << "Too low!" << endl << endl;
}
} while (guess != secretNumber);
if (guess == secretNumber&&NumberOfGames<4)
{ TotalNumberOfGuesses=TotalNumberOfGuesses+NumberOfGuesses;
cout <<"You win this round! You got it in " << NumberOfGuesses << " guesses" << endl;
secretNumber = (rand() % range) + start;
getch();
}
}while (NumberOfGames<4);
clrscr();TotalNumberOfGuesses=TotalNumberOfGuesses+NumberOfGuesses;
cout << "You win! You got it in " << TotalNumberOfGuesses << " guesses" << endl;
AvgGuesses=TotalNumberOfGuesses/NumberOfGames;
cout<<"Your average number of guesses is "<<AvgGuesses;
getch();
return 0;
}