Write a program to grade an n-question multiple-choice exam (for n between 5 and
ID: 3778020 • Letter: W
Question
Write a program to grade an n-question multiple-choice exam (for n between 5 and 50) and provide feedback about the most frequently missed questions. Your program will take data from the file examdat.txt. The first line of the file contains the number of questions on the exam followed by a space and then an n-character string of the correct answers. Write a function fgetAnswers that inputs the answers from an open input file. Each of the lines that follow contain an integer student ID followed by a space and then that student's answers. Function fgetAnswers can also be called to input a student's answers. Your program is to produce an output file, report.txt, containing the answer key, each student's ID and each student's score as a percentage, and then information about how many students missed each question. Here are short sample input and output files.Explanation / Answer
/*
C-File that reads a text file examdat.txt that contains answers and student
id and answers of students . The c file creats the text file called report.txt
that contais the student id and percentage of scores and answers missed by students*/
//examscores.c
//include header files
#include <stdio.h>
#include <conio.h>
//function prototypes
void fgetAnswers(FILE*,int*,char[]);
void fgetAnswers2(FILE*,int*,int,char[]);
//main function
int main()
{
char ans[50],choices[50];
int n,i,id,missed[50],tot;
FILE *input,*output;
//open input file for reading
input = fopen("examdat.txt","r");
//check if file exists not
if(!input)
{
printf("Error opening input file! ");
getch();
return 0;
}
fgetAnswers(input,&n,ans);
output = fopen("report.txt","w");
fprintf(output," Exam Report ");
fprintf(output,"%-15s"," Question");
for(i=1;i<=n;i++)
fprintf(output,"%3d",i);
for(i=0;i<n;i++)
missed[i]=0;
fprintf(output,"%-15s"," Answer ");
for(i=0;i<n;i++)
fprintf(output,"%3c",ans[i]);
fprintf(output," ID Score(%%) ");
do
{
fgetAnswers2(input,&id,n,choices);
tot=0;
if(id>=0)
{
for(i=0;i<n;i++)
{
//count for correct answers
if(choices[i]==ans[i])
tot++;
else
//otherwise count by the missed array
missed[i]++;
}
//calculate percentage
float fPercentage = (float(tot)/n) * 100.0;
//write id and peropercenatge
fprintf(output,"%3d %3.0f ",id,fPercentage);
}
}while(id>=0);
//write questions missed count
fprintf(output,"%-15s"," Question ");
for(i=1;i<=n;i++)
fprintf(output,"%3d",i);
fprintf(output,"%-15s"," Missed by ");
for(i=0;i<n;i++)
fprintf(output,"%3d",missed[i]);
//close file points
fclose(input);
fclose(output);
return 0;
}
/**
The function that takes FILE object reference , and a pointer , scores m and ans array of type char
*/
void fgetAnswers2(FILE* input,int *n,int m,char ans[])
{
int i;
fscanf(input,"%d",n);
if ( feof(input) )
{
*n=-1;
return;
}
fgetc(input);
//read answers using fgetc for m=5 scores
for(i=0;i<m;i++)
ans[i]=fgetc(input);
}
/**
The function fgetAnswers that takes FILE pointer , pointer n and ans array
*/
void fgetAnswers(FILE* input,int *n,char ans[])
{
int i;
fscanf(input,"%d",n);
fgetc(input);
for(i=0;i<*n;i++)
ans[i]=fgetc(input);
}
-------------------------------------------------------
File : "examdat.txt"
5 dbbac
111 dabac
102 dcbdc
251 dbbac
------------------------------------------------
report.txt
Exam Report
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.