luc081.c (1576B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Create structure for Cricketers (Name, Age, Tests, Avg Runs). Sort 20 records by average runs using qsort(). 9 */ 10 /* Let Us C, Chap- 17 (Structures), Qn No.: B(d) */ 11 12 /* This file is auto-generated by a bot. */ 13 /* This code is not compiled; it is for reference only. */ 14 15 16 #include <stdio.h> 17 #include <string.h> 18 #include <stdlib.h> 19 20 struct cricketer 21 { 22 char name[30]; 23 int age; 24 int tests; 25 float avg_runs; 26 }; 27 28 // Comparator function for qsort 29 int compare(const void *a, const void *b) 30 { 31 struct cricketer *c1 = (struct cricketer *)a; 32 struct cricketer *c2 = (struct cricketer *)b; 33 34 if (c1->avg_runs > c2->avg_runs) return 1; 35 else if (c1->avg_runs < c2->avg_runs) return -1; 36 else return 0; 37 } 38 39 int main() 40 { 41 // Initializing fewer than 20 for demonstration, but logic applies to 20 42 struct cricketer team[5] = { 43 {"Kohli", 34, 110, 53.4}, 44 {"Smith", 33, 95, 59.8}, 45 {"Root", 32, 120, 50.1}, 46 {"Sharma", 35, 80, 45.5}, 47 {"Williamson", 32, 90, 54.0} 48 }; 49 int n = 5, i; 50 51 printf("Before Sorting:\n"); 52 for (i = 0; i < n; i++) 53 printf("%s: %.2f\n", team[i].name, team[i].avg_runs); 54 55 qsort(team, n, sizeof(struct cricketer), compare); 56 57 printf("\nAfter Sorting (Ascending Avg Runs):\n"); 58 for (i = 0; i < n; i++) 59 printf("%s: %.2f\n", team[i].name, team[i].avg_runs); 60 61 return 0; 62 }