Q. A college needs to maintain following information for the records of it's Students.
Student code (int)
Student name(maximum 40 characters)
Age (int)
Total Fee (int)
Address (maximum 100 characters)
Write a program using structure to input records of N students and arrange these records in alphabetical order and display these records.
#BIT_FirstSem_2016_2c_Solutoin
==> Code goes like this:
#define SIZE 3
#include<stdio.h>
int main()
{
struct student
{
int std_code,age,tot_fee;
char name[40], address[100];
};
struct student std[SIZE],temp;
int i,j, n;
printf("How many records:");
scanf("%d",&n);
printf("Enter records of students: \n");
for ( i = 0; i < n; i++)
{
printf("Name: ");
scanf("%s",std[i].name);
printf("Address: ");
scanf("%s",std[i].address);
printf("Student code: ");
scanf("%d",&std[i].std_code);
printf("Age: ");
scanf("%d",&std[i].age);
printf("Total fee: ");
scanf("%d",&std[i].tot_fee);
}
//Sorting by Alphabet
for ( i = 0; i < n-1; i++)
{
for ( j = i+1; j < n; j++)
{
if(strcmp(std[i].name,std[j].name)>0)
{
temp=std[i];
std[i]=std[j];
std[j]=temp;
}
}
}
//Displaying records
printf("\n Details of students are: \n");
printf("-----------------------------------------------------------------------\n");
printf("Std id:\t\t Name:\t\t Address:\t\t Age:\t\t Total fee:\n");
for ( i = 0; i < n; i++)
{
printf("%d\t\t %s\t\t %s\t\t %d\t\t %d\t\t \n",std[i].std_code,std[i].name,std[i].address,std[i].age,std[i].tot_fee);
}
return 0;
}
Output:
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
Maintain information of students and display them by sorting in alphabetical order - C
2:27 PM
C Programs
,
featured
,
OldQuestionSolution2016C
,
sort by alphabets in c
,
sort by name
,
Sorting in C
,
structure in c
Edit
0 comments:
Post a Comment