Name : Vina Melinda
NIM : 1801380106
Untuk kali ini saya akan menjawab Assignment #9 Problem Set dan Review Questions dari Chapter 7 dari Sebesta.
Review Questions:
6. Q: What associativity rules are used by APL?
A: Associativity rules are used by APL: all operators have equal precedence and all operators associate right to left.
7. Q: What is the difference between the way operators are implemented in
C++ and Ruby?
C++ and Ruby?
A: All of the arithmetic, relational, and assignment operators, as well as array indexing, shifts, and bitwise logic operators, areimplemented as methods in Ruby
8. Q: Define functional side effect.
A: It is a side effect of a function. Occurs when the function changes either one of its parameters or a global variable.
9. Q: What is a coercion?
A: Coercion or Implicit type conversion, is an automatic type conversion by the compiler. Some languages allow, some require, compilers to provide coercion
10. Q: What is a conditional expression?
A: A conditional expression is a compound expression that contains a condition that is implicity converted to type bool in C++ (operand1), an expression to be evaluated if the condition evaluates to true (operand2), and an expression to be evaluated if the condition has the value false (operand3).
Problem Set:
6. Q: 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?
be included in other languages (that do not already have them)? Why or
why not?
A: Yes C should, because it will ease the increment or even decrement while we use in looping rather than manually by the assigning, and also by using that we can easily know that it is not operation, instead it is an increment or decrement which is usually used in repetition.
7. Q: Describe a situation in which the add operator in a programming language
would not be commutative.
would not be commutative.
A: In many programming languages, the operands of an addition are actually memory locations, and the result of an addition is stored in the same location as one of the operands, so the operation is not commutative. A+B may not give quite the same result as B+A, because of the storage location of the result.
8. Q: Describe a situation in which the add operator in a programming language
would not be associative.
would not be associative.
A: Consider the integer expression A + B + C. Suppose the values of A, B, and C are 20,000, 25,000, and -20,000, respectively. Further suppose that the machine has a maximum integer value of 32,767. If the first addition is computed first, it will result in overflow. If the second addition is done first, the whole expression can be correctly computed.
9. Q: 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
b. a * (b - 1) / c mod d
c. (a - b) / c & (d * e / a - 3)
d. -a or c = d and e
e. a > b xor c or d <= 17
f. -a + b
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
b. a * (b - 1) / c mod d
c. (a - b) / c & (d * e / a - 3)
d. -a or c = d and e
e. a > b xor c or d <= 17
f. -a + b
A:
(a) ( ( ( a * b )1 – 1 )2 + c )3
(b) ( ( ( a * ( b – 1 )1 )2 / c )3 mod d )4
(c) ( ( ( a – b )1 / c )2 & ( ( ( d * e )3 / a )4 – 3 )5 )6
(d) ( ( ( – a )1 or ( c = d )2 )3 and e )4
(e) ( ( a > b )1 xor ( c or ( d <= 17 )2 )3 )4
(f) ( – ( a + b )1 )2
(a) ( ( ( a * b )1 – 1 )2 + c )3
(b) ( ( ( a * ( b – 1 )1 )2 / c )3 mod d )4
(c) ( ( ( a – b )1 / c )2 & ( ( ( d * e )3 / a )4 – 3 )5 )6
(d) ( ( ( – a )1 or ( c = d )2 )3 and e )4
(e) ( ( a > b )1 xor ( c or ( d <= 17 )2 )3 )4
(f) ( – ( a + b )1 )2
10. Q: 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.
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
(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