Doubt [MEDIA PROBLEM]

Asked

Viewed 78 times

-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?

  • https://moj.naquadah.com.br/contests/jl_eda1a_a2_2020_0/vetor8.pdf

  • 1

    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.

  • 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) ? " " : "");.

2 answers

0

If you have the maximum (ie 10000 values equal to 1000000) the sum (10000*1000000 = 10000000000) exceeds the limit available in 32 bits (2147483647).

The guy signed long int is not necessarily suitable for that sum.

If you can use C99, try uint_least64_t or /*un*/signed long long int.

#include <inttypes.h> // auto include <stdint.h>
#include <stdio.h>

int main(void) {
    uint_least64_t soma1 = 10000;
    uint64_t soma2 = 10000; // pode haver computadores com C99 e sem este tipo
    unsigned long long int soma3 = 10000;
    signed long int soma4 = 10000;
    signed int soma5 = 10000;

    soma1 *= 1000000;
    soma2 *= 1000000;
    soma3 *= 1000000;
    soma4 *= 1000000;
    soma5 *= 1000000;

    printf("%" PRIuLEAST64 ", %" PRIu64", %llu, %li, %i\n", soma1, soma2, soma3, soma4, soma5);
    printf("sizeof (long)=%zu, sizeof (int)=%zu\n", sizeof (long), sizeof (int));
    return 0;
}

You can see the code running on ideone.com.


If you are limited to C89 (C90) try to calculate the average without calculating the total...

média(N) = média(N-1) + (Val(N) - Val(N-1))/N

i.e., in C language

/* NÂO TESTADO! */
long oldtmp;
scanf("%li", oldtmp);
double media = oldtmp;
for (i = 1; i < N; i++) { /* inicia com i a 1 porque o primeiro valor já foi lido */
    long tmp;
    scanf("%li", &tmp);
    media = media + (tmp - oldtmp) / (i + 1);
}

-1

Using gcc was normal here. I also think in the comparison if you can invert to facilitate understanding. Where if( (media/N) < values[i] ) is read if( values[i] > (media/N) ) .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.