using c++ language. thanks in advance! Problem 1 10 Points The local Driver\'s L
ID: 3712919 • Letter: U
Question
using c++ language. thanks in advance!
Problem 1 10 Points 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 correct answers: 1. A 2. D 3.? 4. B 5. ? 6. B 7. A 8.? 9. ? 10. D 16.C 12.C 13.D 14. ? 15. D 18. A 19. D 20.B Your program should store the correct answers shown above in an array. It should ask the user to enter the student's answers for each of the 20 questions, and the answers should be stored in a separate array. The program must validate user's input and re-prompt as long as it's invalid. After the student's answers have been entered, the program should display a message indicating whether the student passed or failed the exam. A student must correctly answer 15 of the 20 questions to pass the exam. It should then display the percentage the student scored in the exam, the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing each question with the student answer, the correct answer and if the student got it right or wrong. You must use functions to organize your code!Explanation / Answer
#include <iostream> //used for cin, cout
using namespace std;
//Function taking user answer as input
void inputUserAns(char uans[])
{ int i=0;
cout<<"Enter the Answers of 20 questions"<<endl;
while(i<20)
{
cout<<"Answer "<<i+1<<":";
cin>>uans[i];
//Validating user input
if(uans[i]!='A' && uans[i]!='B' && uans[i]!='C' && uans[i]!='D')
{
cout<<"Enter the valid answer"<<endl;
continue;
}
i++;
}
} //End of function
//Function will display the result
void displayResult(char cans[],char uans[])
{ int i,totcorrect=0, totwrong=0;
for(i=0;i<20;i++)
{
if(cans[i]==uans[i])
totcorrect++; //Counting total correct answer
else
totwrong++; //Counting total incorrect answer
}
//Printing the result as Pass or Fail
if(totcorrect>=15)
cout<<"Student passed the exam"<<endl;
else
cout<<"Student failed the exam"<<endl;
cout<<"Percenatge scored by the student:"<<(totcorrect/20.0)*100.0<<endl;
cout<<"Total number of correctly answered questions:"<<totcorrect<<endl;
cout<<"Total number of incorrectly answered questions:"<<totwrong<<endl;
//Printing Question number with student answer, correct answer & status of answer(Right or Wrong)
cout<<"Question-No. "<<"Student-Answer "<<"Correct Answer "<<"Status"<<endl;
cout<<"----------------------------------------------------------------------"<<endl;
for(i=0;i<20;i++)
{
if(cans[i]==uans[i])
cout<<i+1<<"."<<" "<<uans[i]<<" "<<cans[i]<<" "<<"Right-Answer"<<endl;
else
cout<<i+1<<"."<<" "<<uans[i]<<" "<<cans[i]<<" "<<"Wrong-Answer"<<endl;
}
} //End of function
//main() function
int main()
{
//Storing correct answer in array
char correctans[20]={'A','D','B','B','C','B','A','B','C','D','A','C','D','B','D','C','C','A','D','B'};
char userans[20]; // Array to store user's answer
inputUserAns(userans); // Calling function to take user answer as input by passing userans[] array as parameter
displayResult(correctans,userans); // Calling function to display the result
return 1;
} //End of main() function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.