hello
i was trying to do one of my assignments but i got stuck at sorting my strut arrays elements by alphabetical order. here is the code that i have dose so far but after this even reading as much as i can, i can not get to go any where. hope can get some help here.
#include <stdio.h>
#include <string.h>
void record();
void display();
void sort();
struct std
{
int stdid;
char name[20];
int mark;
char status[10];
}s1[5];
void main()
{
record();
sort();
display();
}
void record()
{
int i;
for(i=0;i < 5;i++)
{
printf("Enter the Student Id: ");
scanf("%d",&s1[i].stdid);
printf("Enter the name: ");
flushall();
gets(s1[i].name);
printf("Enter the student marks: ");
scanf("%d",&s1[i].mark);
printf("Enter the status: ");
flushall();
gets(s1[i].status);
}
}
void display()
{
int i;
printf("The record is\n");
for(i=0;i < 5;i++)
{
printf("\nStudent Name : %s",s1[i].name);
printf("\nStudent ID: %4d",s1[i].stdid);
printf("\nStudent Marks: %d",s1[i].mark);
printf("\nStudent Status: %s",s1[i].status);
}
}
void sort()
{
int i;
for ( i = 0; i <5; i++ ){//single pass
if (strcmp( s1[i].name, s1[i+1].name ) < 0 )
{//sort in order of last name
s1[i] = s1[i+1];//swap array elements if statement is true
}
}
}