对一组整型数据求最大值、最小值、累加和,要求用一个函数完成(多值返回)。
#include <bits/stdc++.h>
using namespace std;
void inputArray(int a[], int n);
int getSum(int a[], int n, int *Max, int *Min);
int main()
{
int n;
cin >> n;
int a[n];
inputArray(a,n);
int Max, Min;
int sum = getSum(a, n, &Max, &Min);
cout << "该数组中最大值是:" << Max << endl;
cout << "该数组中最小值是:" << Min << endl;
cout << "该数组中元素的累加和是:" << sum << endl;
return 0;
}
void inputArray(int a[], int n)
{
int i;
for(i=0;i<n;i++)
{
cin >> a[i];
}
}
int getSum(int a[], int n, int *Max, int *Min)
{
*Max = a[0];
*Min = a[0];
int sum=0;
for(int i=0;i<n;i++)
{
if(a[i]>*Max)
{
*Max = a[i];
}
if(a[i]<*Min)
{
*Min = a[i];
}
sum += a[i];
}
return sum;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有