Math Tutor Enhanced

The Adml Tutors
Creative Commons License photo credit: AdmlCrunch

4.9: This is a modification of Chapter 3′s Problem 15, which was:

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

This time, write a program that can be used as a math tutor for a young student. The program should display two random numbers that are to be added. The program should wait for the student to enter the answer. If the answer is correct, a message of congratulations should be printed. If the answer is incorrect, a message should be printed showing the correct answer.

My solution:

/* Chapter 4 Challenge 9
   Written by Kirk Hingsberger
   October 3, 2007
   Math Tutor - Enhanced
*/

#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, answer;

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

  cout << "Enter your answer after the problem appears:" << endl;
  cout << setw(5) << num1 << endl;
  cout << "+ " << setw(3) << num2 << endl;
  cout << "_____" << endl;
  cin >> answer;
  num3 = num1 + num2;
  cin.get(ch);

  cout << setw(5) << num3 << " is the correct answer, ";
  
  if (answer == num3)
    {
      cout << "great job!" << endl;
    }
  else if (answer > num3)
    {
      cout << "your answer (" << answer << ") was too high." << endl;
    }
  else
    {
      cout << "your answer (" << answer << ") was too low." << endl;
    }

  return 0;
}

Note: This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (4) was titled “Making Decisions”.

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.

Share




2 Responses to “Math Tutor Enhanced”

  1. You have a great site here. Questions.

    I’m an online student in Programming I. Are you a programmer?

    $4.95 a month for this site? And how hard was it to set up.

  2. Thanks, yep I’m a programmer. It wasn’t hard to setup, and yep Hostgator is super cheap as a host, very reliable and great value.

    Thanks again.

Leave a Reply