Write a C++ main function to accomplish the following tasks and name the source code arithmetic.cpp.1. Select an operator randomly from addition ('+'), subtraction ('-'), multiplication ('*'), division ('/'),and remainder ('%').Hint: You can create an array of characters (type char in C++) with elements '+', '-', '*', '/', and'%'. Generate a random integer to represent a valid index in this array. Retrieve the operator locatedat the chosen index in the array.Warning: To ensure accurate grading in Gradescope, save operators in the order of '+', '-', '*', '/',and '%'. If the chosen operator differs from the grading script’s expectation, you may not receivecredits for your work.2. Generate the first operand as a single-digit integer, that is, an integer in the range [0, 9].3. If the operator is either division or remainder, generate the second operand as a non-zero single-digitinteger, that is, an integer in the range [1, 9], otherwise, generate the second operand as a single-digitinteger in the range [0, 9].4. Perform the operation on the operands based on the chosen operator and save the result.5. Prompt the user with the problem, receive a reply from the user. If the reply is correct, print “true”;otherwise, print “false”.
Question
Write a C++ main function to accomplish the following tasks and name the source code arithmetic.cpp.1. Select an operator randomly from addition ('+'), subtraction ('-'), multiplication (''), division ('/'),and remainder ('%').Hint: You can create an array of characters (type char in C++) with elements '+', '-', '', '/', and'%'. Generate a random integer to represent a valid index in this array. Retrieve the operator locatedat the chosen index in the array.Warning: To ensure accurate grading in Gradescope, save operators in the order of '+', '-', '*', '/',and '%'. If the chosen operator differs from the grading script’s expectation, you may not receivecredits for your work.2. Generate the first operand as a single-digit integer, that is, an integer in the range [0, 9].3. If the operator is either division or remainder, generate the second operand as a non-zero single-digitinteger, that is, an integer in the range [1, 9], otherwise, generate the second operand as a single-digitinteger in the range [0, 9].4. Perform the operation on the operands based on the chosen operator and save the result.5. Prompt the user with the problem, receive a reply from the user. If the reply is correct, print “true”;otherwise, print “false”.
Solution
Here is the C++ code that accomplishes the tasks you described:
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0)); // seed for random number generation
char operators[] = {'+', '-', '*', '/', '%'}; // array of operators
int randomIndex = rand() % 5; // generate a random index for operator
char op = operators[randomIndex]; // select operator
int operand1 = rand() % 10; // generate first operand
int operand2;
if (op == '/' || op == '%') { // if operator is division or remainder
operand2 = 1 + rand() % 9; // generate second operand in the range [1, 9]
} else {
operand2 = rand() % 10; // generate second operand in the range [0, 9]
}
int result;
switch(op) { // perform operation based on operator
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
case '%': result = operand1 % operand2; break;
}
int userAnswer;
std::cout << "What is " << operand1 << " " << op << " " << operand2 << "?\n";
std::cin >> userAnswer;
if (userAnswer == result) {
std::cout << "true\n";
} else {
std::cout << "false\n";
}
return 0;
}
This code first generates a random operator from the array of operators. Then it generates the first operand. If the operator is division or remainder, it generates the second operand in the range [1, 9], otherwise it generates the second operand in the range [0, 9]. It then performs the operation based on the operator and saves the result. It prompts the user with the problem and receives a reply. If the reply is correct, it prints "true", otherwise it prints "false".
Similar Questions
Program that performs arithmetic operations with values of type char.
Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function
.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.Evaluate the expression. Return an integer that represents the value of the expression.Note that:The valid operators are '+', '-', '*', and '/'.Each operand may be an integer or another expression.The division between two integers always truncates toward zero.There will not be any division by zero.The input represents a valid arithmetic expression in a reverse polish notation.The answer and all the intermediate calculations can be represented in a 32-bit integer. Example 1:Input: tokens = ["2","1","+","3","*"]Output: 9Explanation: ((2 + 1) * 3) = 9
Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.