배열에 채워야하는 함수에서 배열을 반환하여 배열에 입력 된 성적의 수를 반환하려고합니다. 그런 다음 해당 데이터를 사용하여 배열을 다른 함수로 전달하고 배열에 기록 된 최저, 최고 및 평균 점수를 읽고 싶습니다. 필자가 작성한 프로그램은 아래와 같습니다. 내가 어디로 잘못 가고 있는지 확실하지 않습니다. 프로그램과C++의 함수에서 반환 된 배열에 입력 된 정수의 총 수와 최저 점수와 평균 점수는 어떻게 얻을 수 있습니까?
#include <iostream>
#include <iomanip>
using namespace std;
int getGrades(int[], int);
void calcStats(int[], int, int&, int&, double&);
int main()
{
double av;
int count;
const int numGrades = 20;
int grade[numGrades];
int low = grade[0];
int high = grade[0];
count = getGrades(grade, numGrades);
calcStats(grade, count, low, high, av);
cout << "You have entered " << count << " grades." << endl;
cout << "The lowest score was " << low << "." << endl;
cout << "The highest score was " << high << "." << endl;
cout << "The average of all the scores was " << av << "." << endl;
system("pause");
return 0;
}
int getGrades(int grade[], int size)
{
int gr, count = 0;
cout << "Please enter up to 20 grades and -1 when finished." << endl;
while (cin >> gr && gr != -1)
{
grade[count] = gr;
count++;
}
return count;
}
void calcStats(int grade[], int S2, int &low, int &high, double &av)
{
for (int count = 1; count < S2; count++)
if (grade[count] < low)
{
low = grade[count];
}
for (int count = 1; count < S2; count++)
if (grade[count] > high)
{
high = grade[count];
}
int total = 0;
for (int count = 0; count < S2; count++)
total += grade[count];
av = total/S2;
}
샘플 출력은 지금 읽
는Please enter up to 20 grades and -1 when finished.
45
45
45
45
45
-1
You have entered 5 grades.
The highest score was 45.
The lowest score was -85679.
The average of all the scores was 45.
등급의 수는이 시나리오 5에해야 가장 높은, 가장 낮은 평균은 모든해야 45
질문을 편집하여 샘플 실행 결과 (예 : 5 개 항목)를 포함 시키십시오. – clusterdude
감사합니다. 나는 간단한 시험을했다. –