In: Computer Science
Write a program in C to grade an n-question multiple-choice exam (for n between 5 and 20) and provide feedback about the most frequently missed questions. Your program will use a function scan_exam_data to scan and store the exam data in an appropriate data structurer. Use file redirection to input data – do not use fopen() in your code. The first line of input contains the number of students. Second line is number of questions on the exam followed by a space and then an n-character string of the correct answers. Each of the lines that follow contain an integer student ID followed by a space and then the student answers.
Sample Input
3
5 dbbac
111 dabac
102 dcbdc
251 dbbac
Your program then uses a function analyze_data to calculate each
student’s score as a percentage and the number of each missed
questions in the exam. A function print_data will be called to
produce an output, containing the answer key, each student’s ID,
each student’s score as a percentage, and information about how
many students missed each question.
Sample Output
Exam Report
Students 3
Question 1 2 3 4 5
Answer d b b a c
ID Score(%)
111 80
102 60
251 100
Question 1 2 3 4 5
Missed by 0 2 0 1 0
Error checking
You will have to perform some error checks while the program is
being running. However, these error checks will not cause the
program to terminate – except if you have a corrupted file. Your
program should print error messages when appropriate. Here is a
list that need to be checked: Illegal inputs like the number of
questions must be between 5-20 and key answers must be lower-case
letters.
If you have any doubts, please give me comment...
#include<stdio.h>
void scan_exam_data(int stud_ids[], char stud_answers[][20], int no_students);
void analyze_data(char correct_ans[], char stud_answers[][20], int no_students, int no_questions, int perc[], int misses[]);
void print_data(int stu_ids[], char correct_ans[], int no_studs, int no_ques, int perc[], int misses[]);
int main(){
int no_students, no_questions, i;
char correct_answers[21];
scanf("%d", &no_students);
scanf("%d %s", &no_questions, correct_answers);
int stud_ids[no_students];
char stud_answers[no_students][no_questions+1];
int percentage[no_students];
int misses[no_questions];
for(i=0; i<no_students; i++)
stud_ids[i] = 0;
for(i=0; i<no_questions; i++)
misses[i] = 0;
scan_exam_data(stud_ids, stud_answers, no_students);
analyze_data(correct_answers, stud_answers, no_students, no_questions, percentage, misses);
print_data(stud_ids, correct_answers, no_students, no_questions, percentage, misses);
return 0;
}
void scan_exam_data(int stud_ids[], char stud_answers[][20], int no_students){
int i;
for(i=0; i<no_students; i++){
scanf("%d %s", &stud_ids[i], stud_answers[i]);
}
}
void analyze_data(char correct_ans[], char stud_answers[][20], int no_students, int no_questions, int perc[], int misses[]){
int i, j;
for(i=0; i<no_students; i++){
int correct = 0;
for(j=0; j<no_questions; j++){
if(correct_ans[j]==stud_answers[i][j])
correct++;
else
misses[j]++;
}
perc[i] = ((double)correct/no_questions)*100;
}
}
void print_data(int stu_ids[], char correct_ans[], int no_studs, int no_ques, int perc[], int misses[]){
int i;
printf("Exam Report\n");
printf("Students %d\n", no_studs);
printf("Question");
for(i=1; i<=no_ques; i++)
printf(" %d", i);
printf("\nAnswer");
for(i=0; i<no_ques; i++)
printf(" %c", correct_ans[i]);
printf("\n\nID\tScore(%%)\n");
for(i=0; i<no_studs; i++){
printf("%d %d\n", stu_ids[i], perc[i]);
}
printf("\nQuestion");
for(i=1; i<=no_ques; i++)
printf(" %d", i);
printf("\nMissed by");
for(i=0; i<no_ques; i++)
printf(" %d", misses[i]);
printf("\n");
}