Combined Operator Question

My professor wants us to use the combined operator for adding y * 5 to x. Wouldnt it just be

x += (y*5);

Also this one : Divide profit by shares minus 1000

profit /= (shares) - 1000;
Last edited on
You are right for the first one but the second question isn't clear to me , is it "Divide profit by share and subtract 1000 from the resultant" or "Divide profit by (shares - 1000) ".If it's the second then you did correct.
I believe its Divide profit by share and subtract 1000 from the resultant. Thats the way it is worded online so its also unclear to me
If you need to subtract 1000 after, you'd have to do some algebraic manipulation to get that.

1
2
//math:
a/b - c == (a - bc)/b


Kinda pointless, in actual code just make it a 2-liner...


Edit: Oh wait nevermind it's not possible in one line by the looks of it, since you'd have to have profit be by itself on the numerator to work.
Just make it a two liner
1
2
profit /= shares;
profit -= 1000;
Last edited on
I think It's possible in one line ,
 
(profit/=shares)-=1000;

edit :This probably falls under undefined behavior see post below.
I don't know why I had in my mind that there would be a sequence point at the end of the bracket.

edit 2 : It isn't undefined ... I ran it ... it is clearly defined ...
Last edited on
It's possible in one line ,
(profit/=shares)-=1000;


I believe the behavior of this statement is undefined because you are making two changes to the same variable without an intervening sequence point.

Divide profit by shares minus 1000

I'd interpret this as profit /= (shares-1000); If it were the other way then you'd say something like "divide profit by shares and subtract 1000"

In any case, it's clear that it's ambiguous. Ask the prof what he/she means.
Topic archived. No new replies allowed.