某月10天内的气温为:-5,3,4,0,2,7,0,5,-1,2(摄氏度),编程统计出气温在零度以上、零度和零度以下各多少天?并计算出这10天的平均气温值。
參考答案:#include <iostream.h>
#define MAXDATE 10
float date[MAXDATE] =
{-5,3,4,0,2,7,0,5,-1,2};
int main()
{
int above,below,zero;
int i,average,count,sum;
sum = 0;
count = sizeof(date)/sizeof(date[0]);
above = below = zero = 0;
for(i = 0;i<9;i++)
{
sum += date[i];
if (date[i] <0)
{
below ++;
continue;
}
if (date[i]>0)
{
above++;
continue;
}
zero ++;
}
average = sum / count;
cout<<"大于0度的天数:"<<above<<endl;
cout<<"小于0度的天数:"<<below<<endl;
cout<<"等于0度的天数:"<<zero<<endl;
cout<<"平均温度:"<<average<<endl;
return 0;
}