Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   The order of precedence MDAS (http://www.go4expert.com/forums/precedence-mdas-t15898/)

Player 21Jan2009 23:30

The order of precedence MDAS
 
Hi all.

I have been here before but not for a while. I hope my question is in the right section :). I am new to learning C and seem to be picking it up and enjoying it. I am a little confused about how the order of precedence works. I have wrote the following simple program but can't workout how the compiler gets it answer. I have shown my workings using the MDAS rule but i'm stuck. Sorry to all the professionals out there at what may seem a dumb question but i really want to understand everything i'm learning.

Thanks for reading :)
Code:

#include<stdio.h>
int main()
{
    printf("Answer=%d\n",18/9-2+6*2);

    /*              18/9-2+6*2
                      18/9-2+12
                      2-2+12
                      2-14
                      my answer = -12
                      compiler = 12*/

    printf("Answer2=%d",12-32/2*8+10);

    /*              12-32/2*8+10
                      12-32/16+10
                      12-2+10
                      12-12
                      my answer = 0
                      compiler = -106*/
                   
    return(0);
}


xpi0t0s 22Jan2009 14:29

Re: The order of precedence MDAS
 
The order of multiple equal precedence operators is left to right, just as in maths. So 2-2+12 is not 2-(2+12) but (2-2)+12.

So the first is 18/9-2+6*2= 2-2+12= 0+12= 12.

Similarly 12-2+10=(12-2)+10, not 12-(2+10), and 32/2*8=(32/2)*8 not 32/(2*8), so 12-32/2*8+10= 12-16*8+10= 12-128+10= -116+10= -106.

So you seem to be working right-to-left for some reason. Some things in C work that way, but expression evaluation isn't one of them.

Player 22Jan2009 18:15

Re: The order of precedence MDAS
 
Quote:

Originally Posted by xpi0t0s (Post 41835)
The order of multiple equal precedence operators is left to right, just as in maths. So 2-2+12 is not 2-(2+12) but (2-2)+12.

So the first is 18/9-2+6*2= 2-2+12= 0+12= 12.

Similarly 12-2+10=(12-2)+10, not 12-(2+10), and 32/2*8=(32/2)*8 not 32/(2*8), so 12-32/2*8+10= 12-16*8+10= 12-128+10= -116+10= -106.

So you seem to be working right-to-left for some reason. Some things in C work that way, but expression evaluation isn't one of them.

Thanks you very much for your answer. This has made it a lot clearer. I was looking at the following and i guess i miss understood :). I'm sure i will be back with more questions in the not too near future.

Thanks again.

xpi0t0s 22Jan2009 18:29

Re: The order of precedence MDAS
 
You didn't get right-to-left from that page, because is presents left to right evaluation: 100+10-20 -> 110-20, not 100-10. Also it shows 5*2 evaluated before 60/3, again in left to right order.

Player 22Jan2009 18:32

Re: The order of precedence MDAS
 
Quote:

Originally Posted by xpi0t0s (Post 41837)
You didn't get right-to-left from that page, because is presents left to right evaluation: 100+10-20 -> 110-20, not 100-10. Also it shows 5*2 evaluated before 60/3, again in left to right order.

I have miss understood that then too. The important bit is that i now understand it from your explination. I have tested myself with long random sums and i'm getting it right everytime now. Again, many thanks for taking the time to explain.:)

shabbir 22Jan2009 19:09

Re: The order of precedence MDAS
 
Quote:

Originally Posted by Player (Post 41836)
Thanks you very much for your answer. This has made it a lot clearer. I was looking at the following and i guess i miss understood :). I'm sure i will be back with more questions in the not too near future.

Thanks again.

I removed the attachment from this post as I did not see anything related to it in the post

xpi0t0s 22Jan2009 20:20

Re: The order of precedence MDAS
 
"I was looking at the following" referred to the attachment.

Player 22Jan2009 20:34

Re: The order of precedence MDAS
 
Quote:

Originally Posted by shabbir (Post 41840)
I removed the attachment from this post as I did not see anything related to it in the post

I was just trying to show how i had gone wrong. I was doing the following:

I was going to the multiflication first as in.

12-25/5+7*2
12-25/5+(14)

Then the division

12-(5)+14

Then the addition

12-(19)

Then the subtraction

12-19=-7

But now i know its.

12-25/5+7*2

12-5+14=21

Thanks again, i hope i have made myself clear as to where i was going wrong.

asadullah.ansari 23Jan2009 14:15

Re: The order of precedence MDAS
 
Quote:

Originally Posted by Player (Post 41843)
I was just trying to show how i had gone wrong. I was doing the following:

I was going to the multiflication first as in.
(INLINE) : You can't go for multiplication first as xpi0t0s already told you that on same precedence level operators you have to evaluate from left to right.
12-25/5+7*2
(INLINE): Now in this expression, * and / are same precedence level which precedence are more than same level oprator + , - .
Now evaluate from left to right on same level * and /
=> 12-5+7*2 (/ is on the left)
=> 12-5+14 ( now operate on *)
Now + and - are the same precedence level but remember one thing "evaluation will be from left to right"
=>12-5+14
=>7+14
=>21 (Answer)

12-25/5+(14)

Then the division

12-(5)+14

Then the addition

12-(19)

Then the subtraction

12-19=-7

But now i know its.

12-25/5+7*2

12-5+14=21
(Inline): It's right
Thanks again, i hope i have made myself clear as to where i was going wrong.

See inline message .....
Remeber one thing only " Capture the higher precendency operator it may be more than one If it is then go for operation on that operator which if in the left means operation should be from left to right on same precedence Operators"

Player 23Jan2009 15:32

Re: The order of precedence MDAS
 
Quote:

Originally Posted by asadullah.ansari (Post 41871)
See inline message .....
Remeber one thing only " Capture the higher precendency operator it may be more than one If it is then go for operation on that operator which if in the left means operation should be from left to right on same precedence Operators"

I was working multiplication first before division. I was working from left to right but skipping division if i came across it first. I didn't realise that M and D was the same precedence and A and S was also the same. I was putting them in order 1=M 2=D 3=A 4=S instead of 1=M&D 2=A&S.
I hope this has cleared this up, i certainly understand it now thanks to you guys :)


All times are GMT +5.5. The time now is 04:10.