Big exercise ! help
Oct 21, 2012 at 4:21pm UTC
Hi,look at this.
Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.
★ Modify the program to output how many guesses it took the user to correctly guess the right number.
★★ Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.
★★★★ Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.
I did the second part correctly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <time.h>
using namespace std;
int main ()
{
srand (time(NULL));
int a=rand() % 100 + 1;
int c;
int b=rand() % 100 + 1;
do {
cout << "guess:" ;
cout << b << endl;
if (b>a) {
c++;
b=rand() % b + 1;
cout << "too high" << endl;}
else if (b<a){
c++;
b=rand() % 100 + b;
cout << "too low" << endl;}
}while (b!=a);
cout << "done!" << endl;
cout << "it took you " << c << " tries" << endl;
return 0;}
And thats how i did the third part but still not working:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
[code]
#include <iostream>
#include <time.h>
using namespace std;
int main ()
{
srand (time(NULL));
int a=rand() % 100 + 1;
int b=rand() % 100 + 1;
int c;
int d;
int e;
int f;
int g;
int i;
int j;
for (int x=0;x<10000;x++) {
cout << "guess:" ;
cout << b << endl;
if (b==a) { break ; }
else if (b>a) {
c++;
d=rand() % b + 1;
cout << "too high" << endl;}
else if (b<a){
c++;
d=rand() % 100 + b;
cout << "too low" << endl;}
cout << "guess" ;
cout << d;
if (d==a) { break ; }
else if ( d>a) {
c++;
e=rand() % d + 1;
cout << "too high" << endl;}
else if (d<a) {
c++;
e=rand() % b + d;
cout << "too low" << endl;}
cout << "guess:" ;
cout << e;
if ( e==a) {break ;}
else if (e>a) {
c++;
f=rand() % e + 1;
cout << "too high" << endl;}
else if (e<a) {
c++;
f=rand() % d + e;
cout << "too low" << endl; }
cout << "guess:" ;
cout << f;
if ( f==a) { break ; }
else if ( f > a) {
c++;
g=rand() % f + 1;
cout << "too high" << endl;}
else if ( f<a) {
c++;
g=rand() % e + f;
cout << "too low" << endl; }
cout << "guess:" ;
cout << g;
if (g==a) { break ; }
else if (g>a) {
c++;
i=rand() % g + 1;
cout << "too high" << endl; }
else if ( g<a) {
c++;
i=rand() % f + g;
cout << "too low" << endl; }
cout << "guess:" ;
cout << i;
if (i==a) { break ; }
else if ( i > a) {
c++;
j=rand() % i + 1;
cout << "too high" << endl; }
else if ( i<a) {
c++;
j=rand() % g + i;
cout << "too low" << endl; }
cout << j;
if ( j==a) { break ; }
else { cout << "cant" << endl; }
}
cout << "done!" << endl;
cout << "it took you " << c << " tries" << endl;
return 0;}
Last edited on Oct 21, 2012 at 4:23pm UTC
Oct 21, 2012 at 4:49pm UTC
can anyone help?
Topic archived. No new replies allowed.