In: Computer Science
Multidimensional Arrays
Design a C program which uses two two-dimensional arrays as
follows:
- an array which can store up to 50 student names where a name is
up to 25 characters long
- an array which can store marks for 5 courses for up to 50
students
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.
Below is code in C language
#include <stdio.h>
int main()
{
int count,i,j;
double avg1=0, avg2=0, avg3=0, avg4=0, avg5=0;
printf("Enter total number of students: ");
scanf("%d", &count);
if(count > 50)
{
printf("Sorry you can not enter marks for more than 50 students");
}
char students_name[50][25];
int students_marks[50][5];
//Take students name and 5 subjects marks
for(i=0; i<count; i++)
{
printf("Enter student: %d name: ", i+1);
getchar();
scanf("%[^\n]s", students_name[i]);
printf("Enter 5 subject marks for student %d: ", i+1);
for(j=0; j<5; j++)
{
scanf("%d", &students_marks[i][j]);
}
}
//Calculate subject vise average
for(i=0; i<count; i++)
{
avg1 += students_marks[i][0];
avg2 += students_marks[i][1];
avg3 += students_marks[i][2];
avg4 += students_marks[i][3];
avg5 += students_marks[i][4];
}
//Type cast average
avg1 = (double) avg1/count;
avg2 = (double) avg2/count;
avg3 = (double) avg3/count;
avg4 = (double) avg4/count;
avg5 = (double) avg5/count;
//print students name, marks and subject vise average mark
printf("Student PRG DGS MTH ECR GED");
for(i=0; i<count; i++)
{
printf("\n%s %d %d %d %d %d",students_name[i],students_marks[i][0],students_marks[i][1],students_marks[i][2],students_marks[i][3],students_marks[i][4]);
}
printf("\nAVERAGE %0.1f %0.1f %0.1f %0.1f %0.1f",avg1,avg2,avg3,avg4,avg5);
return 0;
}
Output: