Hi all, I've a problem in average calculus with a program in C language.
Below I wrote some snippet of code.
In funzioni.c
Code:
float voto_medio(studente stud[], int dim) {
int i, somma = 0;
float vmedio;
for (i = 0; i < dim; i++) {
somma = stud[i].voto + somma;
}
vmedio = somma/dim;
return (vmedio);
}
and in funzioni.h I have
Code:
#define MAX 50
typedef char STRINGA[30];
typedef struct {
STRINGA nome;
STRINGA cognome;
int voto;
} studente;
void inserisci_studenti(studente*, int);
void visualizza_studenti(studente*, int);
float voto_medio(studente*, int);
void ordina(studente*, int);
void visualizza_studenti_con_giudizio(studente*, int);
void salva_su_file(studente*, int);
My problem is that average calculus in voto_medio function is not correct.
I'm not able to make average with float numbers. For example, with 30, 28 and 24 I have as result 27.000 whereas I must 27.333...
Where is my errore in voto_medio function?
Thanks in advance.