luc081.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Create structure for Cricketers (Name, Age, Tests, Avg Runs). Sort 20 records by average runs using qsort().
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct cricketer
{
char name[30];
int age;
int tests;
float avg_runs;
};
// Comparator function for qsort
int compare(const void *a, const void *b)
{
struct cricketer *c1 = (struct cricketer *)a;
struct cricketer *c2 = (struct cricketer *)b;
if (c1->avg_runs > c2->avg_runs) return 1;
else if (c1->avg_runs < c2->avg_runs) return -1;
else return 0;
}
int main()
{
// Initializing fewer than 20 for demonstration, but logic applies to 20
struct cricketer team[5] = {
{"Kohli", 34, 110, 53.4},
{"Smith", 33, 95, 59.8},
{"Root", 32, 120, 50.1},
{"Sharma", 35, 80, 45.5},
{"Williamson", 32, 90, 54.0}
};
int n = 5, i;
printf("Before Sorting:\n");
for (i = 0; i < n; i++)
printf("%s: %.2f\n", team[i].name, team[i].avg_runs);
qsort(team, n, sizeof(struct cricketer), compare);
printf("\nAfter Sorting (Ascending Avg Runs):\n");
for (i = 0; i < n; i++)
printf("%s: %.2f\n", team[i].name, team[i].avg_runs);
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47