用c++求十个数的最小值、最大值、平均值、以及和

帮忙啊
2024-11-09 05:52:35
推荐回答(2个)
回答1:

这个不就是4个算法问题吗...
你定义一个数组
先直接累加 求和
然后除以10 得到平均值
之后用比较法 比较a[i] a[i+1] 然后让t=大的,h=小的,i从0到8
然后输出t 最大值
h最小值

回答2:

#include
using namespace std;

void main()
{
int num[10];
int max,min;
int i;
int max_num,min_num;
int sum;
int average;

cout<<"Please enter the ten numbers:(after each input please press Enter!!)"<
for(i=0;i<10;i++)
{
cout<<"Please enter number "<< i+1 < cin>>num[i];
cout< }

cout<<"the munbers are:"<
for(i=0;i<10;i++)
{
cout<< num[i] <<" ";
}

cout<
max=num[0];
min=num[0];
max_num=0;
min_num=0;

sum=0;

for(i=0;i<10;i++)
{
sum+=num[i];

if(num[i]>max)
{
max=num[i];
max_num=i;
}
else if(num[i] {
min=num[i];
min_num=i;
}
else
continue;
}

average=sum/8;

cout<<"the max is number " << max_num+1 << ":"<< max < cout<<"the min is number " << min_num+1 << ":"<< min < cout<<"the sum is "<< sum << endl;
cout<<"the average is "<< average << endl;

}

vc6.0调试通过的