fork download
  1. //Cameron Pham CS1A Chapter 3, P. 146, #15
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Generate Random Addition Problems
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program acts as a math tutor for a young student. It generates two
  9.  * random numbers, pauses while the student solves it, and then displays the
  10.  * correct answer.
  11.  *
  12.  ******************************************************************************/
  13. #include <iostream>
  14. #include <cstdlib> // Allows for usage of rand() and srand()
  15. #include <ctime> // Allows for usage of time()
  16. using namespace std;
  17.  
  18. int main()
  19. {
  20. // Seed the random number generator and use the current time to make the
  21. // numbers different each time the program is run.
  22. srand(static_cast <unsigned int> (time(0)));
  23.  
  24. // Generate two random numbers between 100 and 999
  25. int number1 = rand() % 900 + 100;
  26. int number2 = rand() % 900 + 100;
  27.  
  28. // Calculate the correct answer.
  29. int answer = number1 + number2;
  30.  
  31. // Display the addition problem.
  32. cout << "Hi! I'm your personal 'Math Tutor'." << endl;
  33. cout << "Solve the following addition problem:" << endl;
  34.  
  35. cout << " " << number1 << endl;
  36. cout << "+ " << number2 << endl;
  37. cout << "---------" << endl;
  38.  
  39. // Pause the program so the student has time to solve the problem.
  40. system("pause");
  41.  
  42. // Display the correct solution.
  43. cout << endl;
  44. cout << "The correct answer is:" << endl;
  45.  
  46. cout << " " << number1 << endl;
  47. cout << "+ " << number2 << endl;
  48. cout << "---------" << endl;
  49. cout << " " << answer << endl;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
Hi! I'm your personal 'Math Tutor'.
Solve the following addition problem:
  572
+ 915
---------

The correct answer is:
  572
+ 915
---------
  1487
stderr
sh: 1: pause: not found