Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- int randomNum = rand() % 100 + 1;
- int guess, guessCount = 1;
- bool found = false;
- do
- {
- cout << "Guess a random number from 1-100: ";
- cin >> guess;
- if (guess == randomNum)
- {
- found = true;
- cout << "Congratulation you found the number in " << guessCount << " guesses" << endl;
- }
- else if (guess < randomNum)
- {
- cout << "Too Low, keep trying" << endl;
- }
- else if (guess > randomNum)
- {
- cout << "Too high, keep trying" << endl;
- }
- guessCount++;
- cout << endl;
- } while (!found);
- cout << endl << endl << "Computer Section" << endl;
- cout << "Enter a number you want the computer to guess: ";
- cin >> guess;
- found = false;
- guessCount = 1;
- do
- {
- randomNum = rand() % 100 + 1;
- if (guess == randomNum)
- {
- found = true;
- cout << "Congratulation you found the number in " << guessCount << " guess" << endl;
- }
- else if (randomNum < guess)
- {
- cout << "Too Low: " << randomNum << endl;
- }
- else if (randomNum > guess)
- {
- cout << "Too high: " << randomNum << endl;
- }
- guessCount++;
- cout << endl;
- } while (!found);
- cout << endl << endl << "Computer Section 2.0" << endl;
- cout << "Here the computer should guess much faster(less than 7)" << endl;
- cout << "Enter a number you want the computer to guess: ";
- cin >> guess;
- found = false;
- int maxRange = 100;
- int lowRange = 1;
- int newHigh = 0;
- guessCount = 1;
- do
- {
- randomNum = rand() % maxRange + lowRange;
- if (guess == randomNum)
- {
- found = true;
- cout << "Congratulation you found the number in " << guessCount << " guesses" << endl;
- }
- else if (randomNum < guess)
- {
- cout << "Too Low: " << randomNum<< endl;
- newHigh = maxRange + lowRange;
- lowRange = randomNum+1;
- maxRange = newHigh- lowRange;
- for (int x = 0; x < newHigh; x++)
- {
- if ((lowRange + x) < guess)
- {
- lowRange += x;
- maxRange = newHigh - lowRange;
- }
- }
- }
- else if (randomNum > guess)
- {
- cout << "Too high: " << randomNum << endl;
- maxRange = randomNum-lowRange;
- for (int x = 0; x < randomNum - 1; x++)
- {
- if ((randomNum - x) > guess)
- {
- maxRange = (randomNum-x) - lowRange;
- }
- }
- }
- guessCount++;
- cout << endl;
- } while (!found);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement