0

I have been struggling with Week 2 Problem Set 2 Readability Question. At this point, I feel like I am stuck, and spend too much time just trying to fix one error. The code works perfectly except "Grade 8". For Letters, Words and, Sentences, I get the correct output always. Expect "Grade 8" and only on a specific paragraph. Every other grade comes perfectly true. I checked it with debugging tool and tried check50 as well and it gives an error in the expected grade 8 examples. I think there is a middle ground in the index function and I can't get out of it.

"Grade 8: "

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"

The output after the Coleman-Liau index comes
Grade float: 7.403509.
Rounded Grade: 7 Expected: 8

The index function:

float L = (float)letters / (float)words * 100; // Average number of letters per 100 words
float S = (float)sentences / (float)words * 100;// Average number of sentences per 100 words
float index = 0.0588 * L - 0.296 * S - 15.8;
return index;

main:

float gradi = grade(letters, words, sentences); //  Call function

int grad = round(gradi); // round the float save as int

Then I go to print functions.

Does anyone have any idea what I am doing wrong? The only error I get is from Grade 8 and I am burned out right now.

1

I have a suggestion after trying a few variations in my own code.

Consider that 100 is an int. What effect is that having on casting floats back to integers? However, 100.0 is a float. It won't cause anything to be cast back to an int.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • I tried to change it. But nothing is changing. Since the division at the end operated in float, changing the type of "100" doesn't change anything. Feb 28 at 1:52
  • Did you recompile? Also, try changing it to 100.0 * letters/words
    – Cliff B
    Feb 28 at 1:54
  • Yes, I tried other combinations as well, there wasn't a problem with the index function. I posted a question thinking there could be a mistake but after I found out the problem was in counting functions. Thank you all. Feb 28 at 2:22
0

After searching and fixing and debugging every part one by one, I found the problem. For anyone who is facing the same issue try looking at your method of how you count letters, words, sentences there is a high probability is a mistake in the counting method. At least it was in my case.

1
  • Glad you found it. No way we could have discovered it without seeing the code. Happy coding!
    – Cliff B
    Feb 28 at 2:32

You must log in to answer this question.

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