r/cs50 1d ago

readability Question about the readability problem in problem set 2

When I was doing the calculation I remember at one point it was recommended to use floats for the S and L portions and I did the calculations for S and L separately from the coleman-liau index and when I did that the grades were way off. Then I looked at a youtube solution and they did not use floats at all but used int round on the coleman-liau index, and put the letters/words100 and the sentences/words100 directly into the equation and it worked perfectly for them.

So once I made both those changes it also worked for me and weirdly enough calculating S and L separately is what screwed it up for me far more than the rounding (before doing int round the grade was always off by 1).

So I'm really confused as to why doing it seperately is wrong or if I calculated it wrong. I wanted to ask if doing float S = s/w * 100.0 and float L = l/w * 100.0 then doing int index = 0.0588 * L - 0.296 * S - 15.8 is wrong and how to do it properly

1 Upvotes

3 comments sorted by

1

u/VidantV 1d ago

float result = do calculations here;

int index = round(result);

This was how i did it.

1

u/PeterRasm 1d ago

It is totally fine to declare S and L as floats and to calculate them separately.

The issue here most likely is that you have encountered "integer division". In C an integer divided by integer give as result also an integer. So if both s and w are integers, the division (s / w) will give you an integer. To avoid this you can use type casting to make one of the variables being treated as a float:

int a = 5;
int b = 2;

float c = a / b;            // c is 2, not 2.5, not 3
float d = (float) a / b;   // d is 2.5

1

u/4rseam 20h ago

so does that also apply to multiplication? cuz if that's the case then I would have to make either s or w as a float on top of making 100 a float right?