Doubt about the lower value of the notes

1

I have a question about the conditional test to store the smallest value, it is not a doubt, the problem is that when it prints, the value of the smallest value gets 0.0, which impairs to calculate the average correctly, at the highest value it worked, but not in the least, what could it be?

#include <stdio.h>
#define NUM 5

int main(){
    int i, j;
    float notas[NUM];
    float media;
    float maior=0, menor=0;

        for(i=0; i<NUM; i++){
            scanf("%f", &notas[i]);
        }

        for(i=0; i<NUM; i++){

            if(notas[i] >= notas[i+1]){
                maior = notas[i];
            }
            else{
                maior = notas[i+1];
            }

        }

        for(i=0; i<NUM; i++){
            if(notas[i] < notas[i+1]){
                menor = notas[i];
            }
            else{
                menor = notas[i+1];
            }
        }


        for(i=0; i<NUM; i++){

            media += notas[i];

        }

        media -= maior;//cálculo da média da escola

                    printf("%.1f %.1f %.1f\n", maior, menor, media);

                        printf("\n");

            for(i=0; i<NUM; i++){
                printf("%.1f ", notas[i]);
            }

    return 0;       
}
    
asked by anonymous 06.10.2016 / 19:29

3 answers

2

This algorithm is very complex and much of it is unnecessary. And it would use like this:

#include <stdio.h>
#include <limits.h>
#define NUM 5

int main(){
    float notas[NUM];
    float media = 0;
    float maior = 0, menor = INT_MAX;
    for (int i = 0; i < NUM; i++) {
        scanf("%f", &notas[i]);
        maior = notas[i] > maior ? notas[i] : maior;
        menor = notas[i] < menor ? notas[i] : menor;
        media += notas[i];
    }
    media /= NUM;
    printf("%.1f %.1f %.1f\n\n", maior, menor, media);
    for (int i = 0; i < NUM; i++) {
        printf("%.1f ", notas[i]);
    }
}

See working on ideone and in C ++ Shell .

It can improve, I did not validate the best way, because the original did not do it too, but it is already a gain. I'd give it to avoid comparison and use bit operators, but I do not think I'd like that, even though it's faster.

I calculated the average of the right way, modernized the code, and simplified it. I do not know if there was any obligation to do something like that, but it does not make sense.

    
06.10.2016 / 19:47
1

I believe it is correct.

#include <stdio.h>

#define NUM 5
int main() {

float notas[NUM];
int i;
float maior, menor, soma = 0;

for(i=0; i<NUM; i++) {
    scanf("%f", &notas[i]);
}
maior = notas[0]; menor = notas[0];
for(i=0; i<NUM; i++) {

    if(maior < notas[i + 1]) {
        maior = notas[i];
    }
    if (menor > notas[i + 1]) {
            menor = notas[i];
    }
        soma += notas[i];
}

printf("Maior: %.2f\nMenor: %.2f\n", maior, menor);
printf("Media: %.2f\n", soma / NUM);

    return 0;
}
    
06.10.2016 / 19:58
1

The loop is wrong, in the last iteration you compare the last value of the vector with a value outside of it. There you can get any value, including 0.0.

Do this:

for(i=0; i<NUM-1; i++){
    if(notas[i] < notas[i+1]){
        menor = notas[i];
    }
    else{
        menor = notas[i+1];
    }
}

Itera to NUM-1. Do it for the highest value too, to avoid future mistakes.

    
06.10.2016 / 19:34