Sort Array using Structure coderinme

Sort Array using Structure coderinme

Sort Array using Structure coderinme
write a program to enter the name and marks of student and sort on the basis of name.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct record
{
char name[50];
int marks;
};
int comp_avg (const void *a, const void *b)
{
struct record *ia = (struct record *)a;
struct record *ib = (struct record *)b;
return strcmp(ia->name,ib->name);
}
void sort_structs_example(void)
{
int i,len;
struct record structs[100];
printf("\nGIVE THE NUMBER OF THE RECORDS:\n");
scanf("%d",&len);
printf("\nNAME MARKS\n");
for(i=0;i<len;i++)
{
scanf("%s",&structs[i].name);
scanf("%d",&structs[i].marks);
}
qsort(structs, len, sizeof(struct record),comp_avg);
printf("\nYOUR SORTED RECORD IS:\n");
for(i=0;i<len;i++)
{
printf("%s ",structs[i].name);
printf("%d ",structs[i].marks);
printf("\n");
}
}
int main()
{
sort_structs_example();
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Sort Array using Structure coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.