Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Dasproc C - 2019
- William Handi Wijaya
- 0087
- Fungsi baseball
- */
- #include <stdio.h>
- int main()
- {
- int player; // input file - player number
- char prog[100]; // input file - match records
- int progSize; // how many attempts of each player
- FILE *fp; // input - files of records
- int hits = 0, bats = 0, a = 0; // variables to calculate
- // opening file
- fp = fopen("inputBase.txt", "r");
- if(fp == NULL) // check if the file can be opened or not
- {
- printf("Error opening file\n");
- return 0;
- }
- //program loop
- while( fscanf(fp, "%d %s\n", &player, &prog) != EOF ){
- progSize=sizeof(prog)/sizeof(prog[0]); //get the total attempts
- //get the number of bats and hits
- while(a-1!=progSize){
- if((prog[a] == 'O') || (prog[a] == 'H'))
- bats++;
- if(prog[a] == 'H')
- hits++;
- a++;
- }
- //display the results
- printf("\n\nPlayer %d's record: ", player);
- printf("Player %d's batting average: %.3f", player, ((double)hits / bats));
- //refreshing variables
- a=0;
- bats=0;
- hits=0;
- }
- //closing file
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement