3

I have written a solution for readability from pset2 but I'm having problems with one of the points:

:( handles single sentence with multiple words expected "Grade 7\n", not "Grade 8\n".

So I enter the suggested text from check50 and get the following:

Text: In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
Index: 7.535600
Grade 8

I'm adding the index just to explain what I want to say. I'm using round(index) to get the rounded value and it rounds 7.5356 to 8 as it normally would. But check50 doesn't agree with this saying that the grade should be 7 not 8. For this reason I want to round the number based on the second digit after the decimal point which is 3. If we multiply it by 10 it becomes 75.3 and if we round it, it becomes 75. I tried: the following but it's not working because it rounds every other value to the smallest number, like 7.77 becomes 7.

int grade = (int)round(index*10)/10;

1 Answer 1

3

Your problem isn't that round(7.535600) rounds to 8 (It should, and does), but that 7.535600 isn't the correct index value.

For that text, I'd expect these values:

96 letter(s)
23 word(s)
1 sentence(s)
L: 417.391296 S: 4.347826 Score: 7.455652 
Grade 7

Perhaps you are running into problems in your intermediate calculations. Have you made sure that you aren't doing any "integer division"? In C, if you divide 2 integers, you will get an integer (ie, a truncated result). Might that be it? If at least one of the operands is a float, then you'll get floating point division.

2
  • ok, in case sb else someday has the same issue i'm writing what was wrong. I had int l, s and w and the formula to calculate the average: float lAvg = l * 100 / w; which caused the miscalculation. I changed it to: float lAvg = (float)l * 100 / (float)w; and it is fine. Thank you for helping me realize it!
    – Ada
    Commented Feb 5, 2020 at 0:50
  • thanks! that was helpful Commented Aug 7, 2020 at 13:27

You must log in to answer this question.

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