1

I am currently stuck at a problem for a few days to find out why it's happening.

I am writing a program to find out number of letters, words, and sentences. Using these values, I am using Coleman-Liau index to find out the article's readability level.

I think I have a pretty solid program with the help online but I don't know why some of the errors are happening in the link below. https://submit.cs50.io/check50/35e7ae7e4b968d6b06a8152f61a0e83381b376ab

For context, please see https://cs50.harvard.edu/college/psets/2/readability/

Here's the code that I am having trouble with:

[removed solution code per Academic Honesty guidelines]

// calculate Coleman-Liau index

    int grade = 0.0588 * (100 * letter / word) - 0.296 * (100 * sentence / word) - 15.8;

// debugger

    printf("Letters: %i\n Words: %i\n Sentences: %i\n", letter, word, sentence);

Could anyone help me find out what the problem is? as far as I can see, I can't see why some of the results are skewed.

Thanks!

2

The problem is integer division and the affect it has on the final calculation.

Your (100 * letter / word) is going to evaluate as an integer, as well as your (100 * sentence / word) so both will be truncated, which means your final answer will be wrong. Then, you are storing that final answer in an int, which again will be truncated. Make sure that all of your calculations use floats and then round your final result before storing in an int.

0

have you uploaded to GitHub to see your code? Thanks

0

Why it keeps rounding off even I already use float?

1
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – Cliff B Aug 25 '20 at 2:43

You must log in to answer this question.

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