Incorrect Printed Form Values

Asked

Viewed 62 times

0

I don’t know where I’m going wrong.

#include <stdio.h>

int main (){
int i, num, soma, maior, menor;
float media;

printf ("Informe 10 valores:\n");
for (i=1; i<=10; i++){
    scanf ("%d", &num);
    if (i==0){
        maior=num;
        menor=num;
    }
    else{
        if (num > maior){
            maior=num;
        }
        if (num < menor){
            menor=num;
        }
}
soma = soma + num;
media = soma / 10;
}
printf ("Media: %.2f", media);
printf ("\nMaior: %d", maior);
printf ("\nMenor: %d", menor);
}

2 answers

4

There are several errors, probably a mix of conventions without you have noticed:

  1. i never zero, so never start the values of menor nor of maior
  2. The value of soma is not initialized, so loads memory junk
  3. The calculation of media is being done with whole arithmetic
  4. You don’t need to average every iteration

Solving the problems

The first is to iterate starting at 0 and going up to 10 in the open interval. That is to say:

for (i = 0; i < 10; i++) ...

Simply declare the variable soma already initialized solves the problem:

int i, num, soma = 0, maior, menor;

Remove the average calculation from the middle of the iteration, while maintaining the soma resolves item 4.

At the end of the iteration, dividing by 10 as floating point resolves item 3:

media = soma / 10.0f
  • If my i start at 0 the number printed on the smallest is incorrect.

  • So there’s more... pera

  • I don’t see anything yet that could be wrong beyond what I’ve already pointed out, I’ll need to put in ideone. Cc @Gustavocarvalho

  • Before I was having problems because I was not showing the values in bigger and smaller were coming out as 0 but the average came out right, then I saw that they were in the float then I put them as int so that now the average goes wrong and the values and bigger and smaller comes out correct.

  • Failed to initialize soma, updating the response

  • how to initialize the sum?

Show 1 more comment

2


Below is the code with some corrections.

#include <stdio.h>

    int main (){
    int i, num, soma, maior, menor;
    float media;

    printf ("Informe 10 valores:\n");
    soma=0;//Faltava inicializar a variável soma, estava com valor "lixo" da memória
    for (i=0; i<10; i++)//inicializei em 0, com i=1 ele não estava tratando todos os valores de num
    {
        scanf ("%d", &num);
        if (i==0){
            maior=num;
            menor=num;
        }
        else{
            if (num > maior){
                maior=num;
            }
            if (num < menor){
                menor=num;
            }
    }
    soma = soma + num;//Somatório de todos os 10 valores
    }
    media = soma / 10.0;//Soma dividido por 10.0, divisão de int por float resulta em float
    printf ("Media: %.2f", media);
    printf ("\nMaior: %d", maior);
    printf ("\nMenor: %d", menor);
    }

I did the test with values from 1 to 10 and got the following output:

    Media: 5.50
    Maior: 10
    Menor: 1

Browser other questions tagged

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