0

First question here. I am having some issues with the index equation for the readability problem. The code compiles and runs, and I believe it counts the letters, words, and sentences correctly. As written, the resulting values for variables "words_clf" and "sentences_clf" are incorrect which throws off the rest of the equation. Not quite sure what or where my mistake is.

I appreciate the help and let me know if there are other suggestions.

    words_clf = 0.0588 * (letters / words_p1 * 100);
    printf("Words CLF: %f\n", words_clf);

    sentences_clf = 0.296 * (sentences / words_p1 * 100);
    printf("Sentences CLF: %f\n", sentences_clf);

2 Answers 2

1

Line 57 is a problem. Both "letters" and "words_p1" are ints. In the test text you have in the file, the number of words is 14, and the number of letters is 65. Although you might hope the computer figures out that 65/14 = 4.643, since these are both integers, 65/14 is also an integer, thus 4. Later when 4 is multiplied, the rounding error affects the result, even though that later result is a floating point number.
Line 60 has a similar problem.
Advice: put that test text is it's own file. It's possible to pass a file as the input to the program. In this case, since the program takes a single input, the file can be multiple lines, saving the Grade, Letters, etc in the same file.

~/pset2/readability/ $ cat test
Congratulations! Today is your day. You're off to Great Places! You're off and away!
Grade 3
Sentences = 4
Words = 14
~/pset2/readability/ $ ./readability < test 
Text: Sentences: 1
Sentences: 2
Sentences: 3
Sentences: 4
Words + 1: 14
Words CLF: 23.520000
Sentences CLF: 0.000000
CLF Value: 7.720000
CLF Value Rounded: 8.000000
Grade 8.000000
0

Based on my understanding of the course so far, I committed a truncation error with my code initially. The fix for me came within the "sentences_clf" and "words_clf" equations themselves. I added (float) to the equation, "(float)letters / words_p1" for example, and then the program gave me the correct answers.

Thank you for your help as well!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .