Menu Close

Sort the student details based on their names in ascending order

Aarav, Advika, binitta are good friends. They are studying final year B.E. Computer Science and Engineering. They want to develop an application to order student details in ascending order which will be useful for the high school in the village they belong to.

Can you help them in the application development? 

Input:
First-line n the number of students

Next line the name, department, year of study, and CGPA  details of n students 

Output:
Print the expected output in proper format

Note: Students details should be sorted based on their "Names" in ascending order
#include <stdio.h>
#include <string.h>
struct Student{
 char name[50];
 char department[5];
 int yearOfStudy;
 float cgpa;
}S1[100],t;

int main()
{
  int i=0,j=0,n;
  scanf("%d",&n);
  for(i=0;i<n;i++){
   scanf("%s %s %d %f",S1[i].name,S1[i].department,&S1[i].yearOfStudy,&S1[i].cgpa);
  }
  for(i=0;i<n;i++){
   for(j=i+1;j<n;j++){
   if(strcmp(S1[i].name,S1[j].name)>0){
   t=S1[i];
   S1[i]=S1[j];
   S1[j]=t;
   }
   }
  }
  for(i=0;i<n;i++){
   printf("Name:%s\n",S1[i].name);
   printf("Department:%s\n",S1[i].department);
   printf("Year of study:%d\n",S1[i].yearOfStudy);
   printf("CGPA:%.1f\n",S1[i].cgpa);
  }
return 0;
}

INPUT_1:
3
Binitta  cse  1  7.8
Aarav  IT  2  8.2
Adhvika  swe  3  8.6

OUTPUT:
Name: Aarav
Department: IT
Year of study: 2
CGPA: 8.2
Name: Adhvika
Department: swe
Year of study: 3
CGPA: 8.6
Name: Binitta
Department: cse
Year of study: 1
CGPA: 7.8


INPUT_2:
1
stella  cse  1  7.8

OUTPUT:
Name: stella
Department: cse
Year of study: 1
CGPA: 7.8


ILLUSTRATION

Executed using linux terminal

Morae Q!