Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. srand(time(NULL));
  10.  
  11. int randomNum = rand() % 100 + 1;
  12. int guess, guessCount = 1;
  13. bool found = false;
  14.  
  15. do
  16. {
  17. cout << "Guess a random number from 1-100: ";
  18. cin >> guess;
  19.  
  20. if (guess == randomNum)
  21. {
  22. found = true;
  23. cout << "Congratulation you found the number in " << guessCount << " guesses" << endl;
  24. }
  25. else if (guess < randomNum)
  26. {
  27. cout << "Too Low, keep trying" << endl;
  28. }
  29. else if (guess > randomNum)
  30. {
  31. cout << "Too high, keep trying" << endl;
  32. }
  33. guessCount++;
  34. cout << endl;
  35. } while (!found);
  36.  
  37.  
  38. cout << endl << endl << "Computer Section" << endl;
  39. cout << "Enter a number you want the computer to guess: ";
  40. cin >> guess;
  41.  
  42. found = false;
  43. guessCount = 1;
  44.  
  45. do
  46. {
  47. randomNum = rand() % 100 + 1;
  48.  
  49. if (guess == randomNum)
  50. {
  51. found = true;
  52. cout << "Congratulation you found the number in " << guessCount << " guess" << endl;
  53. }
  54. else if (randomNum < guess)
  55. {
  56. cout << "Too Low: " << randomNum << endl;
  57. }
  58. else if (randomNum > guess)
  59. {
  60. cout << "Too high: " << randomNum << endl;
  61. }
  62. guessCount++;
  63. cout << endl;
  64. } while (!found);
  65.  
  66.  
  67. cout << endl << endl << "Computer Section 2.0" << endl;
  68. cout << "Here the computer should guess much faster(less than 7)" << endl;
  69. cout << "Enter a number you want the computer to guess: ";
  70. cin >> guess;
  71.  
  72. found = false;
  73. int maxRange = 100;
  74. int lowRange = 1;
  75. int newHigh = 0;
  76. guessCount = 1;
  77.  
  78.  
  79. do
  80. {
  81. randomNum = rand() % maxRange + lowRange;
  82.  
  83. if (guess == randomNum)
  84. {
  85. found = true;
  86. cout << "Congratulation you found the number in " << guessCount << " guesses" << endl;
  87. }
  88. else if (randomNum < guess)
  89. {
  90. cout << "Too Low: " << randomNum<< endl;
  91. newHigh = maxRange + lowRange;
  92. lowRange = randomNum+1;
  93.  
  94. maxRange = newHigh- lowRange;
  95.  
  96. for (int x = 0; x < newHigh; x++)
  97. {
  98. if ((lowRange + x) < guess)
  99. {
  100. lowRange += x;
  101. maxRange = newHigh - lowRange;
  102. }
  103. }
  104. }
  105. else if (randomNum > guess)
  106. {
  107. cout << "Too high: " << randomNum << endl;
  108. maxRange = randomNum-lowRange;
  109.  
  110. for (int x = 0; x < randomNum - 1; x++)
  111. {
  112. if ((randomNum - x) > guess)
  113. {
  114. maxRange = (randomNum-x) - lowRange;
  115. }
  116. }
  117. }
  118.  
  119. guessCount++;
  120. cout << endl;
  121. } while (!found);
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement