24 lines
508 B
C++
24 lines
508 B
C++
|
// 编写一个函数,用于计算一个整数数组中所有元素的平均值,并返回结果。
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
double average(int arr[], int size)
|
||
|
{
|
||
|
int sum = 0;
|
||
|
int size_copy = size;
|
||
|
while (--size >= 0)
|
||
|
{
|
||
|
sum += arr[size];
|
||
|
}
|
||
|
|
||
|
return sum / (double)size_copy;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int arr[] = {1, 2, 3, 4, 5, 6};
|
||
|
int size = sizeof(arr) / sizeof(arr[0]);
|
||
|
cout << "数组的平均值为:" << average(arr, size) << endl;
|
||
|
return 0;
|
||
|
}
|