8) Write a program to compute various offensive statistics on baseball. The foll
ID: 3758734 • Letter: 8
Question
8) Write a program to compute various offensive statistics on baseball. The following information
will be available on each player:
Number of Walks (BB), Strike Outs (SO), Hit by Pitch (HP), Sac Flys (SF), Singles,
Doubles (2B), Triples (3B), and Home Runs (HR) as well as Number of At Bats (AB). Based on this information, develop a set of functions within your program that will compute the following: Total Bases, Batting Average, Home Run Ratio, Strike Out Ratio, On Base Percentage, and Slugging Average. You do not need to be a baseball fan to do this ... All the information you need in terms of the formulas and explanations can be found at: http://www.baseball-almanac.com/stats.shtml Note: Number of hits is: singles + doubles + triples + home runs If you want to check your work, see compiled stats of the 2014 Boston Red Sox at: http://www.baseball-reference.com/teams/BOS/2014.shtml
Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <string.h>
int CalulateTotalBases(int singles,int doubles,int triples,int hr)
{
int totalbase;
totalbase=(int)singles +(2 * doubles)+(3 * triples)+(4 * hr);
return totalbase;
}
int CalculateBattingAverage (int hits,int ab)
{
int battingAverage=hits/ab;
return battingAverage;
}
int CalculateHomeRunRatio(int hr,int ab)
{
int ratio=hr/ab;
return ratio;
}
int CalculateStrikeOutRatio(int so,int ab)
{
int ratio=so/ab;
return ratio;
}
int CalculateOnBasePercentage(int hits,int bb,int hp,int ab,int sf)
{
int precentage=(hits+bb+hp)/(ab+bb+hp+sf);
return precentage;
}
int CalculateSlugginAverage(int singles,int doubles,int triples,int hr,int ab)
{
int average=((singles+(2*doubles)+(3*triples)+(4*hr)))/ab;
return average;
}
void main()
{
int yesno;
char name[15];
int hits;
int bb;
int so;
int hp;
int sf;
int singles;
int doubles;
int triples;
int hr;
int ab;
int totalBases;
int BattingAverage;
int HomeRunRatio;
int strikeoutRatio;
int onBasePercentage;
int sluggingAverage;
int Enter=1;
int first=1;
int c;
FILE *fp;
do
{
char name[15];
char *playerDetails="";
int c;
FILE *file;
printf("Enter the name of the player ");
scanf("%s",&name);
printf("Enter the Number of Hits ");
scanf("%d",&hits);
printf("Enter the Number of Walks(BB) ");
scanf("%d",&bb);
printf("Enter the Strike Outs ");
scanf("%d",&so);
printf("Enter the Hits by Pitch ");
scanf("%d",&hp);
printf("Enter the Sack Flys ");
scanf("%d",&sf);
printf("Enter the Singles ");
scanf("%d",&singles);
printf("Enter the doubles ");
scanf("%d",&doubles);
printf("Enter the triples ");
scanf("%d",&triples);
printf("Enter the Home Run ");
scanf("%d",&hr);
printf("Enter the Number of At Bats ");
scanf("%d",&ab);
totalBases=CalulateTotalBases(singles,doubles,triples,hr);
BattingAverage=CalculateBattingAverage(hits,ab);
HomeRunRatio=CalculateHomeRunRatio(hr,ab);
strikeoutRatio=CalculateStrikeOutRatio(so,ab);
> sluggingAverage = CalculateSlugginAverage(singles,doubles,triples,hr,ab);
//Palyer data will be saved in the File
fp=fopen("C:\Temp\PlayerDetails.txt","a");
if(fp==NULL)
{
printf("File Not Found");
}
else
{
fprintf(fp,"Name = %s BB = %d Hits = %d SF = %d ab = %d so = %d HR = %d Hp = %d Singles = %d Doubles = %d Triples = %d TotalBases = %d SlugginAverage = %d BattingAverage = %d StrikeOutRatio = %d HomeRunRatio = %d ",name,bb,hits,sf,ab,so,hr,hp,singles,doubles,triples,totalBases,sluggingAverage,BattingAverage,strikeoutRatio,HomeRunRatio);
}
printf("Do you want to enter another Player Details? Press 1 for Yes , Press 0 from NO ");
scanf("%d",&yesno);
if(yesno==1)
{
}
else
{
Enter =2;
}
}while(Enter==1);
//Below Part of code Will Display the Details of the Players By reading the files
fclose(fp);
fp= fopen("C:\Temp\PlayerDetails.txt", "r");
if (fp) {
while ((c = getc(fp)) != EOF)
putchar(c);
fclose(fp);
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.