一、题目
设计程序: 在数组a的10个数中求平均值v,将大于等于v的数组元素进行求和, 并将结果以格式"%.5f"输出.
#include “stdio.h” int main() { int i; float a[10]={7.23,1.5,5.24,2.1,2.45,6.3,5,3.2,0.7,9.81},sum,v=0;
/ * * * 在这里添加代码 * * /
printf("%.5f,%.5f",v,sum); }
二、代码
1.代码如下
#include <iostream>
#include<iomanip>
using namespace std;
float resBeyondAve(float Array[], const int length, float ave);
float aveOfArray(float Array[], const int length);
int main()
{
float a[10] = {7.23, 1.5, 5.24, 2.1, 2.45, 6.3, 5, 3.2, 0.7, 9.81};
float result = aveOfArray(a, 10);
float sum = resBeyondAve(a, 10, result);
cout << fixed << setprecision(5)<< sum<<endl;
}
/*
求出数组平均数
*/
float aveOfArray(float Array[], const int length) {
float sum = 0;
float result;
for (int index = 0; index < length; index++) {
sum += Array[index];
}
result = sum / length;
return result;
}
/*
对数组大于平均数的数进行求和
*/
float resBeyondAve(float Array[], const int length, float ave) {
float sum = 0;
for (int index = 0; index < length; index++) {
if (Array[index] > ave) {
sum += Array[index];
}
}
return sum;
}