2

So I am creating a program where I have to create random problem sets that have random numbers and operators. I had no problem making random numbers. However, I am confused on how to randomize the three operators I need to use (addition, subtraction, and multiplication). I know I have to use numbers to represent these three operators, but I don't understand how to do that. I have to use the random number generator in order to do this and If & Then statements. Here is my source code.

I've tried creating a separate constant called "const int MAXOP_VALUE = 3" . I am stuck on what to do afterward. How do I represent the addition, subtraction and multiplication operators as numbers?

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>  
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
/*Constants*/
const int MIN_VALUE = 1;
const int MAX_VALUE = 100;

/*Variables*/
int number_1;
int number_2;
int math_op;

/*Get the System Time*/
unsigned seed = time(0);

/*Seed the Random Number Generator*/
srand(seed);

/*Generates Random Numbers for the Math Problems*/
number_1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
number_2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

/*Answer to Problem*/


/*Explains How the Program Works*/
cout << "****************************************" << endl << endl;
cout << "Welcome to the awesome math tutor! \n";
cout << "Get ready to add, subtract, and multiply!" << endl << endl;
cout << "****************************************" << endl << endl;
cout << "How much is" << number_1 << math_op << number_2 << "?" << 
endl;


return 0; 
}

I expect the output to be along the lines of this: "What is 25 +42 ?" "What is 54*3 ?" "What is 76-2 ?"

Jurga Burga
  • 33
  • 1
  • 3

2 Answers2

2

One liner for generating random math_op. Remove the int math_op and put this line somewhere after srand(seed).

char math_op = "+-*"[rand() % 3];

And you may use switch-case statement for the actual calcuation.

Sid S
  • 6,037
  • 2
  • 18
  • 24
Hanjoung Lee
  • 2,123
  • 1
  • 12
  • 20
0

Once you have the two random numbers, you can use another random number to generate the operation and expected result, something like:

char op; int expected;
switch(rand() % 3) {
    case 0:  op = '+'; expected = num1 + num2; break;
    case 1:  op = '-'; expected = num1 - num2; break;
    default: op = '*'; expected = num1 * num2; break;
}

Then you'll be able to output the expression and compare what's entered with the expected result:

int answer;
std::cout << "What is " << num1 << " " << op << " " << num2 << "? ";
std::cin >> answer;
std::cout << "Your answer is " << (answer == expected) ? "right" : "wrong" << ".\n";

Normally I'd also suggest you check the expected result is okay, as in no overflow or divide-by-zero, or be wary of doing integer division where 5 / 2 == 2.

But with both numbers between one and a hundred, and divide-by-zero/integral-division being a non-issue as your specifications only allow for addition, subtraction, and multiplication, it should be fine.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953