Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Sorting of a sturcture of multiple keys

Posted By: Maya Hughes     Category: C Programming     Views: 6980

Sorting of a sturcture of multiple keys.

Code for Sorting of a sturcture of multiple keys in C Programming

#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 ;
}
  
Share: 


Didn't find what you were looking for? Find more on Sorting of a sturcture of multiple keys Or get search suggestion and latest updates.

Maya Hughes
Maya Hughes author of Sorting of a sturcture of multiple keys is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!