c++输入一个整形数组,长度为10,用自定义函数完成:输出和值,输出平均值,输出最大值,输出最小值

2025-01-01 09:46:29
推荐回答(2个)
回答1:

#include
#includeusing namespace std;int sum(int a[])
{
int b = 0;
for(int i = 0; i < 10; i++)
{
b = b + a[i];
}
return b;
}int avr(int a[])
{
int b;
b = sum(a)/10;
return b;
}int max(int a[])
{
int b = 0;
for(int i = 0; i < 10; i++)
{
if(b < a[i])
{
b = a[i];
}
}
return b;
}int min(int a[])
{
int b = a[0];
for(int i = 0; i < 10; i++)
{
if(b > a[i])
{
b = a[i];
}
}
return b;
}void main()
{
int a[10];
cout << "请输入10个整数并按空格键或者回车分开(一定要输完10个):";
for(int i = 0 ; i < 10; i++)
{
cin >> a[i];
}
cout << "总和为:" << sum(a) < cout << "平均值为:" << avr(a) < cout << "最大值为:" << max(a) < cout << "最小值为:" << min(a) < system("pause");
}

回答2:

#include
using namespace std;
void process(float a[],int n) {

float min,max;
int i=0;
for(i=0,max=min=a[0];i
if(min>a[i])min=a[i];
if(max
}

cout<<"\nmin="< cout<<",max="<
}
void print(float a[],int n)
{

for(int i= 0;i if(i%6==0)cout<<"\n";
cout<
}

}
void main()
{
float a[24]={2,1,4,45,23,33,-7,8,5,9,0,-4,66,12,55,33,32,11,63,14,88,-9,23,6};
print(a,24);
process(a,24) ;
}