Q6 : Should C’s single-operand assignment forms (for example, ++count) be included in other languages (that do not already have them)? Why or why not?
A : Yes C should, because those assigning operations make operation simpler and easy to know if that operation is assigning operation. And it will ease the increment or even decrement while we use in looping.
Q7 : Describe a situation in which the add operator in a programming language would not be commutative.
A : If the add operator for a language is also used to concatenate strings, it’s quite apparent that it would not be commutative.
For example:
“abc” + “def” = “abcdef”
“def” + “abc” = “defabc”
These two strings are obviously not equal, so the addition operator is not commutative.
Q8 : Describe a situation in which the add operator in a programming language would not be associative.
A : It is not associative when it includes the other operator with higher precedence like the multiplication and division.
Q9 : Assume the following rules of associativity and precedence for expressions:
Precedence
Highest *, /, not
+, –, &, mod
– (unary)
=, /=, <, <=, >=, >
And
Lowest or, xor
Associativity Left to right
Show the order of evaluation of the following expressions by parenthesizing all subexpressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression
a + b * c + d
the order of evaluation would be represented as
((a + (b * c)1)2 + d)3
a. a * b - 1 + c ((( a * b )1 - 1)2 + c )3
b. a * (b - 1) / c mod d ((( a * ( b - 1 )1 )2 / c )3 mod d )4
c. (a - b) / c & (d * e / a - 3) (((a - b)1 / c)2 & ((d * e)3 / a)4 - 3)5)6
d. -a or c = d and e (( -a )1 or ( ( c = d )2 and e )3 )4
e. a > b xor c or d <= 17 (((a > b)1 xor c)3 or (d <= 17)2 )4
f. -a + b (–( a + b )1 )2
Q10 : Show the order of evaluation of the expressions of Problem 9, assuming that there are no precedence rules and all operators associate right to left.
A :
a. ( a * ( b – ( 1 + c )1 )2 )3
b. ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
c. ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
d. ( – ( a or ( c = ( d and e )1 )2 )3 )4
e. ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
f. ( – ( a + b )1 )2
No comments:
Post a Comment