Math Tutor

I am just
Creative Commons License photo credit: *Zara

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.

I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.

3.15: Write a program that can be used as a math tutor for a young student. The program should display two random numbers to be added, such as

   247
+ 129
------

The program should then pause while the student works on the problem. When the student is ready to check the answer, he or she can press a key and the program will display the correct solution, for example:

   247
+ 129
------
   376

My solution:

/* Chapter 3 Challenge 15
   Written by Kirk Hingsberger
   September 26, 2007
   Math Tutor
*/

#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
  char ch;
  srand(time(0));
  const int SEED = 999;
  int num1, num2, num3;

  num1 = 1 + rand() % SEED;
  num2 = 1 + rand() % SEED;

  cout << setw(5) << num1 << endl;
  cout << "+ " << setw(3) << num2 << endl;
  cout << "_____" << endl;
  num3 = num1 + num2;
  cin.get(ch);
  cout << setw(5) << num3 << endl;

  return 0;
}

Here is the capture script I turned in for grading:

Script started on Thu 27 Sep 2007 01:24:48 PM MDT
$ cat chap3chlg15.cc
/* Chapter 3 Challenge 15
   Written by Kirk Hingsberger
   September 26, 2007
   Math Tutor
*/

#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
  char ch;
  srand(time(0));
  const int SEED = 999;
  int num1, num2, num3;

  num1 = 1 + rand() % SEED;
  num2 = 1 + rand() % SEED;

  cout << setw(5) << num1 << endl;
  cout << "+ " << setw(3) << num2 << endl;
  cout << "_____" << endl;
  num3 = num1 + num2;
  cin.get(ch);
  cout << setw(5) << num3 << endl;

  return 0;
}
$ g++ -o chap3chlg15 chap3chlg15.cc -s
$ chap3chlg15
  983
+  77
_____
 1060
$ exit
exit

Script done on Thu 27 Sep 2007 01:25:38 PM MDT
  • Share/Bookmark




Leave a Reply