UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help |

1. a + b - c | 2. a + b * c | 3. c / b | 4. c % b | 5. b % c |

6. g / f | 7. g / c | 8. 12 / f | 9. 4 / 3 * 3.14159 | 10. 12 - 18 % b / c + a |

11. (5 + b / c) % 2 | 12. 5 * 3 % 7 + 8 / 3 - 6 | 13. (14 + 5) % 5 - 6 / 5.0 | 14. f / (10 % c) | 15. 20 * a + (g - c) |

2 + 4 - 6 = 6 - 6 = 0

6 / 4 = 1 with remainder of 2, so the value of 6 % 4 is 2.

Since 4 and 3 are both integers, 4 / 3 is integer division and the result is 1. Then 1 * 3.14159 is an integer times a floating point so the result is floating point 3.14159.

Since % and / have higher precedence than - and +, they are performed first, from left to right.

First 18 % 4 is the remainder after dividing so 18 % 4 = 2. So we now have 12 - 2 / 6 + 2. Since 2 and 6 are integers, integer division id performed and 2 /6 = 0, which simplifies the problem to 12 - 0 + 2 = 14.

Starting inside the parenthesis, we first perform the integer division 4 / 6, which is 0. Since 5 + 0 = 0, the problem simplifies to 5 % 2. The % operator is the remainder after divsion. 5 / 2 is 2 with remainder of 1, so 5 % 2 = 1.

Remeber that doing x % 2 tells us if x is odd or even. If x is odd, the result is 1. If x is even the result is 0.

Since there are no parenethesis, and /, * and % have higher precedence than + and -, let's put parenthesis to indicate the order of operations:

( (5 * 3) % 7) + (8 / 3) - 6

In the first set of parentheis, 5 * 3 = 15 and then 15 % 7 = 1.

In the second set of parenthesis, 8 / 3 = 2 (integer division).

So the problem reduces to 1 + 2 - 6 = -3.

Parenthesis first, so 14 + 5 = 19, yielding 19 % 5 - 6 / 5.0

% and / have higher precedence than -, so first 19 % 5 = 4.

Second, 6 / 5.0 is mixed division (integer and floating point), so it becomes all floating point, so 6.0 / 5.0 = 1.2.

Our problem has reduced to 4 - 1.2 which is 2.8

Inside the parenthesis, 10 % 6 = 4. Then 5.0 / 4 is mixed division (integer and float), so it becomes all float and

the result is of type float..... 5.0 / 4.0 = 1.25

Inside the parenthesis, 8.0 - 6 becomes 8.0 - 6.0 which is 2.0

Our problem is then 20 * 2 + 2.0

Since * is higher precedence than +, we first multiply, then add.

20 * 2 = 40.... 40 + 2.0 = 42.0

CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:14:20 EDT