따라서 프로그램은 지정된 수의 점수를 수집 한 다음 오름차순으로 표시하고 은 평균 점수를 나타내는으로되어 있습니다. 하지만 지금은 첫 번째 점수 만 표시되며 점수의 수로 나뉩니다. 어떻게 올바르게 표시 할 수 있습니까?(C++) 평균 수익률을 계산하는 함수 (첫 번째 점수/총 점수 수)?
#include <iostream>
#include <iomanip>
using namespace std;
void sortArray(int*, int);
double getAverage(int*, int);
int main()
{
int *scores;
int num_Tests;
cout << "How many test scores would you like to enter?" << endl;
cin >> num_Tests;
scores = new int[num_Tests];
cout << "\nEnter score number (do not use negative numbers):\n";
for (int count = 0; count < num_Tests; count++)
{
cout << count + 1 << ". ";
cin >> scores[count];
}
sortArray(scores, num_Tests);
cout << "\n\n";
cout << "\n\n________________________________________________________________________________" << endl;
cout << "Test Score List (in ascending order)" << endl;
cout << "________________________________________________________________________________" << endl;
for (int count = 0; count < num_Tests; count++)
{
cout << count + 1 << ". ";
cout << scores[count] << endl;
}
cout << "\nAverage test score: " << getAverage(scores, num_Tests) << endl;
return 0;
}
double getAverage(int *scores, int size)
{
double ttlScore = 0.0;
double avgScore = 0.0;
ttlScore += *scores;
avgScore = ttlScore/size;
return avgScore;
}
void sortArray(int *scores, int size)
{
int temp;
bool swap;
do
{ swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (scores[count] < scores[count + 1])
{
temp = scores[count];
scores[count] = scores[count + 1];
scores[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
평균 점수는 모든 점수를 숫자로 나눈 값입니다. 따라서 합계를 계산하여 시작하십시오. –
점수 배열을 반복 할 필요가 있습니다. 단지'ttlScore + = * score '만 할 수는 없으며,'ttlScore + = scores [index]'가되어야합니다. –
또 다른 포인트는이 코드가 될 수 있다는 것입니다. 예를 들어 출력을 표시하기 위해 메인에서 더 많은 기능을 사용했다면 좀 더 깔끔합니다. 개인적인 취향의 문제지만 내 주요 기능이 깔끔하다 : P –