why isn't this working?

Why does this return 0?
 
float ac=(79+91+100+43)/400;
Probably something else in your code that makes it zero. Perhaps casting to an int?
There is integer division here:
49 + 91 + 100 + 43 = 283. (int)
int → 283/400 ← int = (int) 0.

You need to perform actual floating point division:
float ac=(79+91+100+43)/400.0f;
Weird that I never knew also... But I found the solution is this:
float ac=(float)(79+91+100+43)/400;
using C-style casts isn't really recomended. Unless you're writing in C of course :)
http://en.cppreference.com/w/cpp/language/static_cast
Last edited on
Topic archived. No new replies allowed.