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", ¬as[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:
You are printing
soma
(andnotas[i]
also) with%d
. Then theprintf
is trying to interpret the bytes ofsoma
(andnotas[i]
also) as if it were an integer. The correct thing would be to print with%f
forfloat
– Jefferson Quesado
The statement is ambiguous, so it becomes complicated to solve properly.
– Maniero
mdss, had not even noticed kkkkkkkk, mto thanks
– Ari Sbardelotto
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).
– Maniero