1 Overview An exam has 2 sections (named Section A and Section B) each containin
ID: 3847860 • Letter: 1
Question
1 Overview
An exam has 2 sections (named Section A and Section B) each containing 4 questions worth 20 points each. Students must answer 5 questions in total with at least 2 from Section A and 2 from Section B. If more questions than required are answered, then the first ones are counted and the later ones are disregarded. Unanswered questions are indicated by a grade of 0.
Write a C program to read in each student’s id number (a 4 digit value) followed by 8 scores (with each score being in the range 0–20 inclusive). An id number of 0 indicates the end of input. You may assume the correct number of input values are entered; however, if any score is out-of-range you are to replace it in your calculations (and the output table) with a 0. Here is some sample input data:
Input to your program must come from standard input via redirection on the command line (as discussed in class; see the sample run below). You will echo print each student’s id and scores along with printing out their result and all appropriate comments. Output, including all labels and spacings, must appear exactly as it appears in the sample run below. Depending on the data entered your program should also print out 0 or more of the following comments in the order and manner that is shown in the sample run:
You also should report the total number of students who took the exam along with the average result.
Terminal tcsh 80x22 liberty N/tmp/96 cat exams. dat 1234 10 15 0 0 20 8 17 0 3200 10 9 7 20 24 0 10 9122 5 6 10 0 19 5 3 14 6022 0 0 0 20 0 15 0 6 1077 20 18 0 0 20 19 16 0 liberty tmp/96 gcc proj4.c liberty tmp/96 a out exams .dat Proj #4 D. Resler id Section A Section B Result Comments 1234 10 15 0 0 20 8 17 0 70 3200 10 9 7 20 0 0 0 10 36 Score incorrect. Too many from A. 9122 5 6 10 0 19 5 3 14 45 More than 5. Too many from B 6022 0 0 0 20 0 15 0 6 41 Less than 5 1077 20 18 0 0 20 19 16 0 93 Total students: 5 Average result: 57.0 liberty tmp IExplanation / Answer
//C program for given problem statement.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int id;
int arr[8],i,t;
int total=0;
int number=0;
printf(" id Section A Section B Result Comments ");
printf(" ----- -------------- -------------- ------- --------- ");
while(1)
{
scanf("%d",&id);
if(id==0) break;
for(i=0;i<8;i++)
{
scanf("%d",&arr[i]);
if(arr[i]>20 || arr[i]<0) //check the score is valid or not
arr[i]=0;
}
int sum=0;
int count=0;
int countb=0;
//For Section A
for(i=0;i<4;i++)
{
if(arr[i]!=0) //adding to sum if not equal to 0
{
count++;
if(count<=3) //only add if count is less than 3 as given condition.
sum+=arr[i];
}
}
//Similarly for Section B
for(i=4;i<8;i++)
{
if(arr[i]!=0)
{
countb++;
if(count>=3)
t=2;
else
t=3;
if(countb<=t)
sum+=arr[i];
}
}
//Displaying results
printf(" %d ",id);
for(i=0;i<8;i++)
{
printf("%d ",arr[i]);
if(i==3)printf(" ");
}
printf(" %d ",sum);
if(count>3)
printf("Incorrect answer.Too many from A. ");
else if(count<2)
printf("Incorrect answer.Too less from A. ");
else if(countb>t)
printf("Incorrect answer.Too many from B. ");
else if(countb<t)
printf("Incorrect answer.Too less from A ");
else
printf(" ");
total+=sum;
number++;
}
printf("Total students: %d ",number);
printf("Average Result: %d ",total/number);
return 0;
}
Output :
G580:~/codes/schegg$ gcc student.c
G580:~/codes/schegg$ ./a.out < exam.dat
id Section A Section B Result Comments
----- -------------- -------------- ------- ---------
1234 10 15 0 0 20 8 17 0 70
3200 10 9 7 20 0 0 0 10 36 Incorrect answer.Too many from A.
9122 5 6 10 0 19 5 3 14 45 Incorrect answer.Too many from B.
6022 0 0 0 20 0 15 0 6 41 Incorrect answer.Too less from A
1077 20 18 0 0 20 19 16 0 93
Total students: 5
Average Result: 57
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.