I am currently taking a cs50 course (free version, so i dont have classmates or instructor feedback) and we are asked to write a program that reads text input from the user, and analyzes the text for:
- number of letters
- number of words
- number of sentences
- L = avg # of letters per 100 words
- S = avg # of sentences per 100 words
- grade level = (((0.0588) * L) - ((0.296) * S)) - 15.8)
Sample text:
"Would you like them here or there? I would not like them here or there. I would not like them anywhere."
I have run debugging on the code and it successfully counts letters, words, and sentences.
The first issue arises here:
float calculate_avg_letters(int letters, int
words)
{
float L = ((letters) / (words)) * 100;
return (L);
}
I have tried everything (i think) from changing the data type, rearranging the parentheses, using two separate functions to perform the division first then multiply the resulting variable, changing the data type of the preceding variable. Step by step debugging shows (letters = 80), (words = 21), and (sentences = 3), so L = ((80 / 21) * 100). It should be ~380, but the closest I was able to get was 300, and most variations output something like 1.44e13
for context, here is the entire code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
float count_letters(string paragraph);
float count_words(string paragraph);
float count_sentences(string paragraph);
float calculate_avg_letters(int letters, int
words);
float calculate_avg_sentences(int sentences, int
words);
int calculate_grade_level(int L, int S);
int main(void)
{
string text = get_string("Text: ");
float letters = count_letters(text);
float words = count_words(text);
float sentences = count_sentences(text);
float L = calculate_avg_letters(letters,
words);
float S = calculate_avg_sentences(sentences,
words);
int grade = calculate_grade_level(L, S);
// print results
if (grade < 1)
{
printf("Before Grade 1\n");
}
else if (grade >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", grade);
}
}
int calculate_grade_level(int L, int S)
{
int grade = (((0.0588 * L) - (0.296 * S)) -
15.8);
return round(grade);
}
float count_letters(string paragraph)
{
int length = strlen(paragraph);
float letters = 0;
for (int i = 0; i < length; i++)
{
if (isalpha(paragraph[i]))
letters++;
}
printf("%.1f letters\n", letters);
return letters;
}
float count_words(string paragraph)
{
int length = strlen(paragraph);
float words = 0;
for (int i = 0; i < length; i++)
{
if (paragraph[i] == ' ')
words++;
}
words = words + 1;
printf("%.1f words\n", words);
return words;
}
float count_sentences(string paragraph)
{
int length = strlen(paragraph);
float sentences = 0;
for (int i = 0; i < length; i++)
{
if (paragraph[i] == '.' || paragraph[i]
== '!' || paragraph[i] == '?')
{
sentences++;
}
}
printf("%.1f sentences\n", sentences);
return sentences;
}
float calculate_avg_letters(int letters, int
words)
{
float L = ((letters) / (words)) * 100;
return L;
}
float calculate_avg_sentences(int sentences, int
words)
{
float S = ((sentences / words) * 100);
return S;
}