For Pset2: Readability, I'm writing a program in C that prompts the user for text input, analyzes how many letters, words, and sentences are in the text entered by the user, and calculates the grade level using the Coleman Liau index rounded to the nearest integer.
index = 0.0588 * L - 0.296 * S - 15.8
L = average number of letters per 100 words
S = average number of sentences per 100 words
Here is what my code reads as currently (I've even included lines of code I used for debugging purposes).
// Initialize Coleman-Liau index formula variables
float L = (letters / words * 100);
float S = (sentences / words * 100);
float index = round(0.0588 * L - 0.296 * S - 15.8);
// Debug calculation
float index2 = (0.0588 * (96 / 23 * 100) - 0.296 * (1 / 23 * 100) - 15.8);
int i = (int)index;
// Print Grade level output calculated by index
if (i >= 16)
{
printf("Grade 16+\n");
}
if (i < 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade %i\n", i);
// Print calculation debug
printf("Grade %f\n", index2);
}
// Debug correct letter, word, and sentence count
printf("%i letters, %i words, %i sentences\n", letters, words, sentences);
I used floats so that the values don't truncate and I get a more accurate final answer. As for the debugging, I printed the count for letters, words, and sentences to see what was going on there, as well as initialized index2 to plug in exact values instead of variables without the round function to see the exact float answer before rounding to the nearest integer.
Here is the string I was testing:
In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
According to the assignment, the text is supposed to calculate as Grade 7. I am getting "Grade 8" as the output.
The text consists of 96 letters, 23 words, and 1 sentence, thus the respective values were plugged in manually when finding the value for index2, which calculates as 7.720000. I did the math on Google Calculator, and I got an answer of 7.45565217391, which sounds like the more accurate value as it would round out to Grade 7 as it should according to the assignment.
Is there something wrong with how I'm getting the final answer? I've even tried doubles in hopes that the answer would read more accurately, but I still got the same answer. Why is it a different answer than what Google Calculator provided?
Thanks!