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

CS 200 – Exam 1 Name ________________________________ Overview: In this project

ID: 672541 • Letter: C

Question

CS 200 – Exam 1 Name ________________________________ Overview: In this project you will create a C++ program to compute simple batting statistics for a baseball player. The project has four parts. In the first part you will receive batting history for a player and compute their batting average. Then you will compute two additional batting statistics: slugging percentage, on-base percentage. Part One: 1. Create a comment at the beginning with your name, the date, and a brief description of the project. 2. Create variables to hold the counts for: number of singles, the number of doubles, the number of triples, the number of homeruns, the number of walks, and the number of outs. 3. Create the code necessary to read the batting history from a file named “battingHistory.txt”. This file will contains values for the number of singles, doubles, triples, homeruns, walks, and outs. 4. Create a function named computeBattingAverage that will accept two parameters: number of walks and total number of hits (singles + doubles + triples + homeruns). This function should return the batting average BattingAverage = TotalHits / (TotalHits + Outs) Part Two: Calculate on-base percentage 1. Create a function to calculate on-base percentage. 2. This function will accept 3 parameters (total hits, walks, and outs). 3. The function will return the on-base percentage: on-base percentage = (total hits + walks) / (hits + walks + outs). Part Three: Calculate slugging percentage 1. Create a function to calculate slugging percentage. 2. This function will receive 6 parameters: number of singles, the number of doubles, the number of triples, the number of homeruns, and the number of outs. 3. The function will return the slugging percentage: (singles + doubles * 2 + triples * 3 + homeruns * 4) / (total hits + number of outs). Part Four: 1. Create a menu that will allow the user to select one of the batting statistics (batting average, on-base percentage, of slugging percentage. There should also be a selection to end the process. 2. Using a switch-structure call the appropriate function and display a message and the results of the function call displayed to 3 decimal places. For example “Batting Average: 0.215“ 3. The menu should be in a looping structure that will allow the user to continue selecting statistic to view until they choose the menu item to end the process.

Explanation / Answer

program:

/*
Name:
Date:
Description:In this project computes simple batting statistics for a baseball player.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

double computeBattingAverage(int ,int );
double computeOnbasePercentage(int , int , int );
double calculateSluggingPercentage(int , int , int, int, int, int);
int main()
{
   int numberOfSingles, numberOfDoubles, numberOfTriples, numberOfHomeruns, numberOfWalks, numberOfOuts;
   int TotalHits, choice=0;
   ifstream infile;
   infile.open("battingHistory.txt");
while(!infile.eof())
{
   infile>>numberOfSingles;
   infile>>numberOfDoubles;
   infile>>numberOfTriples;
   infile>>numberOfHomeruns;
   infile>>numberOfWalks;
   infile>>numberOfOuts;
   }
infile.close();
TotalHits=numberOfSingles + numberOfDoubles + numberOfTriples + numberOfHomeruns;
//cout<<TotalHits;
while(choice<5)
{
   cout<<endl<<"MENU";
   cout<<endl<<"1.Batting Average";
       cout<<endl<<"2.Onbase Percentage";
       cout<<endl<<"3.Slugging Percentage";
       cout<<endl<<"4.Quit";
       cout<<endl<<"Enter your choice:";
       cin>>choice;
       double d;
       switch(choice)
       {
           case 1:d=computeBattingAverage(numberOfWalks , TotalHits);
           cout<<endl<<"Batting Average: "<<std::setprecision(3)<<d;
           break;
           case 2:d=computeOnbasePercentage(TotalHits, numberOfWalks, numberOfOuts);
               cout<<endl<<"Onbase Percentage: "<<std::setprecision(3)<<d;
               break;
           case 3:d=calculateSluggingPercentage(numberOfSingles, numberOfDoubles, numberOfTriples, numberOfHomeruns, numberOfWalks, numberOfOuts);
           cout<<endl<<"Slugging Percentage: "<<std::setprecision(3)<<d;
               break;
           case 4: exit(0);
       }
   }
   return 0;
}

double computeBattingAverage(int walks ,int totalhits)
{
   double BattingAverage = double(totalhits)/double(totalhits + walks) ;
   return BattingAverage;
}

double computeOnbasePercentage(int totalhits, int walks, int outs)
{
   double + walks) / double(totalhits + walks + outs);
   return OnbasePercentage;
}

double calculateSluggingPercentage(int singles, int doubles, int triples, int homeruns, int walks, int outs)
{
   int totalHits=singles + doubles + triples + homeruns;
   double SluggingPercentage=double(singles + doubles * 2 + triples * 3 + homeruns * 4) / double(totalHits + outs);
   return SluggingPercentage;  
}

output:


MENU
1.Batting Average
2.Onbase Percentage
3.Slugging Percentage
4.Quit
Enter your choice:1

Batting Average: 0.697
MENU
1.Batting Average
2.Onbase Percentage
3.Slugging Percentage
4.Quit
Enter your choice:2

Onbase Percentage: 0.815
MENU
1.Batting Average
2.Onbase Percentage
3.Slugging Percentage
4.Quit
Enter your choice:3

Slugging Percentage: 1.18
MENU
1.Batting Average
2.Onbase Percentage
3.Slugging Percentage
4.Quit
Enter your choice:4

--------------------------------
Process exited after 15.21 seconds with return value 0
Press any key to continue . . .