求一维数组a中所有元素的平均值,结果保留两位小数 此题为改错题,改正程序中的错误使它能得出正确的答案

#include <conio.h>
#include <stdio.h>
main()
{ int a[10]={10,4,2,7,3,12,5,34,5,9},i;
/************found************/
int aver,s;
clrscr();
/************found************/
s = 0;
for ( i=1; i<10; i++)
s += a[i];
aver = s / i;
printf("The aver is: %.2f\n", aver);
}

#include <conio.h>
#include <stdio.h>
main()
{ int a[10]={10,4,2,7,3,12,5,34,5,9},i;
/************found************/
int aver,s; // 两位小数-->改为 float aver, s;
clrscr();
/************found************/
s = 0;
for ( i=1; i<10; i++) // 注意首元素!改为 for (i=0; i<10; i++)
s += a[i];
aver = s / i;
printf("The aver is: %.2f\n", aver);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-26
int a[10]={10,4,2,7,3,12,5,34,5,9},i;
/************found************/
// int aver,s;
float aver, s;
clrscr();
/************found************/
s = 0;
for ( i=1; i<10; i++)
s += a[i];
aver = s / i;
printf("The aver is: %.2f\n", aver);
第2个回答  2011-05-26
#include <conio.h>
#include <stdio.h>
main()
{ int a[10]={10,4,2,7,3,12,5,34,5,9},i;
/************found************/
int aver,s;
clrscr();
/************found************/
s = 0;
for ( i=0; i<10; i++)
s += a[i];
aver = s / 10;
printf("The aver is: %.2f\n", aver);
}
第3个回答  2011-05-26
#include <conio.h>
#include <stdio.h>
main()
{
int a[10]={10,4,2,7,3,12,5,34,5,9},i;
/************found************/
float aver,s;
clrscr();
/************found************/
s = 0;
for ( i=0; i<10; i++) //下标应该从a[0]到a[9]
s += a[i];

aver = s / i;
printf("The aver is: %.2f\n", aver);
}
第4个回答  2011-05-26
aver 定义成FLOAT型。。不是在TC上运行的话。。请删掉clrscr(); i从0开始循环。。
以上
相似回答