#include
#include
using namespace std;
const LINESIZE = 20;
void prtResult(double *pd, int len);
double getMax(double *pd, int len);
double getMin(double *pd, int len);
double getAverage(double *pd, int len);
void print(double *pd, int len);
int main() {
int length = 0;
cout<<"输入数组长度"< cin>>length; double * const pd = new double[length]; //用随机数初始化数组 cout<<"随机产生的数组内容如下:"< srand((long)time(NULL)); for(int i=0;i < length;i++) { pd[i] = (rand() % 500 +1); cout.width(3); cout< if((i+1) % LINESIZE == 0) cout< } //打印输出结果 cout< for(i = 0;i < (int)(length / 40);i++) { cout< prtResult((pd+40 * i), 40); } cout< if(length % 40 != 0){ prtResult((pd + 40 * i), length % 40); } //释放动态存储空间 delete [] pd; return 0; } void prtResult(double *pd, int len) { print(pd, len); cout<<"max="< <<"min="< <<"average="< } double getMax(double *pd, int len) { double max = pd[0]; for(int i=1;i < len;i++) if(max < pd[i]) max = pd[i]; return max; } double getMin(double *pd, int len) { double min = pd[0]; for(int i=0;i < len;i++) if(min > pd[i]) min = pd[i]; return min; } double getAverage(double *pd, int len) { double sum = 0; for(int i=0;i < len;i++) sum += pd[i]; return (sum / len); } void print(double *pd, int len) { for(int i=0;i < len;i++) { cout.width(3); cout< if((i+1) % LINESIZE == 0) cout< } //cout< } /* * 源码已上传,下载运行即可。 * 通过创建动态变长数组,测试不同长度数组的执行结果. * 运行结果1: * 运行结果2: */
#include
using namespace std;
//最大值
int get_max(int arr[], int size)
{
int max = arr[0];
for (int i = 0; i < size; ++i)
if (max < arr[i])
max = arr[i];
return max;
}
//平均值
int get_average(int arr[], int size)
{
int sum = 0;
for (int i = 0; i < size; ++i)
sum += arr[i];
return sum;
}
int main()
{
int arr[40];
int loop = 0;
cout << "Enter loop: " << endl;
cin >> loop;
//多少组数,程序健壮性不够... loop < 0就....
while (loop--)
{
int i = 0;
while (i < 40 && cin >> arr[i++]);
cout << "max= " << get_max(arr, i) << endl;
cout << "average= " << get_average(arr, i) << endl;
}
return 0;
}
while循环嵌套,然后一堆变量不解释。。。自己试试吧!