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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
const int ARRAY_SIZE = 100; // Declare Array size
// Prototypes
void InputData(string [],int [], int &);
void DisplayPlayerData(string [], int [], int);
double CalculateAverageScore(int [], int );
void DisplayBelowAverage(string [],int [], int, double);
int main() // Main
{
string playerName[ARRAY_SIZE];
int score[ARRAY_SIZE];
int numPlayers = 0;
double averageScore = 0;
cout << fixed << showpoint << setprecision(2);
InputData(playerName, score, numPlayers);
DisplayPlayerData(playerName, score, numPlayers);
CalculateAverageScore(score, numPlayers);
DisplayBelowAverage(playerName, score, numPlayers, averageScore);
system("PAUSE");
return(0);
}
void inputData(string playerName[], int score[], int numPlayers) // inputData function
{
while(numPlayers < ARRAY_SIZE)
{
cout << "Enter player's name (Q to quit): ";
getline(cin, playerName[numPlayers], '\n');
cout << endl;
if ((playerName[numPlayers] == "Q") || (playerName[numPlayers] == "q"))
break;
cout << "Enter " << playerName[numPlayers] << "'s score: ";
cin >> score[numPlayers];
cout << endl;
cin.ignore();
numPlayers++;
}
}
void DisplayPlayerData(string playerName[], const int score[], int numPlayers)
{
cout << setw(10) << left << "\n Name"
<< setw(5) << right << "Score" << endl;
for (int i = 0; i < numPlayers; i++)
{
cout << setw(10) << left << playerName[numPlayers]
<< setw(5) << right << score[numPlayers] << endl;
}
}
double calculateAverageScore(int score[], int numPlayers)
{
int i;
double averageScore = 0, totalScore;
for (i = 0, totalScore = 0; i < numPlayers; i++)
{
totalScore += score[i];
}
averageScore = totalScore/i;
cout << fixed << showpoint << setprecision(2);
cout << averageScore << endl << endl;
return averageScore;
}
void DisplayBelowAverage(string playerName[],int score[], int numPlayers, double averageScore)
{
cout << "Players who scored below average\n";
cout << setw (10) << left << "Name"
<< setw(5) << right << "Score" << endl;
for (int i = 0; i < numPlayers; i++)
if(score[i] < averageScore)
cout << setw (10) << left << playerName[i]
<< setw(5) << right << score[i] << endl;
}
|