I'm working on an exercise that is intended to display items in a vector in quartiles based on the values of the items. The first quartile should display the largest numbers in the first quarter of the vector and so on. I found a way to do this using while loops, but I thought I could make the code cleaner and more efficient using a for loop.
Area of interest is specifically line 23 - 25. I checked the values of i and the value of size and they are correct but for some reason the math just isn't happening because when I check the value of pct and ipct, I get all zero's. (The end of line 26 is my last error check before posting)
Can anybody explain what I am doing wrong and why the values are zero?
hello again.
It looks like there is a situation similar to the one in your previous thread. On line 24, you have 3 variables, each with a different type. The problem is with the right side of the assignment; i and size are ints, and integer division is kinda funny sometimes.
i will always be less than size, so the expression i / size is 0.
(do you see why?) Therefore, pct = 0, and ipct = pct * 100 = 0 as well.
The first solution that comes to mind is casting i and size to doubles before the division, so that the result is not 0.