I have to sort the cricketers in ascending order of the average run scored by them using qsort()
function and then print all the details according to it. I am not able to figure out that how can I sort according to the average run scored and then print the list according to it.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int compare(const void*a, const void*b)
{
return (*(int*)a - *(int*)b);
}
void main()
{
int i;
struct cricket
{
char name[20];
int age;
int no_test;
int avrun;
}
players[20] = {
"Virat Kohli", 25, 29, 94,
"Rohit Sharma", 26, 19, 86,
"Mahendra Singh Dhoni", 32, 40, 69,
"Gautum Gambhir", 29, 28, 90,
"Hardik Pandya", 27, 18, 59,
"Chris Gayle", 38, 50, 48,
"James Watson", 40, 54, 68,
"Brett Lee", 38, 53, 83,
"Suresh Raina", 32, 29, 59,
"Sachin Tendulkar", 40, 60, 95,
"Virendra Sehwag", 45, 55, 83
};
qsort(players,11,sizeof(cricket),compare);
for(i = 0; i < 11; i++)
{
printf("%s ", players[i].name);
printf("%d ", players[i].age);
printf("%d ", players[i].no_test);
printf("%d \n", players[i].avrun);
}
getch();
}