Write a c++ program that grades an online math quiz. The program asks the studen
ID: 3770791 • Letter: W
Question
Write a c++ program that grades an online math quiz. The program asks the student to enter his/her answer to 10 multiple-choice questions. All of the quiz answers are integers. The program stores the student’s responses in an array and then compares those responses to the correct answers to determine the student’s grade. The correct answers are:
Questions=1 2 3 4 5 6 7 8 9 10
Answers = 5 22 35 0 16 28 4 3 42 51
Your program must use this main procedure which calls the three procedures you are to implement. setCorrectAnswers takes an out parameter, corrrectAnswers, which is an array of integers. It sets each array element to the correct answer according to the table above. inputAnswers takes an out parameter, givenAnswers which is also an array of integers. It asks the student to input their answers to each of the 10 questions. This procedure must use a loop. numberCorrect takes both correctAnswers and givenAnswers as in parameters and has an integer out parameter, numRight. It sets numRight to the number of questions the student answered correctly. Hints numberCorrect will require a selection nested inside a loop.
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
#define title "MathTest"
#define quests 9
#define miss 6
#define add_min 1
#define add_max 9
#define sub_min 1
#define sub_max 9
#define mul_min 2
#define mul_max 9
#define div_min 2
#define div_max 9
char uname[16];
int key = -1;
int one, two, sol;
int i, q;
int tries;
int st_right, st_wrong, st_total, st_questions;
int sl_right, sl_wrong, sl_total;
bool solved, playing;
int random(int min, int max)
{
return rand() % (max - min + 1) + min;
}
int random(int max)
{
return rand() % max + 1;
}
void comment(char con)
{
if (con == '~')
{
switch (random(3))
{
case 1: cout << "Wrong answer (>_<)"; break;
case 2: cout << "You should try again."; break;
case 3: cout << "Be carefull."; break;
}
}
if (con == '@')
{
switch (random(3))
{
case 1: cout << "You lost the game (;_;)"; break;
case 2: cout << "Game over."; break;
case 3: cout << "How could you let this happen? (T_T)"; break;
}
}
if (con == '!')
{
cout << "Right!";
}
if (con == 'v')
{
cout << "Victory!";
}
if (con == '<')
{
cout << "Try smaller number.";
}
if (con == '>')
{
cout << "Try higher number.";
}
if (con == '+')
{
cout << "A bit higher...";
}
if (con == '-')
{
cout << "A bit lower...";
}
}
void stats()
{
cout << endl << " ***Game statistics***" << endl;
cout << " Total questions: " << st_questions << endl;
cout << " Total answers: " << st_total << endl;
cout << " Right answers: " << st_right << endl;
cout << " Wrong answers: " << st_wrong << endl;
if (st_total != 0)cout << " Accuracy: " << st_right * 100 / st_total << '%' << endl;
}
void stats_local()
{
cout << endl << " ***Round statistics***" << endl;
cout << " Round result: ";
if (playing) cout << "Victory" << endl; else cout << "Defeat" << endl;
cout << " Total questions: " << q << endl;
cout << " Total answers: " << sl_total << endl;
cout << " Right answers: " << sl_right << endl;
cout << " Wrong answers: " << sl_wrong << endl;
cout << " Accuracy: " << sl_right * 100 / sl_total << '%' << endl;
}
void test(char type)
{
sl_right = 0; sl_wrong = 0; sl_total = 0; playing = true;
for (q = 1; q <= quests && playing; q++)
{
tries = miss;
solved = false;
if (type == '+')
{
add_max);
two = random(add_min, add_max);
sol = one + two;
cout << endl << q << ". " << one << " + " << two << " = ?" << endl;
}
if (type == '-')
{
sol = random(sub_min, sub_max);
two = random(sub_min, sub_max);
+ two;
cout << endl << q << ". " << one << " - " << two << " = ?" << endl;
}
if (type == '/')
{
sol = random(div_min, div_max);
two = random(div_min, div_max);
* two;
cout << endl << q << ". " << one << " / " << two << " = ?" << endl;
}
if (type == '*')
{
mul_max);
two = random(mul_min, mul_max);
sol = one * two;
cout << endl << q << ". " << one << " * " << two << " = ?" << endl;
}
while (tries > 0 && !solved)
{
cout << " Your answer > "; cin >> i;
sl_total++;
if (i == sol)
{
sl_right++;
comment('!');
solved = true;
}
else
{
sl_wrong++;
tries--;
if (tries > 0)
{
if (i < sol + 4 && i > sol) comment('-');
else if (i > sol - 4 && i < sol) comment('+');
else if (i < sol + 16 && i > sol) comment('<');
else if (i > sol - 16 && i < sol) comment('>');
else comment('~');
cout << endl << "You have " << tries << " tries remaining!" << endl;
}
else
{
solved = true;
playing = false;
comment('@');
cout << endl << "The correct answer was... " << sol << "!";
}
}
}
}
q--;
st_questions += q;
st_total += sl_total;
st_right += sl_right;
st_wrong += sl_wrong;
if (playing) comment('v');
stats_local();
}
int main()
{
srand(time(NULL));
cout << "What is your name?" << endl << " > "; cin >> uname;
while (1)
{
cout << endl << endl;
cout << "Welcome, " << uname << ", to " << title << "!" << endl;
cout << "Please choose one of the following options:" << endl;
cout << " [1] Addition" << endl;
cout << " [2] Subtraction" << endl;
cout << " [3] Division" << endl;
cout << " [4] Multiplication" << endl;
cout << " [5] Statistics" << endl;
cout << " [0] Exit" << endl;
cout << " > "; cin >> key;
if (key == 1) test('+');
if (key == 2) test('-');
if (key == 3) test('/');
if (key == 4) test('*');
if (key == 5) stats();
if (key == 0) return 1;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.