Sort details of n persons in ascending order by Name and more - C

Write a program using structure to read Name, Address and telephone number of N persons and sort the in ascending order by Name and display all the person's detail whose age is greater than 25. 
#BIT_FirstSem_2017_3b_8Marks_Solution
==> Code goes like this :

#include<stdio.h>
#include<conio.h>
#define SIZE 50
struct test
{
 int age;
 char name[20],address[30];
};
struct test person[SIZE],a;
int main()
{
int n,i,j;
//Input Section
printf("\n How many persons: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n ");
printf("Enter details of %d persons:\n ",i+1);
printf("Name: ");
scanf("%s",&person[i].name);
printf(" Address: ");
scanf("%s",&person[i].address);
printf(" Age: ");
scanf("%d",&person[i].age);
}
//Sorting by Alphabet
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++){
if(strcmp(person[i].name,person[j].name)>0){
a=person[i];
person[i]=person[j];
person[j]=a;
}
}
//Displaying Records
printf("\n Details of persons are:\n");
printf(" ----------------------------------------------");
for(i=0;i<n;i++)
{
printf(" \n Name:%s \t Address:%s \t Age:%d \t \n ",person[i].name,person[i].address,person[i].age);
}

//Displaying Records of persons age>25
printf("\n People having age greater than 25: \n");
printf(" ----------------------------------------------");
for(i=0;i<n;i++)
//for(j=i+1;j<n;j++)
{
    if(person[i].age>25)
    {
        printf(" \n Name:%s \t Address:%s\t Age:%d \t \n ",person[i].name,person[i].address,person[i].age);
    }
}
return 0;
}

Output:
 

















Output1:













If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment