Question

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 100. After N questions, program exits by showing success score (number of correct / number of all questions). See a sample run:

------Sample Run------

Enter number of questions I need to ask: 5

Q1) 3 + 4 = 6

Incorrect, 7 was the answer.

Q1) 8 – 4 = 4

Correct

Q1) 5 * 6= 30

Correct

Q1) 5 - 345= -300

Incorrect, it should be -340.

Q1) 0-0=0

Correct

Your success rate is 60%.

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

// C++ program to ask maths questions to user and calculate their success rate

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

       srand(time(NULL));

       unsigned short op1, op2;

       int result, userAns;

       int num_questions, correct_ans = 0;

       char opr[] = {'+','-','*','/'};

       // input of number of questions to ask

       cout<<"Enter number of questions I need to ask: ";

       cin>>num_questions;

       // validate number of question to be between [3,10] , re-prompt until valid

while(num_questions < 3 || num_questions > 10)

       {

             cout<<"Number of questions must be between [3,10]"<<endl;

             cout<<"Enter number of questions I need to ask: ";

             cin>>num_questions;

       }

       // loop to ask n number of questions

       for(int i=0;i<num_questions;i++)

       {

             cout<<"Q"<<(i+1)<<") ";

             int oprIndex = rand()%4; // randomly generate the operator

             // randomly generate the operands 1 and operands 2

             op1 = rand()%101;

             op2 = rand()%101;

             // perform the operation based on the operator

             switch(opr[oprIndex])

             {

             case '+': // addition

                    result = op1+op2;

                    cout<<op1<<" + "<<op2<<" = ";

                    break;

             case '-': // subtraction

                    result = op1-op2;

                    cout<<op1<<" - "<<op2<<" = ";

                    break;

             case '*': // multiplication

                    result = op1*op2;

                    cout<<op1<<" * "<<op2<<" = ";

                    break;

             case '/': // division

                    // check if op2 = 0, loop to randomly generate value for op2 until we get a non-zero value for op2

                    while(op2 == 0)

                    {

                           op2 = rand()%101;

                    }

                    result = op1/op2;

                    cout<<op1<<" / "<<op2<<" = ";

                    break;

             }

             cin>>userAns; // input user answer

             // check if user answer is correct

             if(result == userAns)

             {

                    cout<<"Correct"<<endl;

                    correct_ans++; // increment correct answer count

             }else

                    cout<<"Incorrect, "<<result<<" was the answer"<<endl;

       }

       if(num_questions > 0)

       {

             // calculate and display the success rate

             int success_rate = (correct_ans*100)/num_questions;

             cout<<"Your success rate is "<<success_rate<<"%"<<endl;

       }

       return 0;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
This program is to ask the user N number of math (using only +, -, *,...
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....

  • 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...

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • use java thanks Write a program that prompts a user to enter number of Math question that she wishes the system to gene...

    use java thanks Write a program that prompts a user to enter number of Math question that she wishes the system to generate. The system will random generate Math questions which consisted of ±' , x and / of two positive integer numbers. The sample run programs are depicted as below Sample run 1 How many Math question you want to create? 4 Sample run 2 How many Math question you want to create? 10 4 questions have been created...

  • Write a program using M.I.P.S that asks user “how many positive number that is devisable by...

    Write a program using M.I.P.S that asks user “how many positive number that is devisable by 6 you want to add?” .Then your loopcounter would be the user input. If the user enters a positive number between 1 and 100 that is devisable by 6, you increment your loop counter and add it to the sum.. You need to decide if the positive number entered by the user is divisible by 6 or not. Your program should print an error...

  • C++ only Implement a program that prompts a user to enter a number N where N...

    C++ only Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial): The function must use loop for the implementation. Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only...

  • Modular program mathlab: Write a program in Matlab that would continuously ask the user for an...

    Modular program mathlab: Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “You got it” If the user’s die is smaller, it should display, “Nice try” If the results are the...

  • Using Java IDE: Write an application that asks elementary students a set of 10 math problems...

    Using Java IDE: Write an application that asks elementary students a set of 10 math problems ● First ask the user for a level and a problem type. ● You need to validate the level and problem type and loop until the user enters a correct one. ● There should be 3 levels. Level 1 operands would have values in the range of 0-9, level 2 operands would have values in the range of 0-99, and level 3 operands would...

  • 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...

  • Need a program in java that creates a random addition math quiz The program should ask...

    Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered

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