Min should be 1 and max should be 4.
Then did you even try what they mentioned?
Basically it is like this:
Get a random number append
Get a random number append
Get a random number append
Get a random number append
Then you are left with some random number , some random number , some random number , some random number.
So if you get 1 , 2 , 3 , 4 then it should be 1234. How do we get this?
well 1 should be multplied by 10^3 then 2 should be multiplied by 10^2 , 3 by 10^1 and 4 by 10^0.
enough pseudo code...
for the sake of simplictity I will do it reverse order so if the random numbers gather are 1 2 3 4 I will do
1 * 10^0 , 2 * 10^1 , 3 * 10^2 , 3 * 10^3
so it will be 4321 when added.
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
|
#include <iostream> //cout , cin , endl
#include <ctime> //time
#include <cstdlib> //srand , rand
int main()
{
srand( time(NULL) );
int number; //input for player
int digits = 0 , power = 1;
int min , max;
int guess , random;
int count = 0;
int temp;
std::cout << "Please enter a number to be guessed: ";
std::cin >> number;
temp = number;
while( temp > 0 )
{
++digits;
temp /= 10;
}
std::cout << "Please enter the minimum digit: ";
std::cin >> min;
std::cout << "Please enter the maximum digit: ";
std::cin >> max;
while( guess != number )
{
++count;
guess = 0;
power = 1;
for( int i = 0; i < digits; ++i )
{
random = rand() % ( max - min + 1 ) + min;
guess += random * power;
power *= 10;
}
std::cout << "The computer guessed: " << guess << std::endl;
std::cout << "The answer is: " << number << std::endl;
}
std::cout << "The computer took " << count << " tries to guess the number." << std::endl;
}
|