In certain applications, arrays need initial values and can be defined globally or as static local arrays. The provided example demonstrates summing marks from two subjects for three students and calculating their averages.
Example:
/* Program to display the average marks of 3 students */
#include <stdio.h>
#define SIZE 3
main() {
int i;
float stud_marks1[SIZE], stud_marks2[SIZE], total_marks[SIZE], avg[SIZE];
// Input marks for subject 1
printf("\n Enter the marks in subject-1 out of 50 marks: \n");
for(i = 0; i < SIZE; i++) {
printf("Student no. =%d Enter the marks= ", i+1);
scanf("%f", &stud_marks1[i]);
}
// Input marks for subject 2
printf("\n Enter the marks in subject-2 out of 50 marks \n");
for(i = 0; i < SIZE; i++) {
printf("Student no. =%d Please enter the marks= ", i+1);
scanf("%f", &stud_marks2[i]);
}
// Calculate and display averages
for(i = 0; i < SIZE; i++) {
total_marks[i] = stud_marks1[i] + stud_marks2[i];
avg[i] = total_marks[i] / 2;
printf("Student no.=%d, Average= %f\n", i+1, avg[i]);
}
}
O/p:
Enter the marks in subject-1 out of 50 marks:
Student no. = 1 Enter the marks= 23
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 42
Enter the marks in subject-2 out of 50 marks:
Student no. = 1 Enter the marks= 31
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 40
Student no. = 1 Average= 27.000000
Student no. = 2 Average= 35.000000
Student no. = 3 Average= 41.000000