- 2069 Asar · 6 marks
Batsman average using structure
Short answer
Define struct batsman with name, runs, dismissed. After input, search name with strcmp. If found and dismissed !=0, compute average as (float)runs/dismissed. Handle zero dismissals by printing a message.
Create a structure in C to store the name of a batsman, runs scored and no of times the batsman in dismissed. In the program, read the data of five players and display the batting average of the player whose name is entered by the user. Batting average is given by total_runs/total_dismissals.
Design. The structure batsman has name, runs and dismissed. Data for five players is read; then a name is read and searched with strcmp(). The average is runs / dismissals, computed in float (a cast avoids integer division). If a player has never been dismissed, the division would be by zero, so a message is displayed instead.
#include <stdio.h>
#include <string.h>
#define N 5
struct batsman {
char name[30];
int runs;
int dismissed;
};
int main(void)
{
struct batsman b[N];
char key[30];
int found = 0;
for (int i = 0; i < N; i++) {
printf("Player %d (name runs dismissals): ", i + 1);
scanf("%29s %d %d", b[i].name, &b[i].runs, &b[i].dismissed);
}
printf("Enter name of the player: ");
scanf("%29s", key);
for (int i = 0; i < N; i++)
if (strcmp(b[i].name, key) == 0) {
found = 1;
if (b[i].dismissed == 0)
printf("%s has never been dismissed: average not defined\n", key);
else
printf("Batting average of %s = %.2f\n", key,
(float)b[i].runs / b[i].dismissed);
}
if (!found)
printf("Player not found\n");
return 0;
}
Sample run (Dipendra scored 1200 runs and was dismissed 25 times)
Input: Paras 1500 30 Sandeep 900 20 Dipendra 1200 25 Rohit 600 12 Kushal 800 16 Dipendra
Player 1 (name runs dismissals): Player 2 (name runs dismissals): Player 3 (name runs dismissals): Player 4 (name runs dismissals): Player 5 (name runs dismissals): Enter name of the player: Batting average of Dipendra = 48.00
Batting average = 1200 / 25 = 48.00.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Sum and average until 0 is entered · Computer Programming, chapter 5
- Display records sorted by name from file · Computer Programming, chapter 10
- Store 48 students in file with append · Computer Programming, chapter 10
- Read records until end; show nth record · Computer Programming, chapter 10 · asked 2×
- Complex number multiplication using structure · Computer Programming, chapter 8
- Correct program: case array and average · Computer Programming, chapter 3
- Semantic network: baseball player · Artificial Intelligence, chapter 5
- Effect of five-times rotor resistance on 40 kW slip ring motor · Electrical Machines I, chapter 5
- Bayes' theorem; DVD players warranty · Probability and Statistics, chapter 1
- Function overloading; new/delete average program · Object Oriented Programming, chapter 3