luc081.c¶
Problem Statement
Create structure for Cricketers (Name, Age, Tests, Avg Runs). Sort 20 records by average runs using qsort().
Metadata¶
| Property | Detail |
|---|---|
| Author | Amit Dutta amitdutta4255@gmail.com |
| Date | 08 Feb 2026 |
| License | MIT License (See the LICENSE file for details) |
| Difficulty | Beginner (index: 3 / 10) |
Concepts¶
Beta Feature
This concept detection system is still in beta and may occasionally show incorrect or incomplete results.
- Sorting
- Array
- Pointers
- Iteration
Actions¶
You can print or save this file by opening Raw and using your browser.
Source Code¶
#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;
}
Explanation¶
Explain with AI
Copy the prompt below and paste it into any AI assistant.
You are explaining a C programming code to a beginner.
STRICT RULES:
- Only use the given code. Do NOT assume anything not present.
- Do NOT add extra examples.
- Keep explanation clear and short.
- If something is unclear, say "Not clear from code".
- Follow the exact format below. Do NOT change headings.
FORMAT:
[START]
## What it does
(Explain the overall purpose in 1-2 sentences)
## Step-by-step
(Explain how the code works in steps, simple language)
## Key Concepts
(List concepts like loop, condition, function, etc.)
## Notes
(Mention any limitations, errors, or assumptions)
[END]
CODE (luc081.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;
}