I can understand why you're having trouble fixing this. The good news is that it's an easy fix once you know what to look at.
Here's a really, really big hint:
In c programming, what's the difference between regular division and integer division?????
Next hint: what are the precedent orders of calculations in a calculation with many operations?
If you can't sort it out, leave a comment after my answer. ;-)
[Edit] The issue lies here:
int index = rintf(0.0588 * (countL / countW * 100) - 0.296 * (countS / countW * 100) - 15.8);
The problem is that this line of code is doing integer division, not regular division!
Here's the difference. Integer division will do the calculation and truncate the result to produce an integer result. For example, in integer division, ( 8 / 3 ) = 2.
Now let's look at part of the line of code above:
0.0588 * (countL / countW * 100)
Consider the order that operations are done. The code is going to do the stuff inside the parentheses. Since it's all multiplication and "division", it will proceed from left to right. So, the first thing that's done is countL / countW
. Since both are integers, the system does integer division. Everything else is downhill from here.
There are a number of ways to force regular division. They all boil down to making sure there's a float in the operation. You could cast one of the operands as a float,
( (float) countL / countW ....)
or you could do something to insert a float into the calculation.
(100.0 * countL / countW)
In this case, note that it's 100.0, a float, and not 100, an integer. The ".0" makes the difference. Since 100.0 is a float, the calculation goes like this. 100.0 * countL is done first and the interim result is a float. That float is used in the division, forcing regular division, so the integer division is no longer happening and the problem goes away.
That's the story. The lesson is that anytime you're doing division, you need to be alert for integer division and whether you want it or not.