Program that the N numbers and printa the media, end of reading occurs when reading a negative value

Asked

Viewed 115 times

0

With the following question to solve, I need to create a progression in c that le n positive integers compute and display the artimetica media of these, the halting condition of this loop and the insertion of a negative number, I made the following code, but always it returns as result 1 no matter the values used(yes I’ve already set everything as float 1 anyway, ta duplicated nothing, there’s nothing here similar to this problem), what I’m doing wrong?

#include <stdio.h>
#include <stdlib.h>

int main(){
    int n,s,c;
    float m;

    do{
        printf("Digite um valor: ");
        scanf("%d", &n);
        s= s+n;
        c++;
    }while(n>0);
    m=s/c;
    printf("A media dos valores digitados foi: %d", m);
    return 0;
}
  • I’ve already edited the question.

  • Make the appropriate conversion: m = (float)s / c;

  • It did nothing to make this conversion the result displayed yet and 1

  • You forgot to initialize the c and s variables with zero.

  • Gave the same thing

  • 2

    in the printf has %d and to write the value of m is %f

  • and finally it was, thank you very much, I was going to spend the whole day pondering on the same problem

Show 2 more comments
No answers

Browser other questions tagged

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