Problem with the use of float in vector C/C++

Asked

Viewed 113 times

0

I need to do the following problem in C:

Read 12 valid student grades and store them in an array of notes. Assuming that there may be errors in the typing process, some values stored in the vector may be invalid, that is, outside the range between 0.0 and 10.0. Determine and display the amount of invalid banknotes typed and the average of valid banknotes.

By the problem the vector needs to be float, however when typing the value is assigned 0 instead of the value typed at the position being read.


#include <stdio.h>
#include <stdlib.h>
main(){
    //declara as variaveis --> notasval(quantidade de notas validas), soma(soma das notas validas)
    float  media, notas[12], soma=0;    
    int notasval=0, i;

    for(i=0;i<12;i++){
        scanf("%f", &notas[i]); //Lê as notas 
        if(notas[i]>=0.0 && notas[i]<=10.0){ //verifica se as notas estão no intervalo valido (entre 0 e 10)
            notasval = notasval + 1; //soma +1 ao contador notasval caso a nota for valida
            soma = soma + notas[i]; //executa a soma das notas validas para depois fazer a média
        }
        printf("\t%d", notas[i]);//Eu estava usando essas 3 linhas para printar as variáveis
        printf("\t%d", notasval);
        printf("\t%d\n", soma);
    }
    media = soma/notasval; //faz a média

    printf("A quantidae de notas invalidas é: %i\nA média das notas validas é: %.2f\n", 12-notasval, media); //mostra os resultados
}

The first line of zeros is the vector value, the middle line is the number of valid notes, and the last line was to be the sum of the valid notes: resultado

  • You are printing soma (and notas[i] also) with %d. Then the printf is trying to interpret the bytes of soma (and notas[i] also) as if it were an integer. The correct thing would be to print with %f for float

  • The statement is ambiguous, so it becomes complicated to solve properly.

  • mdss, had not even noticed kkkkkkkk, mto thanks

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

The statement is ambiguous so it is not a good problem to solve. He talks about entering 12 valid notes, then admits that among those 12 may have some invalid ones. But pretending you don’t have this problem there is a formatting error of the data presentation. So it is more organized and correct:

#include <stdio.h>

int main() {
    float notas[12], soma = 0;    
    int notasval = 0;
    for (int i = 0; i < 12; i++) {
        scanf("%f", &notas[i]);
        if (notas[i] >= 0.0 && notas[i] <= 10.0) {
            notasval++;
            soma += notas[i];
        }
    }
    printf("A quantidae de notas invalidas é: %i\nA média das notas validas é: %.2f\n", 12 - notasval, soma / notasval);
}

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

I took the impression of every step entered because the statement does not ask for it. If it is there for debugging reasons I see no advantage in keeping it now. I took the comments because they are obvious , redundant and some with misinterpretation of what happens there, one of the reasons it is better not to comment, only has something worse than code without comments, a code with useless and wrong comment.

If you are using C there is no reason to speak in C++, you may not know, but they are very different languages that have some compatible things.

  • mto thanks for the help

  • @Arisbardelotto see on [tour] the best way to say thank you.

Browser other questions tagged

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