[Array] calculate the average number of user input and the output is greater than the average number of all?

[Array] calculate the average number of user input and the output is greater than the average number of all?

#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 ) {
	    int i;
	    double average = sum / cnt;
	    	    
	    //遍历数组
	    for ( i=0; i<cnt; i++ ) {
	        if ( number[i] > average ) {
	            printf("%d ", number[i]); //使用数组中的元素
	        }
	    }
    }
    return 0;
}

Released nine original articles · won praise 0 · Views 98

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/104870140