Advertisement
frain8

Untitled

Nov 19th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. /* Dasproc C - 2019
  2. William Handi Wijaya
  3. 0087
  4.  
  5. Fungsi baseball
  6. */
  7.  
  8. #include <stdio.h>
  9. int main()
  10. {
  11.     int player; // input file - player number
  12.     char prog[100]; // input file - match records
  13.     int progSize; // how many attempts of each player
  14.     FILE *fp; // input - files of records
  15.         int hits = 0, bats = 0, a = 0; //  variables to calculate
  16.         // opening file
  17.         fp = fopen("inputBase.txt", "r");
  18.  
  19.         if(fp == NULL) // check if the file can be opened or not
  20.         {
  21.         printf("Error opening file\n");
  22.         return 0;
  23.         }
  24.         //program loop
  25.         while( fscanf(fp, "%d %s\n", &player, &prog) != EOF ){
  26.             progSize=sizeof(prog)/sizeof(prog[0]); //get the total attempts
  27.             //get the number of bats and hits
  28.             while(a-1!=progSize){
  29.                 if((prog[a] == 'O') || (prog[a] == 'H'))
  30.                     bats++;
  31.                 if(prog[a] == 'H')
  32.                     hits++;
  33.                 a++;
  34.             }
  35.             //display the results
  36.             printf("\n\nPlayer %d's record: ", player);
  37.             printf("Player %d's batting average: %.3f", player, ((double)hits / bats));
  38.             //refreshing variables
  39.             a=0;
  40.             bats=0;
  41.             hits=0;
  42.         }
  43.         //closing file
  44.         fclose(fp);
  45.    
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement