---
title: "Batsman average using structure"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-batsman-average"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-batsman-average
site: BCT Engineering
---

# Batsman average using structure

Computer Programming · Chapter 8: Structures

- Asked in: 2069 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/struct-batsman-average
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

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.

## 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.

## Answer

**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.

```c
#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`

```text
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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the batting average?
   A. runs * dismissals
   B. total runs / total dismissals
   C. dismissals / runs
   D. runs + dismissals
   Answer: B. As given in the question. (Key point: Formula)

2. Why is (float) used in the division?
   A. To convert to a string
   B. To round up
   C. To avoid integer division
   D. To avoid scanf
   Answer: C. int/int would drop the fraction. (Key point: Cast)

3. What if a player was never dismissed?
   A. It prints 0
   B. It prints infinity
   C. The program searches again
   D. The average is undefined (division by zero), so a message is shown
   Answer: D. The code checks dismissed == 0. (Key point: Zero dismissals)

4. How is the player found?
   A. Compare entered name with each name using strcmp() == 0
   B. Using ==
   C. Using sorting
   D. Using the first player
   Answer: A. Strings are compared with strcmp. (Key point: Search)

5. 1200 runs and 25 dismissals give?
   A. 50.00
   B. 48.00
   C. 30.00
   D. 4.80
   Answer: B. 1200/25 = 48. (Key point: Sample)

## Related in Computer Programming

- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [Store 48 students in file with append](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-48-students-append) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Complex number multiplication using structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-multiply) — Computer Programming, chapter 8
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3

## Related in other subjects

- [Semantic network: baseball player](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Asemantic-network-baseball-pee-wee) — Artificial Intelligence, chapter 5
- [Effect of five-times rotor resistance on 40 kW slip ring motor](https://www.bctengineer.com/question.md?key=semester_4%2Felectrical_machines_i%3Aslip-ring-40kw-rotor-resistance-5x) — Electrical Machines I, chapter 5
- [Bayes' theorem; DVD players warranty](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Abayes-dvd-warranty) — Probability and Statistics, chapter 1
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
