Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ please construct a \"Quiz\" class. It is a part of a math quiz program

ID: 3770565 • Letter: U

Question

Using C++ please construct a "Quiz" class. It is a part of a math quiz program for elementary school children.

The "Quiz" class should have the following member items:

- the number of problems in the quiz

- a pointer to an array of problems

- the percentage grade

The "Quiz" class should have the following member functions:

-- a default constructor that asks the user for the number of problems and keeps track of the users response, dynamically allocates an array of Problems, asks the user for the type of Problems, updates each Problem in the array with the correct type, and initializes the grade.

-- a destructor that deletes the Problem array

-- a function to loop through all of the Problems in the array

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

class Problem
{
//member items
private:
int operand1;
int operand2;
char operation;
int user_ans;
int user_rem;
int crct_ans;
int crct_rem;

public:
//constructor
Problem(int first,int second)
{
operand1 = first;
operand2 = second;
user_ans = user_rem = crct_ans = crct_rem = 0;
operation = 'x';
}
//mutator function
void set_operation(char op){operation = op;}

//function to display the problem on screen
void display()
{
if(operation=='x')
{
cout<<"You have not defined the operation. ";
return;
}

cout<<"What is "<<operand1<<operation<<operand2<<" evaluates to? ";
if(operation == '/')
{
cout<<"Quotient->";
cin>>user_ans;
cout<<"Remainder->";
cin>>user_rem;

crct_ans = operand1/operand2;
crct_rem = operand1%operand2;
}else
{
cout<<"Result->";
cin>>user_ans;
if(operation == '+')
crct_ans = operand1 + operand2;
if(operation == '-')
crct_ans = operand1 - operand2;
if(operation == '*')
crct_ans = operand1 * operand2;
}
}

//check for correctioness of the user answer
bool correct()
{
if((crct_ans==user_ans)&&(crct_rem==user_rem))
{
cout<<"Congratulations you got the answer right ";
return true;
}
else
{
cout<<"Sorry the answer is wrong ";
return false;
}
}

//accessor functions
int get_operand1(){ return operand1; }
int get_operand2(){ return operand2; }
char get_operation(){ return operation; }
int get_user_ans(){ return user_ans; }
int get_user_rem(){ return user_rem; }
int get_crct_ans(){ return crct_ans; }
int get_crct_rem(){ return crct_rem; }
};

class Quiz
{
private:
int num;
Problem* head;
float grade;

public:
//constructor
Quiz()
{
srand(time(NULL));
cout<<"How many number of problems do you want to be in this problem? ";
cin>>num;

char operation;
int i,option,op1,op2;

head = (Problem*)malloc(num*sizeof(Problem));

cout<<"Option 1: Do you want to manually assign only operations to all the problems? ";
cout<<"Option 2: Do you want to manually do everthing? ";
cout<<"Record your Option::";
cin>>option;
if(option == 1)
{
for(i=0;i<num;i++)
{
cout<<" Problem no. "<<i+1<<endl;
cout<<"Operation? (Press + or - or / or * ) :: ";
cin>>operation;
if(operation == '/')
{
Problem temp(rand()%10,rand()%10+1);
temp.set_operation(operation);
head[i] = temp;
}
else
{
Problem temp(rand()%10,rand()%10);
temp.set_operation(operation);
head[i] = temp;
}
}
}else if(option == 2)
{
for(i=0;i<num;i++)
{
cout<<" Problem no. "<<i+1<<endl;
cout<<"Enter Operand1:: ";
cin>>op1;
cout<<"Enter Operand2:: ";
cin>>op2;

Problem temp(op1,op2);
cout<<"Operation? (Press + or - or / or * ) :: ";
cin>>operation;
temp.set_operation(operation);
head[i] = temp;
}
}else
cout<<"You entered wrong option ";

grade = 0;
  
}

//destructor
~Quiz() { free(head); }

//this functions loops over all the problems
void start()
{
int i;
for(i=0;i<num;i++)
{
cout<<" ";
head[i].display();
if(head[i].correct())
grade+=1;
}
grade = (grade*1.0/num )*100; //converting into percentage
cout<<"Score = "<<grade<<"%"<<endl;
}

};

int main()
{

Quiz obj;
obj.start();

return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote