私は、ユーザーが100人までのプレーヤーの名前とスコアを入力してから、すべてのプレイヤーの名前とスコアを印刷してから、スコアの平均、最後にスコアが平均以下だったプレイヤーを表示します。私は最後の部分を除いてすべてを行い、平均以下のスコアを表示しました。私はそれについてどうやって行くのかが分かりません。私のDesplayBelowAverage関数では、現在のプレーヤーのスコアを読み取ってそれを平均と比較して、平均以下のスコアとして出力するかどうかを確認しようとしましたが、私が作成したaverageScore値を認識していないようですCalculateAverageScores関数に追加します。ここに私のコードは次のとおりです。平均スコア以下のプレーヤースコアリングプログラムの表示
#include <iostream>
#include <string>
using namespace std;
int InputData(string [], int [], int);
int CalculateAverageScores(int [], int);
void DisplayPlayerData(string [], int [], int);
void DisplayBelowAverage(string [], int [], int);
void main()
{
string playerNames[100];
int scores[100];
int sizeOfArray = sizeof(scores);
int sizeOfEachElement = sizeof(scores[0]);
int numberOfElements = sizeOfArray/sizeOfEachElement;
cout << numberOfElements << endl;
int numberEntered = InputData(playerNames, scores, numberOfElements);
DisplayPlayerData(playerNames, scores, numberEntered);
CalculateAverageScores(scores, numberEntered);
cin.ignore();
cin.get();
}
int InputData(string playerNames[], int scores[], int size)
{
int index;
for (index = 0; index < size; index++)
{
cout << "Enter Player Name (Q to quit): ";
getline(cin, playerNames[index]);
if (playerNames[index] == "Q")
{
break;
}
cout << "Enter score for " << playerNames[index] << ": ";
cin >> scores[index];
cin.ignore();
}
return index;
}
void DisplayPlayerData(string playerNames[], int scores[], int size)
{
int index;
cout << "Name Score" << endl;
for (index = 0; index < size; index++)
{
cout << playerNames[index] << " " << scores[index] << endl;
}
}
int CalculateAverageScores(int scores[], int size)
{
int index;
int totalScore = 0;
int averageScore = 0;
for (index = 0; index < size; index++)
{
totalScore = (totalScore + scores[index]);
}
averageScore = totalScore/size;
cout << "Average Score: " << averageScore;
return index;
}
void DisplayBelowAverage(string playerNames[], int scores[], int size)
{
int index;
cout << "Players who scored below average" << endl;
cout << "Name Score" << endl;
for (index = 0; index < size; index++)
{
if(scores[index] < averageScore)
{
cout << playerNames[index] << " " << scores[index] << endl;
}
}
}
うんを、お役に立てば幸いです。お手伝いありがとう。 – jackofblaze
喜んで助けてください、私はあなたが第2のアプローチを使用していることを願っています – Ezio