Ciao a tutti!
Ho un problema a fare un programma che calcoli la lunghezza media delle parole in una frase.
Io l'ho fatto in un certo modo, ma stampa il numero totale di lettere e non la media.
Qual'è l'errore?
codice:#include <stdio.h> int main(void) { float avg; int n_letters = 0, n_words = 0; char ch; printf("Inserisci una frase: "); while ( (ch = getchar() ) != '\n') { if ( ch != ' ') n_letters++; else if ( ch == ' ') n_words++; } avg = (float)(n_letters)/(float)(n_words); printf("Lunghezza media: %.1f", avg); return 0; }