Question

Math Problem Generator Your job will be to write a C++ program to teach math to...

Math Problem Generator

Your job will be to write a C++ program to teach math to school children. Your program will accomplish this by showing sets of math problems to the user and allowing them to enter an answer. The program should tell the user if they are correct and should continue providing additional questions. The program will keep track of correct and incorrect answers. The questions should use random numbers and should be simple enough math problems that someone could do them without a calculator (ie. 8 + 17 =). The program will be timed (60 seconds for instance – this should be variable) and should keep track of how many you have gotten correct and incorrect during the time limit. As the user correctly answers the questions the difficulty should increase:
1+4 =
15+2-1=

15 + 22 * (18 / 2) % 14 =

Lastly, the program should have some kind of end and review screen which shows the user’s score and allows them to restart.

Sample Run:

Welcome to Math Friend, the best math game ever made. You will be
presented with a series of questions that you will be required to
answer. You have 60 seconds to answer as many questions as possible
and your time will start NOW!!!

60 seconds remaining

Question 1:

81 – 5 = _

76

Correct!

43 seconds remaining

Question 2:

9 * 3 – 1 = _

22

Incorrect : (

The answer was 26

37 seconds remaining

Question 3:

6+9 - _ = 7

11

Incorrect : (

The answer was 8

22 seconds remaining

Question 4:

6 - 22 = _

-18

Incorrect : (

The answer was -16

Time has elapsed

____________________________________________________________________

You reached question 4

Correct responses: 1

Incorrect responses: 3

Would you like to play again (y/n)

n

Thank you for playing…

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include "iostream"

#include <ctime>

using namespace std;

// function to generate an easy question invlcing only one operator

int generateEasyQuestion(){

int number1,number2;

int range = (10-1)+1;

srand(time(0));

number1 = range * (rand() / (RAND_MAX + 1.0)); // generate a random number

number2 = range * (rand() / (RAND_MAX + 1.0)); // generate second random number

while (number1 <= 0) { // if random number is zero generate again

number1 = range * (rand() / (RAND_MAX + 1.0));

}

number2 = range * (rand() / (RAND_MAX + 1.0));

while(number2 <= 0){ // if random number is zero generate again

number2 = range * (rand() / (RAND_MAX + 1.0));

}

int operator_range = (4-1)+1;

int operator1;

operator1 = operator_range * (rand() / (RAND_MAX + 1.0)); // generate a random number to chose operator

string expression = std::to_string(number1);

int answer = 0;

if (operator1 == 1){ // if random num is 1 operator is +

expression.append(" + ");

expression += std::to_string(number2);

answer = number1 + number2;

} else if(operator1 == 2) { // if random num is 2 operator is -

expression.append(" - ");

expression += std::to_string(number2);

answer = number1 - number2;

} else if(operator1 == 3){ // if random num is 3 operator is *

expression.append(" * ");

expression += std::to_string(number2);

   answer = number1 * number2;

} else{

expression.append(" / "); // if random num is other than above operator is /

expression += std::to_string(number2);

answer = number1 / number2;

}

expression.append(" = "); // append "=" at end

cout << expression << endl;

  

return answer;

}

//function to generate difficult question involving 2 operators

int generateDifficultQuestion(){

int number1,number2,number3;

srand(time(0));

int range = (10-1)+1;

number1 = range * (rand() / (RAND_MAX + 1.0)); // generate a random number

while (number1 <= 0) { // if random number is zero generate again

number1 = range * (rand() / (RAND_MAX + 1.0));

}

number2 = range * (rand() / (RAND_MAX + 1.0)); // generate a random number

while(number2 <= 0){ // if random number is zero generate again

   number2 = range * (rand() / (RAND_MAX + 1.0));

}

number3 = range * (rand() / (RAND_MAX + 1.0));// generate a random number

while(number3 <=0){ // if random number is zero generate again

   number3 = range * (rand() / (RAND_MAX + 1.0));

}

int operator_range = (4-1)+1;

int operator1,operator2,operator3;

operator1 = operator_range * (rand() / (RAND_MAX + 1.0)); // generate a random number to chose operator

operator2 = operator_range * (rand() / (RAND_MAX + 1.0)); // generate a random number to chose operator

operator3 = operator_range * (rand() / (RAND_MAX + 1.0)); // generate a random number to chose operator

string expression = std::to_string(number1);

int answer = 0;

if (operator1 == 1){ // if random num is 1 operator is +

expression.append(" + ");

expression += std::to_string(number2);

if (operator2 == 1){

expression.append(" + ");

expression += std::to_string(number3);

answer = number1+number2+number3;

} else if(operator2 == 2) {

expression.append(" - ");

expression += std::to_string(number3);

answer = number1+number2-number3;

} else if(operator2 == 3){

expression.append(" * ");

expression += std::to_string(number3);

   answer = number1+number2*number3;

} else{

expression.append(" / ");

expression += std::to_string(number3);

   answer = number1+number2/number3;

}

} else if(operator1 == 2) { // if random num is 2 operator is -

expression.append(" - ");

expression += std::to_string(number2);

if (operator2 == 1){

expression.append(" + ");

expression += std::to_string(number3);

answer = number1-number2+number3;

} else if(operator2 == 2) {

expression.append(" - ");

expression += std::to_string(number3);

answer = number1-number2-number3;

} else if(operator2 == 3){

expression.append(" * ");

expression += std::to_string(number3);

answer = number1-number2*number3;

} else{

expression.append(" / ");

expression += std::to_string(number3);

answer = number1-number2/number3;

}

} else if(operator1 == 3){ // if random num is 3 operator is *

expression.append(" * ");

expression += std::to_string(number2);

if (operator2 == 1){

expression.append(" + ");

expression += std::to_string(number3);

answer = number1*number2+number3;

} else if(operator2 == 2) {

expression.append(" - ");

expression += std::to_string(number3);

answer = number1*number2-number3;

} else if(operator2 == 3){

expression.append(" * ");

expression += std::to_string(number3);

answer = number1*number2*number3;

} else{

expression.append(" / ");

expression += std::to_string(number3);

answer = number1*number2/number3;

}

} else{

expression.append(" / "); // if random num is other than 1 ,2,3 operator is /

expression += std::to_string(number2);

if (operator2 == 1){

expression.append(" + ");

expression += std::to_string(number3);

answer = number1/number2+number3;

} else if(operator2 == 2) {

expression.append(" - ");

expression += std::to_string(number3);

answer = number1/number2-number3;

} else if(operator2 == 3){

expression.append(" * ");

expression += std::to_string(number3);

answer = number1/number2*number3;

} else{

expression.append(" / ");

expression += std::to_string(number3);

answer = number1/number2/number3;

}

}

  

expression.append(" = ");

cout << expression <<endl;

  

return answer;

}

int main(){

cout<<"Welcome to Math Friend, the best math game ever made. You will be presented with a series of questions that will be required to answer. You have 60 seconds to answer as many questions as possible and your time will start now" <<endl;

char choice = 'y';

int correctQuestions = 0; // variable for correct questions

int totalQuestions = 0; //variable for total questions

int answer;

int userAnswer;

bool isTimeElasped = false;

while (choice == 'y') { // keep on running till user enters y

answer = generateEasyQuestion(); // generate easy question first

totalQuestions = 0;

correctQuestions = 0;

isTimeElasped = false;

auto start = std::chrono::high_resolution_clock::now(); // start timer

while(!isTimeElasped){ // if time elapsed is less than 60 keep on going

totalQuestions++;

cin >> userAnswer; // get user anser

bool response = false;

if(answer == userAnswer){ // if answer is correct generate difficult question and increase correct question count

correctQuestions++;

cout<< "Correct!"<<endl;

response = true;

}else{ // if answer is icorrect generate an easy question

cout<<"Incorrect!"<<endl;

}

  

// Record finish time

auto finish = std::chrono::high_resolution_clock::now();

std::chrono::duration<double> elapsed = finish - start;

if (elapsed.count() >= 60){

isTimeElasped = true; // if 60 seconds are elapsed

} else{

int remainingTime = ((60 - elapsed.count()));

cout<< remainingTime << " seconds remaining" <<endl;

if(response){

   answer = generateDifficultQuestion();

}else{

answer = generateEasyQuestion();

}

}

}

//show data to output

cout<<"Time has elapsed"<<endl;

cout<<"You reached question "<<totalQuestions<<endl;

cout<<"Correct Responses: " <<correctQuestions<<endl;

cout<<"Incorrect Responses: "<< (totalQuestions-correctQuestions)<<endl;

cout<<"Would you like to play again(y/n)?"<<endl;

cin>>choice;

}

cout <<" Thankyou for playing...";

  

  

}

Add a comment
Know the answer?
Add Answer to:
Math Problem Generator Your job will be to write a C++ program to teach math to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a MIPS math quiz program in MARS. The program should start with a friendly user...

    Write a MIPS math quiz program in MARS. The program should start with a friendly user greeting. From there, it should generate a random arithmetic problem. Your program will need to generate three random things: the first and second operand and the operator to use. Your program should generate random positive integers no greater than 20 for the operands. The possible operators are +, -, * and / (division). The user should be prompted for an answer to the problem....

  • This week we looked at an example math program that would display the answer to a...

    This week we looked at an example math program that would display the answer to a random math problem. Enhance this assignment to prompt the user to enter the solution to the displayed problem. After the user has entered their answer, the program should display a message indicating if the answer is correct or incorrect. If the answer is incorrect, it should display the correct answer. The division section should use Real data types instead of Integer data types. Keep...

  • This program is part 1 of a larger program. Eventually, it will be a complete Flashcard...

    This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game. For this part, the program will Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the program The first line is the number of questions (just throw this line away this week)...

  • Instructions Basically, you will modify the mathq program to make it satisfy a different set of...

    Instructions Basically, you will modify the mathq program to make it satisfy a different set of requirements, and fix what is not working in it. Specifically, imagine that the client for whom we created the mathq program for would now like some changes. To be exact: Part 1: They want it to provide addition and subtraction problems as well as multiplication and division. They still want the choice to be random, as before, but now the problem is randomly multiplication,...

  • This program is to ask the user N number of math (using only +, -, *,...

    This program is to ask the user N number of math (using only +, -, *, and /) questions. Once the program start it asks the user to enter how many questions will be asked (N, which is between 3-10). Then, the program asks N questions to the user. Each question will be one of four math operations (+, -, *, and /). The operation and operands will be selected randomly in your program.Operand are “unsigned short” between 0 and...

  • I am writing a Java program. My program would ask two questions: (1) 1+1=? (2) 2+1...

    I am writing a Java program. My program would ask two questions: (1) 1+1=? (2) 2+1 =? Show one question at a time on the console. The program allows users to input answers via a command-line (no GUI). Give users five seconds to answer each question. The next question would show ONLY when the user answers the question correctly OR when the time is up. Once users answer the question correctly within the given time, the next question needs to...

  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

  • (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the...

    (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the four basic arithmetic operations, i.e., addition, subtraction, multiplication and integer division. A sample partial output of the math quiz program is shown below. The user can select the type of math operations that he/she would like to proceed with. Once a choice (i.e., menu option index) is entered, the program generates a question and asks the user for an answer. A sample partial output...

  • [Using Python] Create a program to generate one random question, from a list of 5, for...

    [Using Python] Create a program to generate one random question, from a list of 5, for each repeated run. Report the number of the correct answers after all five questions have been answered. Your 5 questions should be a simple math problem, like "what is 5-4?" use a while loop based on the number of questions use a counter variable to count the number of questions asked e.g. questionCount +=1 user a counter variable to keep track of the correct...

  • USING C LANGUAGE _Design_ a program that asks the user to answer (easy) multiplication problems. It...

    USING C LANGUAGE _Design_ a program that asks the user to answer (easy) multiplication problems. It is up to you how many math problems you want it to present. The numbers should be chosen at random. The program will then display the user stats: - number of problems answered correctly out of the total - percent correct - grade letter (>= 90 is A, >= 80 is B, >= 70 is C, >=60 is D, < 60 is F)   Example...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT