In: Computer Science
In C++
Write a program that will conduct a quiz consists of 10
arithmetic questions and display the final score as ‘Your score in
the quiz is X’ with a
response message in the next line. A response message will be as
follows based on the student’s
performance:
a. Number of correct answers >= 9: Excellent, your are passed
with ‘A’ grade!
b. Number of correct answers >= 7, but less than 9: Very Good,
you are passed with
‘B’ grade!
c. Number of correct answers >= 5, but less than 7: Good, you
are passed with ‘C’
grade!
d. Number of correct answers < 5: Sorry, we cannot pass you in
the quiz!
For generating a question, you need to write a
generateQuestion() function that will generate
a question.. In addition to generate the numbers randomly, the
function should randomly select the operator among +, -, and *
operators. If the operator is + or -, the numbers should be
double-digit. For * operator, the numbers should be single-digit.
The function should also compute the answer for the question to
compare it with the student’s answer. If the answer is correct, the
function returns 1, otherwise 0. Your main() function should call
this
function.
RAW CODE
#include <iostream>
using namespace std;
int generateQuestion(){
char Operator = "-+*"[rand() % 3]; //Choose random character from index 0,1,2
int num1,num2,answer,student_answer;
if(Operator == '*'){ // Check for Operator. (IF '*' THEN GO INSIDE LOOP)
num1 = rand() % 10; //Random number from 0-9 i.e. one digit number only
num2 = rand() % 10;
answer = num1*num2;
}
else{ // Check for Operator. (IF '+-' OR NOT '*' THEN GO INSIDE LOOP)
num1 = 10 + rand() % 90; //Random number from 10-99 i.e. two digit number only
num2 = 10 + rand() % 90;
if(Operator == '+'){ // If '+' then go inside loop
answer = num1 + num2;
}
else{ // If '-' then go inside loop
answer = num1 - num2;
}
}
cout<<num1<<Operator<<num2<<"=";
cin>>student_answer;
if(answer == student_answer){
return 1; // If Right Answer then return 1
}
else{
return 0; // If Wrong Answer then return 0
}}
int main()
{
srand(time(0)); // This will help generate random expressions every single time
int count = 0,points;
cout<<"QUIZ\n\n";
for(int i=0; i<10; i++){ // Loop 10 times. i.e 10 Questions
points = generateQuestion(); // Calling function
count += points;
}
if(count >= 9){
cout<<"Excellent, your are passed with ‘A’ grade!\n";
}
else if(count >= 7 && count <9){
cout<<"Very Good, you are passed with ‘B’ grade!\n";
}
else if(count >= 5 && count <7){
cout<<"Good, you are passed with ‘C’ grade!\n";
}
else{
cout<<"Sorry, we cannot pass you in the quiz!\n";
}
return 0;
}
SCREENSHOTS (CODE AND OUTPUT)
CODE
OUTPUT
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####