A multiple-choice quiz has a total of 10 questions. Each question can either be:
ID: 3814053 • 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 ADBCXCXECA
There is no limit to how many students there are. A line containing a “studentID” 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 Number: 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. Use appropriate variable names (example: int correct=0, wrong=0, noattempted=0, char answers[], char studentanswers[], etc.)
Explanation / Answer
So as i understand it . You read the data from a file whose first line gives you 10 characters which are also the answers to the 10 questions in the quiz . Later lines give info about individual student.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
int studentId;
char answers[10];
int score;
}student;
student* allocate(student* , int size);
int main()
{
student* student_info = NULL;
FILE* fp = NULL;
int i = 0, size = 100, count = 0, id = -1, score = 0, correctAnswersCount[10] = {0};
char ch = ' ',data[50] = {''}, answers[10] = {''};
fp = fopen("input.txt","r");
if(fp == NULL){
fprintf(stderr,"Unable to open file ");
return -1;
}
student_info = allocate(student_info,size);
fscanf(fp,"%[^ ]",data);
strncpy(answers,data,10);
//Read in the ' ' character.
ch = fgetc(fp);
while(1)
{
fscanf(fp,"%[^ ]",data);
//Read in the ' ' character.
ch = fgetc(fp);
student_info[count].studentId = atoi(strtok(data," "));
if( student_info[count].studentId == 0)
break;
strncpy(student_info[count].answers,strtok(NULL," "),10);
score = 0;
for(i=0; i< 10;i++){
if( student_info[count].answers[i] == 'X')
continue;
else if( student_info[count].answers[i] == answers[i] ){
score += 5;
correctAnswersCount[i]++;
}
else{
score -= 2;
}
}
student_info[count].score = score;
count++;
if(count == size){
size += 10;
allocate(student_info, size);
}
}
for(i=0; i< count ;i++){
printf("Student Id: %d , score: %4d ",student_info[i].studentId, student_info[i].score);
}
printf("Total number of students : %d ",count);
printf("Question Number : 1 2 3 4 5 6 7 8 9 10 ");
printf("Number correct: ");
for(i=0;i<10;i++)
printf("%d ",correctAnswersCount[i]);
printf(" ");
fclose(fp);
free(student_info);
return 0;
}
student* allocate(student* s, int size)
{
if(s == NULL){
s = (student*)malloc(size*sizeof(student));
}
else{
s = (student*)realloc(s, size * sizeof(student));
}
return s;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.