问题:
解答:
#include <iostream>
using namespace std;
#define MAX 10
int input(float* grade, int len)
{
int i = 0;
for (i = 0; i < len; i++)
{
cout << "请输入第" << i + 1 << "个高尔夫成绩(按0结束):";
cin >> grade[i];
if (grade[i] == 0)break;
}
return i;
}
void print(float* grade, int len)
{
cout << "高尔夫的成绩为:";
for (int i = 0; i < len; i++)
{
cout << grade[i] << " ";
}
cout << endl;
}
float average(float* grade, int len)
{
double sum = 0;
for (int i = 0; i < len; i++)
{
sum += grade[i];
}
return sum / len;
}
int main()
{
float grade[MAX];
int index = 0;
float aver = 0;
index=input(grade, MAX);
print(grade, index);
aver=average(grade, index);
cout << "平均成绩为:" << aver << endl;
return 0;
}
运行结果:
考查点:
- 函数
- 数组当函数参数
2024年8月30日19:39:00