1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//functions...
int loadArrays(int [], int [], int [], int [], int []);
void calcBatAvg(int[], int[], int[], int);
void printStats(int [], int [], int [], int [], int [],int[], int);
void teamAVG(int[], int[], int[],int[], int);
void best(int[], int[],int);
void worst(int[], int[], int);
const int SIZE = 20;
int playerNum[SIZE];
int atBats[SIZE], hits[SIZE],runs[SIZE], rbis[SIZE], batAvg[SIZE];
int main(int argc, const char * argv[]) {
int numberOfPlayers;
/* Reads the input data into the arrays. */
numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis);
/*Calculates each player's batting average */
calcBatAvg(hits, atBats, batAvg, numberOfPlayers);
//Displays data
printStats(playerNum, atBats, hits, runs, rbis,batAvg, numberOfPlayers);
//Calculate and output teams Average, find best and worst
teamAVG(atBats, hits,runs,rbis, numberOfPlayers);
best(batAvg, playerNum ,numberOfPlayers);
worst(batAvg, playerNum ,numberOfPlayers);
return 0;
}
//load array
int loadArrays(int player[], int bat[], int hit[], int run[], int rbi[])
{
int count = 0;
ifstream statsIn;
//open file
statsIn.open("statsFile.rtf");
// this will alert user if there is a problem opening the file
if (statsIn.fail()) {
cout << "Error opening the file\n";
return 0;
}
while( statsIn >> player[count]>> bat[count]>> hit[count]>> run[count]>> rbi[count])
{ count++;}
statsIn.close();
return count;
}
//calculate batting average
void calcBatAvg(int hits[], int bats[], int batAvg[], int count){
for (int i=0; i< count; i++) {
batAvg[i] = (hits[i] / bats[i]) * 1000;
}
}
//display stats
void printStats(int player[], int bat[], int hit[], int run[], int rbi[],int bavg[],int count){
cout<<"Player Num"<<"\t"<<"At Bat"<<"\t"<<"Hits"<<"\t"<<"Runs"<<"\t"<<"Bat Avg"<<"\t"<<"Comment"<< endl;
for (int i = 0; i < count; i++) {
string comment;
if(bavg[i] >= 500 && bavg[i]>=1000){ comment = "World Series";}
else if (bavg[i] < 500 && bavg[i]>=300){comment = "Farm League";}
else if (bavg[i] < 300){comment = "Little League";}
else{comment ="Not rated";}
cout<<player[i]<<"\t"<<bat[i]<<"\t"<<hit[i]<<"\t"<<run[i]<<"\t"<<rbi[i]<<"\t"<<bavg[i]<<"\t"<< comment <<endl;
}
}
//team AVG
void teamAVG(int bat[], int hit[], int run[],int rbi[], int count){
int btotal = 0, htotal = 0, rtotal = 0, rbtotal= 0, tavg = 0;
for (int i = 0; i<count; i++) {
btotal += bat[i];
htotal += hit[i];
rtotal += run[i];
rbtotal += rbi[i];
}
tavg = (htotal/btotal) *1000;
cout<<"Team"<<"\t"<<btotal<<"\t"<<htotal<<"\t"<<rtotal<<"\t"<<rbtotal<<"\t"<<tavg<<"\t"<<"N/A";
cout<<"Teams Batting AVG: "<< tavg<< endl;
}
//finding best
void best(int bavg[], int num[], int count){
int i, maxIndex = 0;
int max = bavg[0];
for(i=0; i < count; i++)
{
if(max > bavg[i])
{max = bavg[i];
maxIndex = i;}
}
cout<< "Best Player in PLayer #: "<< num[maxIndex]<<" With a Batting AVG of: "<< bavg[maxIndex];
}
//finding worst
void worst(int bavg[], int num[], int count){
int i, minIndex = 0;
int min = bavg[0];
for(i=0; i < count; i++)
{
if(min < bavg[i])
{min = bavg[i];
minIndex = i;}
}
cout<< "Worst Player in PLayer #: "<< num[minIndex]<<" With a Batting AVG of: "<< bavg[minIndex];
}
|