A multiple-choice quiz has a total of 15 questions. Each question can either be:
ID: 3817006 • Letter: A
Question
A multiple-choice quiz has a total of 15 questions. Each question can either be: A, B, C, D, E (five choices). All data for the quiz is stored in a file quiz.txt.
The first line of data contains the correct answers to the 15 questions in the first 15 consecutive character positions
For Example: ABBCBBEDCEDBCCA
Each line in the file contains the answers for an individual student. Data on a line consists of a student ID (integer), then by a space, followed by the 15 answers given by the student in the next 15 consecutive character positions. An X is used if the student did not answer one of the questions.
For example: 1225 ADBCXCXECAXABCA
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 15 questions individually(example:
Question Number: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Number correct: 5 8 2 9 8 5 3 2 8 4 9 4 6 7 9
Hint: For loop's and three array's must be used. Basic/Simple program for beginners.
Use appropriate variable names (example: int correct=0, wrong=0, noattempted=0, char answers[16], char studentanswers[16], char correctnumberanswers[15], etc.)
.
Sample data in quiz.txt
ABEDEEBCEABBCCA
4564 ABEXXBXECDDCBAA
2216 ABEDEEBXXEDEACA
8532 BBEEEBXECDDCBAA
5829 ABEEABEECDDCBAA
7721 CBEXBBXECDDCBXX
0
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main() {
FILE *fp=fopen("quiz.txt","r");
if(fp==NULL) {
cout<<"Wrong filename";
return 0;
}
int f=0, id, cnt=0;
char ans[20], std_ans[20];
int *std_id, *correct, *wrong, *unattempt;
while(true) {
if(f==0) {
f=1;
fscanf(fp,"%s",ans);
if(strcmp(ans,"0")==0) break;
} else {
fscanf(fp,"%d %s",&id,std_ans);
if(id==0) break;
cnt++;
}
}
int std_points[cnt], qc[16]={0};
if(cnt>0) {
std_id=(int*)malloc(sizeof(int)*cnt);
correct=(int*)malloc(sizeof(int)*cnt);
wrong=(int*)malloc(sizeof(int)*cnt);
unattempt=(int*)malloc(sizeof(int)*cnt);
fp=fopen("quiz.txt","r");
f=0; cnt=0;
while(true) {
if(f==0) {
f=1;
fscanf(fp,"%s",ans);
if(strcmp(ans,"0")==0) break;
} else {
fscanf(fp,"%d %s",&id,std_ans);
if(id==0) break;
std_id[cnt]=id;
int t=0, corr_count=0, wrong_count=0, unattemp_count=0;
for(int i=0;i<15;++i) {
if(std_ans[i]=='X') unattemp_count++;
else if(std_ans[i]==ans[i]) t+=5, corr_count++, qc[i]+=1;
else t-=2, wrong_count++;
}
std_points[cnt]=t;
correct[cnt]=corr_count;
wrong[cnt]=wrong_count;
unattempt[cnt]=unattemp_count;
cnt++;
}
}
for(int i=0;i<cnt;++i) {
printf("Student id and marks obtained in exam %d --> %d ", std_id[i],std_points[i]);
}
printf("Total students appeared for exam %d ",cnt);
printf("Question number:");
for(int i=0;i<15;++i) {
printf(" %d",(i+1));
}
printf(" ");
printf("Number correct:");
for(int i=0;i<15;++i) {
printf(" %d",qc[i]);
}
printf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.