In: Computer Science
Write C program
Multidimensional Arrays
Design a program which uses two two-dimensional arrays as follows:
The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that is requested by the user which is greater than 50. The program will compute the average mark for each course and then display all students and their marks, as well as the average mark for each course.
A sample output produced by the program is shown below, if assumed that the user entered marks for 4 students. Please note that the computation of the average mark for each course should use type casting.
Student PRG DGS MTH ECR GED
Ann Smart 93 85 87 83 90
Mike Lazy 65 57 61 58 68
Yo Yo 78 65 69 72 75
Ma Ma 84 79 83 81 83
AVERAGE 80.0 71.5 75.0 73.5 79.0
It is required to submit your source code file, i.e. Lab5.c file as well as a file with your program's run screen captures
Code:
#include <stdio.h>
int main()
{
int number_of_students;
// taking input from user
printf("Enter the number of students\n");
scanf("%d",&number_of_students);
// creating a two dimensional array for 50 students where a name is up to 25 characters long
char names[50][25];
// creating a two dimensional array which can store marks for 5 courses for up to 50 students
int marks[50][5];
int i, j;
// taking input from user
for(i=0; i<number_of_students; i++) {
printf("Enter the name of student %d\n",i+1);
scanf(" %[^\n]",names[i]);
printf("Enter the marks of the student %s\n",names[i]);
for(j=0;j<5;j++){
scanf("%d",&marks[i][j]);
}
}
// printing the output
printf("%-25s%-7s%-7s%-7s%-7s%-7s\n","Student","PRG","DGS","MTH","ECR","GED");
for(i=0; i<number_of_students; i++) {
printf("%-25s",names[i]);
for(j=0;j<5;j++){
printf("%-7d",marks[i][j]);
}
printf("\n") ;
}
printf("%-25s","AVERAGE");
j=0;
// calculating the average of the marks for each course
while(j<5){
int sum =0;
double avg=0;
for(int i=0;i<number_of_students;i++){
sum=sum+marks[i][j];
}
// type casting the average
avg=(double)sum/number_of_students;
printf("%.1f%-3s",avg," ");
j++;
}
return 0;
}
Screenshot of the code:
Screenshot of the output: