
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C program to maintain cricketer’s information in tabular form using structures
Problem
How to store the cricketer’s data in tabular form in sorted order based on average runs using structures in C Programming language.
Solution
Let’s try to enter the cricketer information such as name, age, no of matches and average runs he scored. It will be entered in the console at runtime using the structure concept.
And try to display the information in tabular form in sorted order based on average runs scored by each person so that it is easy to identify each person's details clearly.
The logic we used to sort the cricketers in ascending order based on average runs they scored is −
for(i=0;i<2;i++){ for(j=i+1;j<2;j++){ if(c[i].avrn > c[j].avrn){ temp1=c[i]; c[i]=c[j]; c[j]=temp1; } } }
Program
#include<stdio.h> #include<conio.h> #include<string.h> struct cricketer{ char name[50]; int age; int match; float avrn; char temp; }; struct cricketer c[20],temp1; void main() { int i,j; for(i=0;i<2;i++){ printf("Enter data of cricketer %d
",i+1); //fflush(stdin); printf("Name: "); gets(c[i].name); printf("
Age: "); scanf("%d",&c[i].age); printf("
Matches: "); scanf("%d",&c[i].match); printf("
Average runs: "); scanf("%f",&c[i].avrn); scanf("%c",&c[i].temp); } /******************/ /* sorting records */ /*******************/ for(i=0;i<2;i++) { for(j=i+1;j<2;j++) { if(c[i].avrn > c[j].avrn){ temp1=c[i]; c[i]=c[j]; c[j]=temp1; } } } printf("Sorted records:
"); for(i=0;i<2;i++){ printf("%d\t%s\t%d\t%d\t%f
",i+1,c[i].name,c[i].age,c[i].match,c[i].avrn); } getch(); }
Output
Enter data of cricketer 1 Name: Dhoni Age: 39 Matches: 150 Average runs: 200 Enter data of cricketer 2 Name: virat Age: 36 Matches: 135 Average runs: 190 Sorted records: 1 virat 36 135 190.000000 2 Dhoni 39 150 200.000000
- Related Articles
- Write a C program to display all datatypes ranges in tabular form
- C program to store inventory system using structures
- C program to sort names in alphabetical order using structures
- C++ Program to Add Two Distances (in inch-feet) System Using Structures
- C++ Program to Store and Display Information Using Structure
- Write a program to form a cumulative sum list in Python
- C program to find the area of circle and cylinder using structures.
- Write a C# program to calculate a factorial using recursion
- List the three kinds of blood vessels of human circulatory system and write their function in tabular form.
- Write a program to add two complex numbers using C
- How to write a simple calculator program using C language?
- Write a program in C++ to replace all the 0’s with 5 in a given number
- Write a C program using time.h library function
- Write a C program to print numbers in words using elseif statements
- Write a program to print ‘Tutorials Point’ without using a semicolon in C

Advertisements