/* Create structure for Cricketers (Name, Age, Tests, Avg Runs). Sort 20 records by average runs using qsort().
*/
/* Let Us C, Chap- 17 (Structures), Qn No.: B(d) */

/* This file is auto-generated by a bot. */
/* This code is not compiled; it is for reference only. */


#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;
}
