Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C PROGRAMMING A baseball player\'s batting average is calculated as the number o

ID: 670928 • Letter: C

Question

C PROGRAMMING

A baseball player's batting average is calculated as the number of hits divided by the official number of at-bats. In calculating official at-bats, walks, sacrifices, and occasions when hit by the pitch are not counted. Trips to the plate are coded in the batting record as follows: H - hit, O - out, W - walk, S - sacrifice, P - hit by pitch. The program should output for each player the input data followed by the batting average. (Hint: Each batting record is followed by a new character line.)

Use nested while loops. Control the player by entering the uniform number or -1 to end. Control the at bats by entering 'E' to end. Do not worry about printing the players AB information, but do print their uniform number and average. The data will be entered from the keyboard one piece of data at a time, not in a list as indicated in the text.

Explanation / Answer

Program code:

#include <stdio.h>
#include <math.h>
int main()
{
        int player_number;     
        int i=0;                 
        int hits=0 ;             
        int outs=0 ;                            
        int sum;
  char c;             
        double average;                
  
        printf("Enter the baseball player's number> ");
        scanf("%d", &player_number);
      
        printf("Enter the batting records (H O W S P): ");
        scanf("%c",&c);
        while(c!='E' || c!='-1')
  {
   hits=0;
          if(c == 'H')
     hits = hits + 1;
     
         else if(c == 'O')
           hits = hits + 0;
     
          else if(c == 'W')
     hits = hits + 0;
     
          else if(c == 'S')
     hits = hits + 0;
     
    else if(c == 'P')
     hits = hits + 0;
     
    else
     hits = hits + 0;
   outs=0;
   
          if(c == 'H')
     outs = outs + 0;
         else if(c == 'O')
     outs = outs + 1;
          else if(c == 'W')
     outs = outs + 0;
          else if(c == 'S')
     outs = outs + 0;
    else if(c == 'P')
     outs = outs + 0;
    else
     outs = outs + 0;
    i++;
        }

        sum = hits + outs;
        average = hits / sum;
        printf("Player %d's record: ", player_number);
        printf(" Player %d's batting average: %f ", player_number, average);
        return 0;
}