Use C++ to solve this program: The local Driver\'s License Office has asked you
ID: 3706993 • Letter: U
Question
Use C++ to solve this program:
The local Driver's License Office has asked you to write aprogram that grades the written portion of driver's license exam.The exam has 20 multiple choice questions. Here are the correct answers:
1. B 6.A 11.B 16. C
2. D 7.B 12.C 17. C
3. A 8.A 13.D 18. B
4. A 9.C 14.A 19. D
5. C 10.D 15.D 20. A
Your program should store the correct answers shown above in an array. It should ask the user to enter the student's answers foreach of the 20 questions, and the answers should be stored in another array. After the student's answers have been entered, theprogram should display a message indicating whether the studentpassed or failed the exam. (A student must correctly answer 15 ofthe 20 questions to pass the exam.)
It should then display the total number of correctly answeredquestions, the total number of incorrectly answered questions, anda list showing the questions numbers of the incorrectly answeredquestions.
Input Validations: Only accept the letters A, B, C, or Das answers.
Additional requirement(s):
Student answers must be randomly generated by your code not asked from the user to manually type them in.
Student answers must be different every time the user runs the program.
Explanation / Answer
C++ CODE:
#include <stdio.h>// From the library
#include <stdlib.h>//from the library
int main()//instruction main
{
FILE *fp = fopen("textdata.txt","r");
char answers[20];
char c;
char correctAnswers[20] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
int result[20];// representing the result
int correct=0, incorrect=0;
int i =0;
if(fp == NULL){//then the signal is null
printf(" Could not open file. ");
return 0; //finally to return to 0
}
while((answers[i] = (char)fgetc(fp))!= EOF){
c = (char)fgetc(fp);
i++;// incrementing
}
printf(" Incorrect Answers : ");
for(i = 0; i<20; i++){
if(answers[i] == correctAnswers[i]){
result[i] = 1;
correct++;
}
else {
result[i] = 0;
incorrect++;
printf("%d ",i+1 );
}
}
if(correct >= 15){//then the value is correct
printf(" Congratulations ! You have passed test! ");
}
else{
printf(" Score is below 15. You have failed exam. ");
}
return 0; //then value return to
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.