A multiple-choice quiz has a total of 10 questions. Each question can either be:
ID: 3812662 • Letter: A
Question
A multiple-choice quiz has a total of 10 questions. Each question can either be: A, B, C, D, E (five choices)
The first line of data contains the correct answers to the ten questions in the first 10 consecutive character positions.
For Example: ABBCBBEDCE
Each line in the file contains the answers for the student. Data on a line consists of a student ID (integer), then two spaces, followed by the ten answers given by the student in the next ten consecutive character positions. You must use an X if the student did not answer one of the questions.
For example: 1225 ADBCXCXEDA
There is no limit to how many students there are. A line containing a “student ID” 0 indicates the end of the data. A student’s overall total score is calculated by adding up points
Points for a question are calculated as shown: Correct answer = 5 points, wrong answer = -2 points, no answer = 0 points
.
Write a C program that prints:
1. Each student ID and there overall total score
2. The total number of students
3. The number of correct responses to each of the ten questions individually (example:
Question: 1 2 3 4 5 6 7 8 9 10
Number correct: 5 8 2 9 8 5 3 2 8 4
A loop and array must be used.
Explanation / Answer
C code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *ptr_file;
char buf[1000];
char ans[12];
ptr_file =fopen("input.txt","r");
if (!ptr_file)
return 1;
int n = 0;
int tmp[10];
for (int i = 0; i < 10; ++i)
{
tmp[i] = 0;
}
fgets(ans,1000, ptr_file);
while (fgets(buf,1000, ptr_file)!=NULL)
{
if(buf[0] != '0')
{
n++;
char *ID[50];
int i = 0;
for (i = 0; i < 50; ++i)
{
if(buf[i] == ' ' || buf[i] == ' ')
{
ID[i] = '';
i = i+2;
break;
}
else
{
ID[i] = buf[i];
}
}
int score = 0;
int x = 0;
for (int j = i; j < i + 10; ++j)
{
if(buf[j] == 'X')
{
}
else if(buf[j] == ans[x])
{
score = score + 5;
tmp[x] = tmp[x] + 1;
}
else
{
score = score - 2;
}
x++;
}
for (int z = 0; z < i-1; ++z)
{
printf("%c", ID[z] );
}
printf("%i ", score );
}
}
printf("Total number of students = %i ", n );
for (int i = 0; i < 10; ++i)
{
printf("%i ", i+1 );
}
printf(" ");
for (int i = 0; i < 10; ++i)
{
printf("%i ", tmp[i] );
}
fclose(ptr_file);
return 0;
}
Sample input.txt
ABBCBBEDCE
101 ABBCBBEDCX
102 ABBCBBEDCD
103 ABBCBBEDEX
0
Sample Output:
101 45
102 43
103 38
Total number of students = 3
1 2 3 4 5 6 7 8 9 10
3 3 3 3 3 3 3 3 2 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.