4
I am using codeblocks, I am beginner in programming, starting with C, on account of college.
The error is this: In the part of the code where it stores the value of the average of the student, only stores the correct value of the average of the first student, the averages of the following students the algorithm does not make correct average, that is, initially it calculates the average, from the second student it does not store the correct value.
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct aluno{
char nome[50];
float nota[6];
float media;
};
main()
{
int i, j;
float soma, troca;
char classificado[10];
soma = 0;
struct aluno ficha[6];
// Entrada de dados
for (i=1; i<6; i++){
printf("\n Digite o nome do aluno: ");
scanf("%s", &ficha[i].nome);
printf("\n Nome: %s", ficha[i].nome);
for (j=1; j<5; j++){
printf("\n Informe a nota: ",j);
scanf("%f", &ficha[i].nota[j]);
soma = soma + ficha[i].nota[j];
}
ficha[i].media = soma/4;
printf("\n A média do aluno é: %f",ficha[i].media);
}
// Reordernação de vetor para crescente
for(i=0; i<5; i++){
for(j=i+1; j<6; j++){
if(ficha[i].media > ficha[j].media){
troca = ficha[i].media;
ficha[i].media = ficha[j].media;
ficha[i].media = ficha[j].media;
ficha[j].media = troca;
}// if
}
}
for(i=1; i<6; i++){
printf("\n Os nomes dos alunos são: %s",ficha[i].nome);
}
// Saída de dados
for(i=1; i<6; i++){
printf("\n A média do aluno é: %2f",ficha[i].media);
}
}
After compiled
soma = 0
has to be inside the firstfor
, otherwise does not clear the sum between students.– Isac
Code::Blocks is not a compiler, see https://answall.com/q/101691/101
– Maniero
Thank you very much Ricardo, solved it. Thanks also Maniero. Despite having solved, could tell me Ricardo, what the reason of having to put inside the first is ?
– Felipe Mascarenhas