Language
C
Compiler
gcc 12.1.0
Options
Warnings
Optimization
C99
-pedantic
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[10];
int rollno;
int marks[3]; //array of 3 subjects marks
int totalMarks;
float percent;
};
void calculate(struct student s[10], int n){
//calculate total marks, &percent using loop
int i,m;
for(i=0;i<n;i++){
s[i].totalMarks=0;
s[i].percent=0.0;
for(m=0;m<3;m++){
s[i].totalMarks += s[i].marks[m];
}
s[i].percent= s[i].totalMarks/3.0;
}
}
void display(struct student s[10], int n){
int i,m;
for(i=0;i<n;i++){
printf("Student's Name: %s\n", s[i].name);
printf("Student's Rollno: %d\n", s[i].rollno);
for(m=0;m<3;m++){
printf("Subject %d Marks: %d\n",m+1,s[i].marks[m]);
}
printf("Total Marks: %d\n",s[i].totalMarks);
printf("Total Percent: %.2f\n", s[i].percent);
printf("\n");
}
}
void sort(struct student s[10], int n){
int i,j,m;
//Sort elements of Array(marks[] ) inside Structure (student) in C
for(int i=0;i<n;i++){
for(int j= i+1; j<n; j++){
//if(s[i].marks[m]> s[j].marks[m]){
if(s[i].percent> s[j].percent){
struct student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
//DISPLAY
for(i=0;i<n;i++){
printf("Student's Name: %s\n", s[i].name);
printf("Student's Rollno: %d\n", s[i].rollno);
for(m=0;m<3;m++)
{
printf("%d ",s[i].marks[m ]);
}
printf("Total Marks: %d\n",s[i].totalMarks);
printf("Total Percent: %.2f\n", s[i].percent);
printf("\n");
}
}
int main(){
struct student s[10]; //minimum of 10 students
int i,n,m;
printf("Enter the number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter student's name: ");
scanf("%s",s[i].name);
printf("Enter student's rollno: ");
scanf("%d",&s[i].rollno);
for(m=0;m<3;m++){
printf("Enter the student's marks: ",m+1);
scanf("%d",&s[i].marks[m]);
}
printf("\n");
}
calculate(s,n); //structure,number of students
printf("\nstudent's Information-------------\n");
display(s,n);
printf("\n");
printf("\nSort the Array in ascending order of marks:");
sort(s,n);
printf("\n");
}
5
hoge 123 1 2 3
fuga 456 4 5 6
foo 112233 11 22 33
meow 111 1 1 1
mofu 999999 99 99 99
$ gcc prog.c -Wall -Wextra -std=gnu11
Enter the number of students:
Enter student's name: Enter student's rollno: Enter the student's marks: Enter the student's marks: Enter the student's marks:
Enter student's name: Enter student's rollno: Enter the student's marks: Enter the student's marks: Enter the student's marks:
Enter student's name: Enter student's rollno: Enter the student's marks: Enter the student's marks: Enter the student's marks:
Enter student's name: Enter student's rollno: Enter the student's marks: Enter the student's marks: Enter the student's marks:
Enter student's name: Enter student's rollno: Enter the student's marks: Enter the student's marks: Enter the student's marks:
student's Information-------------
Student's Name: hoge
Student's Rollno: 123
Subject 1 Marks: 1
Subject 2 Marks: 2
Subject 3 Marks: 3
Total Marks: 6
Total Percent: 2.00
Student's Name: fuga
Student's Rollno: 456
Subject 1 Marks: 4
Subject 2 Marks: 5
Subject 3 Marks: 6
Total Marks: 15
Total Percent: 5.00
Student's Name: foo
Student's Rollno: 112233
Subject 1 Marks: 11
Subject 2 Marks: 22
Subject 3 Marks: 33
Total Marks: 66
Total Percent: 22.00
Student's Name: meow
Student's Rollno: 111
Subject 1 Marks: 1
Subject 2 Marks: 1
Subject 3 Marks: 1
Total Marks: 3
Total Percent: 1.00
Student's Name: mofu
Student's Rollno: 999999
Subject 1 Marks: 99
Subject 2 Marks: 99
Subject 3 Marks: 99
Total Marks: 297
Total Percent: 99.00
Sort the Array in ascending order of marks:Student's Name: meow
Student's Rollno: 111
1 1 1 Total Marks: 3
Total Percent: 1.00
Student's Name: hoge
Student's Rollno: 123
1 2 3 Total Marks: 6
Total Percent: 2.00
Student's Name: fuga
Student's Rollno: 456
4 5 6 Total Marks: 15
Total Percent: 5.00
Student's Name: foo
Student's Rollno: 112233
11 22 33 Total Marks: 66
Total Percent: 22.00
Student's Name: mofu
Student's Rollno: 999999
99 99 99 Total Marks: 297
Total Percent: 99.00
Exit Code:
0