Assignment #3 Objective: Use of Nested Loops Data Validation Input: You are to e
ID: 3631591 • Letter: A
Question
Assignment #3Objective: Use of Nested Loops
Data Validation
Input: You are to enter Data for 4 classes of students from
the keyboard:
1. Each class has a varying number of students
2. Each students’ data consists of a first and last initials and
1 exam score (0..100)*
3. There should be at least 5 students per class
*4. At LEAST 1 student per class must have an
invalid score (out of Range)
Processing: Calculate AND/OR find
1. Average for each class
2. Average across all classes
3. Highest class average & what class it was
4. Highest student score in each class
5. Highest student scoreacross all classes
Output: 1. A. Average for each class
B. Average for all classes
C. Highest class average & which class had it
D. Highest student score& who had it, for each class
E. Highest student score & who had it, across all classes
2. Data found in error should be output including:
A. Score in error, initials of student & class they are in.
Explanation / Answer
#include<stdio.h>
void main()
{
char Names[20],highName[20],lowName[20];
int score,high,low,highest[4],lowest[4];
float avera[4];
int i,j;
for(int i=0;i<4;i++)
{
printf("Class:%d",i);
int sum=0;
for(j=0;j<5;j++)
{
printf("Enter name:");
scanf("%s",Names);
printf("Enter score:");
scanf("%d",&score);
if(j==0)
{
strcpy(highName,Names);
strcpy(lowName,Names);
high=score;
low=score;
}
else
{
if(score>high)
{
high=score;
strcpy(highName,Names);
}
if(score<low)
{
low=score;
strcpy(lowName,Names);
}
}
sum=sum+score;
}
highest[i]=high;
lowest[i]=low;
avera[i]=(float)sum/5;
}
float highavg=avera[0];
int highAll=highest[0],h=0,hav=0;
printf("Average for each class");
printf("Class:1 %f",avera[0]);
float avgavg=avera[0];
for(int i=1;i<4;i++)
{
avgavg=avera[i];
if(highest[i]>highAll)
{h=i; highAll=highest[i];}
if(avera[i]>highavg)
{ highavg=avera[i];hav=i;}
printf("Class:%d %f",i+1,avera[0]);
}
printf("Average across all classes:%f",avgavg/4);
printf(" Highest student score in each class");
for(int i=0;i<4;i++)
printf("class 1:%d %d",i+1,highest[i]);
printf("Highest class average%f",highavg);
printf("class:%d",hav);
getch();
}