C语言入门---初试数组---翁恺MOOC

计算数组的平均数,然后打印出高于平均数的那些数
(这个程序的bug在于输入的数字可能超过100个)

#include <stdio.h>

int main()
{
	int x;
	double sum = 0;
	int cnt = 0;
	int number[100];  //定义数组 
	scanf("%d", &x);
	while (x!=-1) {
		number[cnt] = x;  //对数组中的元素赋值 
		sum += x;
		cnt++;
		scanf("%d",&x); 
	}
	if (cnt>0) {
		printf("%f\n",sum/cnt);
		int i;
		for ( i=0; i<cnt; i++) {
			if ( number[i] > sum/cnt) {
				printf("%d\n", number[i]); //使用数组中的元素 
			}
		}
	}
	return 0;
 } 
发布了7 篇原创文章 · 获赞 1 · 访问量 425

猜你喜欢

转载自blog.csdn.net/watermelon_lily/article/details/103988254