Write the following c++ program Write a program that does the following: It plac
ID: 3681974 • Letter: W
Question
Write the following c++ program
Write a program that does the following: It places player names and their scores into two appropriate arrays. It displays player names and their scores in a formatted output It calculates the average score and outputs it on the screen It displays names and scores of players whose score is above average Use functions to implement this program: The main() function declares a PlayerName array and a Score array. The size of the arrays is 10. The InputData() function, inputs the player name and score into the arrays for an unknown number of players up to 10. The DizplayPlayerData() function, displays the name and score of each player. The CalculateAverageScore() function, calculates the average score and returns it by value. The DisplayAbovAverage() function, displays the name and score for any player who scored above the average. Do not use global variables. An example for program output is as follows: Make sure that the program compiles properly and runs per specification.Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
//function prototypes
void InputData(string [], int [], int &);
void DisplayPlayerData(string [], int [], int);
double CalculateAverageScore(int [], int);
void DisplayBelowAverage(string [], int [], double, int);
const int SIZE = 100;
int main()
{
//declare and initialize PlayerName[] and Score[]
string PlayerName [SIZE] = {" "};
int Score [SIZE] = {0};
//declare numberOfPlayers and averageScore
int numberOfPlayers = 0;
double averageScore = 0;
InputData(PlayerName, Score, numberOfPlayers);
DisplayPlayerData(PlayerName, Score, numberOfPlayers);
averageScore = CalculateAverageScore(Score, numberOfPlayers);
DisplayBelowAverage(PlayerName, Score, averageScore, numberOfPlayers);
return 0;
}
void InputData(string names[], int scores[], int &num)
{
while (num < SIZE)
{
cout << "Enter player name (Q to Quit): ";
getline(cin, names[num]);
if ((names[num] == "Q") || (names[num] == "q"))
break;
else
{
cout << "Enter score for " << names[num] << ": ";
cin >> scores[num];
}
cin.ignore();
num++;
}
}
void DisplayPlayerData(string names[], int score[], int num)
{
cout << " Name" << " " << "Score" << endl;
for (int i = 0; i < num; i++)
cout << names[i] << " " << score[i] << endl;
}
double CalculateAverageScore(int score[], int num)
{
double total = 0;
for (int i = 0; i < num; i++)
total += score[i];
double avg = total/num;
cout << " Then average score is: " << avg;
return avg;
}
void DisplayBelowAverage(string names[], int score[], double avg, int num)
{
cout << " Players who scored below average" << endl;
cout << "Name" << " " << "Score" << endl;
for (int i = 0; i < num; i++)
{
if (score[i] < avg)
cout << names[i] << " " << score[i] << endl;
}
}
output
Compile and Execute C++ Online
Help
Enter player name (Q to Quit): Bob
Enter score for Bob: 3245
Enter player name (Q to Quit): Sue
Enter score for Sue: 1098
Enter player name (Q to Quit): Dave
Enter score for Dave: 8219
Enter player name (Q to Quit): Pat
Enter score for Pat: 3217
Enter player name (Q to Quit): Q
Name Score
Bob 3245
Sue 1098
Dave 8219
Pat 3217
Then average score is: 3944.75
Players who scored below average
Name Score
Bob 3245
Sue 1098
Pat 3217
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.