Industrial Training




Sorting of a sturcture of multiple keys



/* Sorting of a sturcture of multiple keys. */

Write a program that accepts a set of 5 records for students. Ask user to enter name, age and height of a student. Sort these records in ascending order of their names. If the names are alike then sort according to their age.

#include <stdio.h>
#include <conio.h>
#include <string.h>

struct stud
{
char name[25] ;
int age ;
float height ;
} ;

void main( )
{
int i, j, choice ;
struct stud s[5], temp ;

float ff ( float ) ;

clrscr( ) ;

printf ( "Enter student's name, age and height in cm :-\n") ;
for ( i = 0 ; i <= 4 ; i++ )
{
fflush ( stdin ) ;
gets ( s[i].name ) ;
scanf ( "%d %f", &s[i].age, &s[i].height ) ;
}

clrscr( ) ;

for ( i = 0 ; i <= 3 ; i++ )
{
for ( j = 0 ; j <= 3 - i ; j++ )
{
if ( strcmp ( s[j].name, s[j + 1].name ) >= 0 )
{
if ( strcmp ( s[j].name, s[j + 1].name ) == 0 )
{
if ( s[j].age > s[j + 1].age )
{
temp = s[j] ;
s[j] = s[j + 1] ;
s[j + 1] = temp ;
}
}
else
{
temp = s[j] ;
s[j] = s[j + 1] ;
s[j + 1] = temp ;
}
}
}
}

printf ( "Records after sorting :-\n") ;
printf ( "Students Name\t\tAge Height\n" ) ;

for ( i = 0 ; i <= 4 ; i++ )
{
printf ( "%-20s %2d %.2f\n", s[i].name, s[i].age, s[i].height ) ;
}

getch( ) ;
}

float ff ( float f )
{
float *f1 = &f ;
return *f1 ;
}



Hi I am Pluto.