Loading your workspace…
Loading your workspace…
Store each student's name and marks in a structure, compute the class average, and list the students who score above and below it.
#include <stdio.h>
#define MAX_STUDENTS 50
struct Student {
char name[30];
float marks;
};
int main(void) {
struct Student list[MAX_STUDENTS];
int n, i, count_above, count_below;
float total = 0.0f, average;
printf("Enter number of students (1-%d):\n", MAX_STUDENTS);
if (scanf("%d", &n) != 1 || n < 1 || n > MAX_STUDENTS) {
printf("Error: invalid number of students\n");
return 1;
}
for (i = 0; i < n; i++) {
printf("Student %d name and marks:\n", i + 1);
scanf("%29s %f", list[i].name, &list[i].marks);
total += list[i].marks;
}
average = total / n;
printf("Average marks: %.2f\n", average);
count_above = 0;
printf("Students above average:\n");
for (i = 0; i < n; i++) {
if (list[i].marks > average) {
printf(" %s - %.2f\n", list[i].name, list[i].marks);
count_above++;
}
}
if (count_above == 0) {
printf(" None\n");
}
count_below = 0;
printf("Students below average:\n");
for (i = 0; i < n; i++) {
if (list[i].marks < average) {
printf(" %s - %.2f\n", list[i].name, list[i].marks);
count_below++;
}
}
if (count_below == 0) {
printf(" None\n");
}
return 0;
}
Input
5
Asha 78
Ravi 65
Meena 90
Kiran 65
Divya 82Output
Enter number of students (1-50):
Student 1 name and marks:
Student 2 name and marks:
Student 3 name and marks:
Student 4 name and marks:
Student 5 name and marks:
Average marks: 76.00
Students above average:
Asha - 78.00
Meena - 90.00
Divya - 82.00
Students below average:
Ravi - 65.00
Kiran - 65.00Input
2
Anu 50
Bala 50Output
Enter number of students (1-50):
Student 1 name and marks:
Student 2 name and marks:
Average marks: 50.00
Students above average:
None
Students below average:
NoneAn array of struct Student keeps each student's name and marks together, so the average and the two lists are computed by walking one array instead of parallel arrays that can drift out of step.
The total is accumulated in a float and divided once at the end. A student whose marks equal the average is excluded from both lists, so the program never reports someone as both above and below.
Edit the program and press Run. It is free and needs no sign-in.
Copy a sample and paste it into the STDIN box in the editor before you press Run.
5 Asha 78 Ravi 65 Meena 90 Kiran 65 Divya 82
Expected output
Enter number of students (1-50): Student 1 name and marks: Student 2 name and marks: Student 3 name and marks: Student 4 name and marks: Student 5 name and marks: Average marks: 76.00 Students above average: Asha - 78.00 Meena - 90.00 Divya - 82.00 Students below average: Ravi - 65.00 Kiran - 65.00
2 Anu 50 Bala 50
Expected output
Enter number of students (1-50): Student 1 name and marks: Student 2 name and marks: Average marks: 50.00 Students above average: None Students below average: None
An array holds many values of one type; a structure groups fields of different types under one name.
With the dot operator, as list[i].marks.
Only the first n entries hold data, so dividing by the number of students gives the correct average.
It stops the read from overflowing the 30-character name field.
Each list has its own heading and its own 'None' message, so printing them separately keeps the output clear.
Back to all VTU BPOPS103 lab programs.