#Please help me, Code Doesnt follow MDAS rule
1 messages · Page 1 of 1 (latest)
Helper please have a look, thanks.
When i input 2+7x3÷5×0.5
It should be equal to 4.1
But instead it says 2.7
Idk what to correct in the code for it to do MDAS correctly, im a first year student
have u tried checking if it follows the order of pemdas
No the problem is that it goea from left to right calculation, it doesnt follow the mdas rule
It goes from 2+7 then x3 then ÷5 then x 0.5 which is wrong, idk what to do, ive searched for everything
can you post what you're typing in the console & what result you're getting exactly? it might help
Oh you mean PEMDAS
Yes
well, 1 strategy
do it MDAS
explicitly
so do one scan where you do the multiplication
another where you do division
another with addition
etc.
Like seperately?
yes
But my teacher wants all at the same time
from the perspective of the user it will all be at the same time
you just scan left to right
so
2 + 7 x 3 / 5 x 0.5
just do the division and multiplication left to right, leaving addition alone
2 + 21 / 5 x 0.5
2 + 4.25 x 0.5
2 + 2.0125
4.0125
just don't do addition or subtraction until you are done with multiplication and division
Will my teacher accept it though
i don't care