Sort Array of Cricketers coderinme

Sort Array of Cricketers coderinme

Sort Array of Cricketers coderinme
Now let us code something using the concept of Structures in C. It is quite easy and simple to understand. Structure is a collection of variables of different types under a single name.

A record contains name of cricketer, his age, and number of test matches that he has played and the average runs he has scored in each test match. Create an array that holds the records of 20 such cricketer and then write a program to read these records and arrange them in ascending order by average runs.

#include<stdio.h>
#define SIZE 3
typedef struct rec
{
char name[50];
int age, no;
int avg;
} rec;
int compare (rec * a, rec * b)
{
if ( a->avg avg )
return -1 ;
if ( a->avg == b->avg )
return 0;
if ( a->avg > b->avg )
return 1;
}
int main()
{
struct rec r[SIZE];
int i;
printf("\nEnter the record as follows\n1.) Name of the cricketer\n2.) Age of the cricketer\n3.) Total runs of the cricketer\n4.) Average score of the cricketer");
for(i = 0; i <SIZE ; i++)
{
printf("\n\nEnter The Record of %d cricketer: \n", i + 1);
scanf("%s",r[i].name);
scanf("%d%d%d",&r[i].age,&r[i].no,&r[i].avg);
}
qsort (r, SIZE, sizeof(r[0]), compare);
printf("\nThe list of entered cricketers are (sorted) : \n");
for(i = 0; i < SIZE ; i++) {
printf("\nRecord of %d cricketer are (sorted) : \n",i + 1);
printf("%s\n%d\n%d\n%d\n",r[i].name,r[i].age,r[i].no,r[i].avg);
}
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Sort Array of Cricketers 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.