-2
Make a C program that receives a sequence of natural numbers and displays all elements which are greater than the arithmetic mean of this sequence. If there are no elements greater than the average, the number 0 shall be printed on a single line.
My code:
#include<stdio.h>
int main(){
int N, i, qtde = 0;
signed long int valores[10000];
float media = 0;
scanf("%d", &N);
for(i = 0; i < N; i++){
scanf("%li", &valores[i]);
media += valores[i];
}
for(i = 0; i < N; i++){
if( (media/N) < valores[i] ){
printf("%li ", valores[i]);
qtde++;
}
}
if(qtde == 0)
printf("0\n");
return 0;
}
The electronic judge accuses "Wrong Answer". Any idea why?
Well, the logic seems correct. I could put here the problem statement?
– G. Bittencourt
https://moj.naquadah.com.br/contests/jl_eda1a_a2_2020_0/vetor8.pdf
– gmn_1450
Maybe it is a problem in formatting the output, since the problem explicitly says that in the output the numbers should be separated by space, but there should be no space after the last number of the output.
– G. Bittencourt
Note that the question says, "Consider also that x is an integer." but you have declared media to float. It is advisable to calculate
media /= N;
a single time right from the first loop and use such a media for comparisons. See also the observation of G. Bittencourt, which can be done as follows:printf("%li%s", valores[i], (i<N) ? " " : "");
.– anonimo