Write a C program that using a 2D array The program should a
Write a C program that using a 2-D array. The program should also prompt the user for five quiz grades for six students. The program will then compute the average score for each student, and the average score, highest score, and the lowest score for each quiz.
Solution
#include <stdio.h>
int main()
{
int scores[6][5];
int i,j;
double average;
int sum = 0;
int highScore, lowScore ;
for(i=0; i<6; i++){
printf(\"Enter the student %d, 5 quiz scores: \",i+1);
for(j=0; j<5;j++){
scanf(\"%d\", &scores[i][j]);
}
}
for(i=0; i<6; i++){
sum = 0;
highScore = scores[i][0], lowScore = scores[i][0];
for(j=0; j<5;j++){
sum = sum + scores[i][j];
if(highScore < scores[i][j]){
highScore = scores[i][j];
}
if(lowScore > scores[i][j]){
lowScore = scores[i][j];
}
}
average = sum/(double)5;
printf(\"Student %d Average Score: %lf Highest Score: %d Lowest Score: %d\ \", i+1,average,highScore,lowScore);
}
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter the student 1, 5 quiz scores: 10 20 30 40 50
Enter the student 2, 5 quiz scores: 20 30 40 50 60
Enter the student 3, 5 quiz scores: 30 40 50 60 70
Enter the student 4, 5 quiz scores: 40 50 60 70 80
Enter the student 5, 5 quiz scores: 50 60 70 80 90
Enter the student 6, 5 quiz scores: 60 70 80 90 100
Student 1 Average Score: 30.000000 Highest Score: 50 Lowest Score: 10
Student 2 Average Score: 40.000000 Highest Score: 60 Lowest Score: 20
Student 3 Average Score: 50.000000 Highest Score: 70 Lowest Score: 30
Student 4 Average Score: 60.000000 Highest Score: 80 Lowest Score: 40
Student 5 Average Score: 70.000000 Highest Score: 90 Lowest Score: 50
Student 6 Average Score: 80.000000 Highest Score: 100 Lowest Score: 60

