Sem-2 Practical Practice Question

Write a C Program that creates structure Cricketer with
cid, cname and total runs of 10 matches.
(1) Input details of 5 Cricketers.
(2) Display those cricketer’s name who have scored more than 500 runs

Solution. 

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

#define NUM_CRICKETERS 5
#define NUM_MATCHES 10

// Define the Cricketer structure
struct Cricketer {
    int cid;
    char cname[50];
    int runs[NUM_MATCHES];
    int totalRuns;
};

int main() {
    struct Cricketer players[NUM_CRICKETERS];

    // Input details for 5 cricketers
    for (int i = 0; i < NUM_CRICKETERS; i++) {
	printf("\nEnter details for Cricketer %d\n", i + 1);
	printf("Enter Cricketer ID: ");
	scanf("%d", &players[i].cid);
	getchar(); // to consume newline after integer input
	printf("Enter Cricketer Name: ");
	fgets(players[i].cname, sizeof(players[i].cname), stdin);
	players[i].cname[strcspn(players[i].cname, "\n")] = '\0'; // remove newline

	players[i].totalRuns = 0;
	for (int j = 0; j < NUM_MATCHES; j++) {
	    printf("Enter runs in match %d: ", j + 1);
	    scanf("%d", &players[i].runs[j]);
	    players[i].totalRuns += players[i].runs[j];
	}
    }

    // Display cricketers who scored more than 500 runs
    printf("\nCricketers who scored more than 500 runs in 10 matches:\n");
    for (i = 0; i < NUM_CRICKETERS; i++) {
	if (players[i].totalRuns > 500) {
	    printf("%s (Total Runs: %d)\n", players[i].cname, players[i].totalRuns);
	}

    }

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *