The local Driver’s License Office has asked you to write a program that grades t
ID: 3757566 • Letter: T
Question
The local Driver’s License Office has asked you to write a program that grades the written portion of the driver’s license exam. The exam has 20 multiple choice questions. Here are the correct answers:
1. B 4. A 7. B 10. D 13. D 16. C 19. D
2. D 5. C 8. A 11. B 14. A 17. C 20. A
3. A 6. A. 9. C 12. C 15. D 18. B
A student must correctly answer 15 of the 20 questions to pass the exam.
Write a class named DriverExam that holds the correct answers to the exam in an array field.
The class should also have an array field that holds the student’s answers. The class should have the following methods:
passed. Returns true if the student passed the exam, or false if the student failed
totalCorrect. Returns the total number of correctly answered questions
totalIncorrect. Returns the total number of incorrectly answered questions
questionsMissed. An int array containing the question numbers of the questions that
the student missed
Demonstrate the class in a complete program that asks the user to enter a student’s answers,
and then displays the results returned from the DriverExam class’s methods.Input Validation: Only accept the letters A, B, C, or D as answers.
Explanation / Answer
#include <iostream>
using namespace std;
class DriverExam
{
char ans[20]={'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
char input[20];
public:
DriverExam(char in[])
{
for(int i=0;i<20;i++)
input[i]=in[i];
}
bool passed()
{
if(totalCorrect()>15)
return true;
return false;
}
int totalCorrect()
{
int c=0;
for(int i=0;i<20;i++)
if(ans[i]==input[i])
c++;
return c;
}
int totalIncorrect()
{
return 20-totalCorrect();
}
void questionsMissed()
{
int n=20-totalCorrect();
int i,j;
j=0;
int arr[n];
for(i=0;i<20;i++)
{
if(ans[i]!=input[i])
arr[j++]=i+1;
}
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};
int main() {
char input[20],ch;
int i;
cout<<"Enter answers (A or B or C or D) "<<endl;
for(i=0;i<20;i++)
{
cin>>ch;
if(ch=='A' || ch=='B' || ch=='C' || ch=='D')
input[i]=ch;
else
cout<<"Invalid Input, try again"<<endl;
}
DriverExam ob(input);
cout<<"Correct Answers: "<<ob.totalCorrect()<<endl;
cout<<"Inorrect Answers: "<<ob.totalIncorrect()<<endl;
if(ob.passed())
cout<<"Passed"<<endl;
else
cout<<"Failed"<<endl;
cout<<"Questions Missed are: "<<endl;
ob.questionsMissed();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.