12. Driver’s License Exam The State Department of Motor Vehicles (DMV) has asked
ID: 3685834 • Letter: 1
Question
12.
Driver’s License Exam The State Department of Motor Vehicles (DMV) has asked you to write a program that grades the written portion of the driver’s license exam, which has 20 multiple choice questions. Here are the correct answers:
1.B 5.C 9.C 13.D 17.C 2.D 6.A 10.D 14.A 18.B 3.A 7.B 11.B 15.D 19.D 4.A 8.A 12.C 16.C 20.A
To do this you should create a TestGrader class. The class will have a correct_answers array of 20 characters, which holds the correct test answers. It will have two public member functions that enable user programs to interact with the class. These are: setCorrect_answers. The setCorrect_answers function receives a 20-character char array holding the correct answers and copies this information into its correct_answers array. grade. The grade function receives a 20-character array holding the test taker’s answers and compares each of their answers to the answers held in the correct_answers array.An applicant must correctly answer 15 or more of the 20 questions to pass the exam. The grade function should print out the following information: User answers and the correct answer for each question An "X" across the questions that the user answered incorrectly a message indicating whether the applicant passed or failed the exam the number of right answers and the number of wrong answers The client program that creates and uses a TestGrader object should rst make a single call to setCorrect_answers, passing it a char array containing the 20 correct answers. Once this is done it should allow a test taker’s 20 answers to be entered in a 20-character array. Then it should call the grade function to grade the exam to display the results of the grading process.
(Here is the tester)
// Chapter 8 - Programming Challenge 11, Driver's License Exam
// This program utilizes a TestGrader class to grade the written portion of a
// driver's license exam. The class grade function compares the applicant's
// answers, stored in one array, to the correct answers, stored in another array.
#include
#include
#include
using namespace std;
#include "TestGrader.h"
// ***************** Class tester ***************
int main()
{
const int NUM_QUESTIONS = 20;
// Create a TestGrader object & set its key with correct answers
char correct_ans[] = { 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A' };
// Array to hold test taker's answers
char user1_ans[20] = { 'A', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'A', 'C', 'D', 'A', 'D',
'A', 'C', 'B', 'D', 'A' };
char user2_ans[20] = { 'A', 'D', 'D', 'A', 'C',
'A', 'B', 'D', 'C', 'D',
'A', 'C', 'D', 'C', 'C',
'A', 'A', 'B', 'D', 'A' };
TestGrader DMVexam;
DMVexam.setCorrect_answers(correct_ans);
//------------- Grade grade User 1's answers ---------
cout << "------------ User # 1 ------------- ";
DMVexam.grade(user1_ans);
DMVexam.print();
//------------- Grade grade User 2's answers ---------
cout << " ------------ User # 2 ------------- ";
DMVexam.grade(user2_ans);
DMVexam.print();
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
const int NUM_QUESTIONS = 20;
class TestGrader
{
char correct_ans[NUM_QUESTIONS];
int correct=0;
public:
void setCorrect_answers(char cans[])
{
for(int i=0;i<NUM_QUESTIONS;i++)
correct_ans[i]=cans[i];
}
void grade(char ans[])
{
cout<<"User answer "<<"Correct answer "<<endl;
for(int i=0;i<NUM_QUESTIONS;i++)
{
cout<<endl;
cout<<ans[i]<<" "<<correct_ans[i]<<" ";
if(ans[i]==correct_ans[i])
{
correct++;
}
else
cout<<"X";
}
cout<<" ";
if(correct>15)
cout<<"Congrats! You have passed the exam"<<endl;
else
cout<<"You have failed in the exam.Better luck next time!"<<endl;
cout<<"No of correct answers: "<<correct<<endl;
cout<<"No of wrong answers: "<<NUM_QUESTIONS-correct<<endl;
}
};
int main()
{
char correct_ans[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C','A', 'B', 'A', 'C', 'D','B', 'C', 'D', 'A', 'D','C', 'C', 'B', 'D', 'A'};
char user1_ans[NUM_QUESTIONS] = { 'A', 'D', 'A', 'A', 'C','A', 'B', 'A', 'C', 'D','A', 'C', 'D', 'A', 'D','A', 'C', 'B', 'D', 'A' };
TestGrader driver;
driver.setCorrect_answers(correct_ans);
cout << "------------ User # 1 ------------- ";
driver.grade(user1_ans);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.