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
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes
const int ARRAY_SIZE = 100;
void InputData(string name[], int score[], int &numPlayers);
void DisplayPlayerData(const string name[], const int score[], const int numPlayers);
double CalculateAverageScore(const int score[], int numPlayers);
void DisplayBelowAverage(const string name[], const int score[], int numPlayers, double averageScore);
//Main
int main()
{
string name[ARRAY_SIZE];
int score[ARRAY_SIZE];
int numPlayers = 0;
double averageScore = 0.0;
cout << fixed << setprecision(2);
InputData(name, score, numPlayers);
DisplayPlayerData(name, score, numPlayers);
CalculateAverageScore(score, numPlayers);
DisplayBelowAverage(name, score, numPlayers, averageScore);
system("pause");
return 0;
}
void InputData(string name[], int score[], int &numPlayers)
{
while (numPlayers < ARRAY_SIZE)
{
cout << "Please enter the player's name (Q to quit):";
getline(cin, name[numPlayers]);
if(name[numPlayers] == "Q" || name[numPlayers] == "q")
{
cout << endl;
break;
}
else
{
cout << "Enter the score for " << name[numPlayers] << ": ";
cin >> score[numPlayers];
cin.ignore();
numPlayers++;
}
}
}
//DisplayPlayerData function
void DisplayPlayerData(const string name[], const int score[], const int numPlayers)
{
cout << setw(10) << left << "\n Name" << setw(5) << right << "Score" << endl;
for (int i = 0; i < numPlayers; i++)
cout << setw(10) << left << name[i] << setw(5) << right << score[i] << endl;
}
//CalculateAverageScore function
double CalculateAverageScore(const int score[], const int numPlayers)
{
double totalScore;
double averageScore;
int i;
for(i = 0, totalScore = 0.0; i < numPlayers; i++)
{
totalScore += score[i];
}
averageScore = totalScore/numPlayers;
cout << "The average score was:" << averageScore << endl;
return averageScore;
}
//DisplayBelowAverage function
void DisplayBelowAverage(const string name[], const int score[], const int numPlayers, const double averageScore)
{
cout << "\n\nScores that were 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 << name[i] << setw(5) << right << score[i] << endl;
}
}
}
|