建立一个Array类,求一个一维数组中各元素的最大值最小值【C++程序设计】】

2024-12-25 12:02:49
推荐回答(2个)
回答1:

运行结果:
Data in Array:
1 3 2 5 6 2 3 6 4 8
Max=8,Min=1,Average=4.00

代码:

#include

class Array
{
public:
Array(int a[10]);
void process();
void print();
private:
int data[10];
int max;
int min;
float average;
};

Array::Array(int a[10])
{
for (int i=0;i<10;i++)
data[i] = a[i];
}

void Array::process()
{
average = 0;
max = min = data[0];
for (int i=0;i<10;i++)
{
if (max < data[i])
max = data[i];
if (min > data[i])
min = data[i];
average+=data[i];
}
average/=10;
}

void Array::print()
{
printf("Data in Array:\n");
for (int i=0;i<10;i++)
printf("%d ",data[i]);
printf("\nMax=%d,Min=%d,Average=%.2f\n",max,min,average);
}

void main()
{
int a[10] = {1,3,2,5,6,2,3,6,4,8};
Array arr(a);
arr.process();
arr.print();
}

回答2:

int[] data =data[10];
...//给里面放数字
int max =data[0];
int min =data[0];
float averge;
float sum=0;
for(int i=1;i sum +=data[i];
if(max max=data[i];
if(min>data[i])
min=data[i];
}
averge =sum/dat.length;