Write a C program to maintain cricketer's information in tabular form using structures

In C programming, we can use structures to organize and store cricketer information such as name, age, number of matches, and average runs. This program demonstrates how to input multiple cricketer records and display them in a sorted tabular format based on their average runs.

Syntax

struct structure_name {
    data_type member1;
    data_type member2;
    // ... more members
};

Approach

We will create a structure to hold cricketer information, input data for multiple cricketers, sort them based on average runs using bubble sort, and display the results in tabular format −

  1. Define a structure with name, age, matches, and average runs
  2. Input data for cricketers
  3. Sort records using bubble sort algorithm
  4. Display sorted data in tabular format

Example

#include <stdio.h>
#include <string.h>

struct cricketer {
    char name[50];
    int age;
    int matches;
    float avgRuns;
};

int main() {
    struct cricketer c[3], temp;
    int i, j, n = 3;
    
    /* Input cricketer data */
    for(i = 0; i < n; i++) {
        printf("Enter data for cricketer %d:
", i + 1); printf("Name: "); scanf("%s", c[i].name); printf("Age: "); scanf("%d", &c[i].age); printf("Matches: "); scanf("%d", &c[i].matches); printf("Average runs: "); scanf("%f", &c[i].avgRuns); printf("
"); } /* Sort records by average runs (ascending order) */ for(i = 0; i < n - 1; i++) { for(j = i + 1; j < n; j++) { if(c[i].avgRuns > c[j].avgRuns) { temp = c[i]; c[i] = c[j]; c[j] = temp; } } } /* Display sorted records in tabular form */ printf("
--- Cricketers Sorted by Average Runs ---
"); printf("%-4s %-15s %-5s %-8s %-10s
", "No.", "Name", "Age", "Matches", "Avg Runs"); printf("------------------------------------------------
"); for(i = 0; i < n; i++) { printf("%-4d %-15s %-5d %-8d %-10.2f
", i + 1, c[i].name, c[i].age, c[i].matches, c[i].avgRuns); } return 0; }
Enter data for cricketer 1:
Name: Dhoni
Age: 39
Matches: 150
Average runs: 50.75

Enter data for cricketer 2:
Name: Virat
Age: 34
Matches: 200
Average runs: 58.50

Enter data for cricketer 3:
Name: Rohit
Age: 36
Matches: 180
Average runs: 45.25

--- Cricketers Sorted by Average Runs ---
No.  Name            Age   Matches  Avg Runs  
------------------------------------------------
1    Rohit           36    180      45.25     
2    Dhoni           39    150      50.75     
3    Virat           34    200      58.50

How It Works

  • Structure Definition: The cricketer structure contains fields for name, age, matches played, and average runs
  • Input: Data is collected for each cricketer using scanf()
  • Sorting: Bubble sort algorithm compares average runs and swaps entire structures
  • Display: Formatted output using printf() with field width specifiers for proper alignment

Key Points

  • Structures allow grouping related data items together
  • Bubble sort has O(n²) time complexity but is simple to implement
  • Using scanf("%s") for names assumes single-word names
  • Field width specifiers (%-15s) ensure proper column alignment

Conclusion

This program demonstrates effective use of structures to manage cricketer data, implementing sorting algorithms to organize records, and formatting output for clear tabular presentation. The bubble sort approach ensures cricketers are displayed in ascending order of their batting averages.

Updated on: 2026-03-15T13:45:58+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements