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.
#include <stdio.h>
#include <string.h>
int main()
{
char names[50][26] = {0}; // 25 characters and null
character
int marks[50][5] = {0};
int n, i, j;
int average[5] = { 0,0,0,0,0 };
printf("Enter No. of Students (max 50) : ");
scanf("%d", &n);
if (n < 1 || n > 50)
{
printf("Incorrect Entry");
return 0;
}
for (i = 0; i < n; ++i)
{
char temp[26] = {0};
printf("Enter Name of Student %d :
",i+1);
scanf("%s", temp);
strcpy(names[i], temp);
for (j = 0; j < 5; ++j)
{
printf_s("Enter
marks of Student %d in subject %d : ", i + 1, j+1);
scanf_s("%d",
&marks[i][j]);
average[j] +=
marks[i][j];
}
}
i = 5;
while (i--)
average[i] /= n;
printf("Student\tPRG\tDGS\tMTH\tECR\tGED\n");
for (i = 0; i < n; ++i)
{
printf("%s\t", names[i]);
for (j = 0; j < 5;
++j)
printf("%d\t",
marks[i][j]);
printf("\n");
}
printf("AVERAGE\t");
for ( j = 0; j < 5; ++j)
printf("%d\t", average[j]);
return 0;
}