简体   繁体   中英

C program for bowling game

I need to write a program for a game of bowling. Ask the user to enter the number of games and the number of bowlers. For each bowler, get the score for each game. Display the score. Calculate the average for each bowler and display the average. Finally display the team average. I wrote a code, it doesn't have errors but the problem is that it doesn't count average score for players and total score for team. Need help with these calculations.

#include <stdio.h>

int main()
{
    int playerTotal = 0;
    int teamTotal = 0;
    int score;
    int numgames, player, bowler, game;

    printf ("How many games?\n");
    scanf ("%d", &numgames);
    printf ("How many players?\n");
    scanf ("%d", &player);

    for (bowler = 1; bowler <= 3; bowler++)
       {
        for (game = 1; game <= 2; game++)
           {
        printf ("\nEnter the score for game %d for bowler %d: ", game, bowler);
        scanf ("%d", &score);

        playerTotal += score;
           }
        printf ("\nThe average for bowler %d is: ", bowler, playerTotal/2);

        printf ("\nThe total for the game is:", teamTotal);

        teamTotal += playerTotal;
       }


   return 0;
}

It's nothing to do with "not counting" - you're just not printing them. This:

printf ("\nThe average for bowler %d is: ", bowler, playerTotal/2);
printf ("\nThe total for the game is:", teamTotal);

should be:

printf ("\nThe average for bowler %d is: %.1f", bowler, playerTotal / 2.0);
printf ("\nThe running team total is: %d", teamTotal);

Note the change from 2 to 2.0 , since playerTotal is an int , but if the total is odd then the average will (or should) have a .5 on the end.

You're also going to want to set playerTotal = 0; at the start of your outer for loop, otherwise each player is going to get the scores of all the bowlers who entered their scores previously, which is probably not all that fair of a scoring system. You should change:

for (bowler = 1; bowler <= 3; bowler++)
       {
        for (game = 1; game <= 2; game++)
           {

to:

for (bowler = 1; bowler <= player; ++bowler) {
    playerTotal = 0;

    for (game = 1; game <= numgames; ++game) {

which will also loop for the same number of times that you had the user enter. If you do this, you'll also need to change:

printf ("\nThe average for bowler %d is: %.1f", bowler, playerTotal / 2.0);

to:

printf ("\nThe average for bowler %d is: %.1f", bowler,
            playerTotal / (double) numgames);

to divide your average by the correct number of games.

Other than those things, you made a pretty nice attempt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM