Doubt about the lower value of banknotes

Asked

Viewed 159 times

2

I’m having a question about the conditional test to store the lowest value, it’s not really a doubt, the problem is that when it prints, the value of the lowest value is 0.0, which hurts to calculate the average correctly, the highest value worked, but the smallest not, which could 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;       
}
  • This average calculation doesn’t make sense, that’s right?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

2

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

Do so:

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

Iterate to NUM-1. Do this pro higher value too, to avoid future mistakes.

2

This algorithm is very complex and has a lot of unnecessary things. And I would use it like this:

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

int main(){
    float notas[NUM];
    float maior = 0, menor = INT_MAX, media;
    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]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It can be better, I didn’t see it in the best way, because the original didn’t do it either, but it’s a gain. You could avoid comparison and use bit operators, but I don’t think you want that, even if it’s faster.

I calculated the average the right way, modernized the code and simplified it. I don’t know if there was any obligation to do something like that, but it doesn’t make sense.

2

I believe that this 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;
}

Browser other questions tagged

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