You are to write a C program that grades a true-false quiz. The quiz data will b
ID: 3564928 • Letter: Y
Question
You are to write a C program that grades a true-false quiz. The quiz data will be available in a text file: here is an example:
Quiz #8 (09/22/14) (corrected version)
TTFTTFTFTF
0080 TTFTTFTFTF
0340 TTFTFFTFTF
1522 TTFFFFTFTF
9001 TFFTTTTTTF
0012 FFTFTFTFTF
9013 TTFTTTFTFT
0913 TTTFFTFTTF
6155 FFTTTTTTFT
3144 FFTTFTTTFT
4400 FFTFFTFTFF
0
The first line in the data file is a comment that you may assume can be up to 80 characters long; it cannot be blank. The second line contains the answer key for the 10-question true-false quiz. The following lines in the data file contain a sutdent's id number in column 1 followed by their answers for the quiz in column 2. A 0 (zero) on a line by itself indicates that there are no more students to process.
Write a program that first reads in the comment line and answer key as strings followed by each student's data. Store the students id numbers in an array. You are to 'grade' each student's quiz using the key provided followed by recording their scores (in a second array) in order to be able to print them out later. You do not need to use an array of strings or characters in your program. Write your program allowing for up to 100 students, and you may assume that at least 1 student will have taken the quiz.
You should create test data text files and provide the data to your program using redirection
from the command line (see the sample run below). Your program should output the number
of students who took the quiz, the average score, the best score, and a table showing each
student
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file ");//take file name as input
gets(file_name);
fp = fopen(file_name,"r"); // open file in read mode
if( fp == NULL )
{
perror("Error while opening the file. ");
exit(EXIT_FAILURE);
}
int i,j=0,k,f=0;
char table[100][16];
int score[100]={0};
while(1)
{
//loop to scan comment
if(f==0)
while( ( ch = fgetc(fp) ) != ' ' )
f=1;
char ans[10];
i=0;
//loop to scan answer key
if(f==1)
{
while((ch=fgetc(fp))!=' ')
ans[i++]=ch;
f=2;
}
i=0;
//loop to scan file and get student ID and there answers
if(f==2)
{
while((ch=fgetc(fp))!=' ' && ch!=EOF)
{
table[j][i++]=ch;
// printf("%c",ch);
}
if(i==1)
break;
for(k=5;k<15;k++)
if(table[j][k]==ans[k-5])
score[j]++;
j++;
}
}
//highest score is stored in max
int max=-1,avg=0;
printf("Total students who took quiz: %d ",j);
for (i=0;i<j;i++)
{
if(max<score[i])
max=score[i];
avg+=score[i];
}
printf("Average score: %d ",avg/j);
printf("Best score: %d ",max);
printf("Student ID : Score : Grade ");
for (i=0;i<j;i++)
{
for (k=0;k<4;k++)
putchar(table[i][k]);
printf(" : %d : ",score[i]);
if(score[i]==max || score[i]==max-1)
printf("A ");
else if(score[i]==max-2)
printf("B ");
else if(score[i]==max-3 || score[i]==max-4)
printf("C ");
else if(score[i]==max-5)
printf("D ");
else
printf("F ");
}
fclose(fp);
return 0;
}
This is output of my programe and 'in' is my input file.
Enter the name of file
in
Total students who took quiz: 10
Average score: 5
Best score: 10
Student ID : Score : Grade
0080 : 10 : A
0340 : 9 : A
1522 : 8 : B
9001 : 7 : C
0012 : 6 : C
9013 : 5 : D
0913 : 4 : F
6155 : 3 : F
3144 : 2 : F
4400 : 1 : F
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.