Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with my C++ homework. Thank you. Must follow the requirement. This is the code I got from another tutor. He declared add(),

I need help with my C++ homework. Thank you.

image text in transcribed

Must follow the requirement. This is the code I got from another tutor. He declared add(), multiply() and subtract() as int. but It must be void. Thank you.

#include

#include

using namespace std;

void getRandNum(int&);

bool isAnswerRight(int, int, int, char);

int add(int, int);

int multiply(int, int);

int subtract(int, int);

int main(){

srand(time(NULL));

int n1, n2, answer, option;

char choice, op;

do{

getRandNum(n1);

getRandNum(n2);

cout

cout

cout

cout

cout

cin>>option;

if(option==1)

op = '+';

else if(option==2)

op = '-';

else if(option==3)

op = '*';

cout

cin>>answer;

while(!isAnswerRight(n1, n2, answer, op)){

cout

cout

cin>>answer;

}

cout

cout

cin>>choice;

}while(choice=='y' || choice=='Y');

return 0;

}

void getRandNum(int& num){

num = rand()%9+1;

}

bool isAnswerRight(int n1, int n2, int answer, char op){

if(op=='+' && add(n1, n2)==answer)

return true;

else if(op=='-' && subtract(n1, n2)==answer)

return true;

else if(op=='*' && multiply(n1, n2)==answer)

return true;

else

return false;

}

int add(int n1, int n2){

return n1+n2;

}

int multiply(int n1, int n2){

return n1*n2;

}

int subtract(int n1, int n2){

return n1-n2;

}

Functions - Simple Math Tutor Instructions: Important: Please read carefully. All functions must be done exactly as asked. Create a C++ console application to generate two small positive random integers between 1 and 9. Ask the user to add, subtract, and multiply the two numbers. As long as the answer is incorrect, keep asking the user for a correct answer. Requirements: Create six functions with following prototypes: void getRandNum(int&); (One reference parameter only) Called by main twice to provide two small random integers (1 to 9) bool isAnswerRight(int, int, int, char); (Char is used to pass the operator) Pass the random numbers, the answer, and the operator (+,-;*) This function is called by the following functions. The following functions receive the random numbers as arguments and ask the questions from the user. They call the isAnswerRight() to check the answer. void add(int, int); (Called by the main function) void multiply(int, int); (Called by the main function) void subtract(int, int); (Called by the main function) How to do it: First, create getRandNum(). Test it to see if it is returning a random number. Code the add() function, and test it without error checking, first. Add the isAnswerRight() function, and call it by add() for error checking. subtract() and multiply() work the same as the add() function with different operators. Additional Requirement: After all questions are answered, ask the user if they want to continue. If the answer is yes, create two new random numbers. This must be done in a loop that will repeat the process again and again

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

Students also viewed these Databases questions

Question

What is the price-recovery component?

Answered: 1 week ago