CSC 407 Homework #2 (Chapter 2)



CSC 407/507 Homework #2 answer key

1) Note: compilation time means the time when the method for the given code is compiled (unless otherwise noted).

• Person is semantically bound at the time the Person class is compiled and bound to memory at the time when this program is loaded (via the import statement)

• p is bound to its type at compilation time and bound to memory when the method containing this instruction is executed (p is pushed onto the run-time stack)

• the memory location for the new Person (what p points too) is allocated in memory when this instruction is executed

• “Frank” and ‘M’ are bound to memory at compilation time because they are literal values and the compiler will place these directly in the machine instructions, but are bound to their type/meaning at language design time

• =, new, (, ), ; are all bound to their meaning at language design time

• the variable age was bound to its type at compile time and the type for spouse was bound when the spouse’s class’ type was compiled. These two variables are bound to memory at the time this method is executed (pushed onto the run-time stack) but the memory that spouse points to is bound to memory in the previous instruction when that instruction (which instantiates spouse) was executed

Note that since age is passed as a parameter, the original parameter was bound at the time that the method that called this method was itself called.

2) #9:

In Sub1: A, Y, Z are declared in Sub1, X is declared in Main

In Sub2: A, X, W are declared in Sub1, Y, Z in Main

In Sub3: A, B, Z are declared in Sub2, X, W in Sub2, Y in Main

#12

a. A, X, W from Sub3, B, Z from Sub2, Y from Sub1

b. A, X, W from Sub3, Y, Z from Sub1

c. A, Y, Z from Sub1, X, W from Sub3, B from Sub2

d. A, Y, Z from Sub1, X, W from Sub3

e. A, B, Z from Sub2, X, W from Sub3, Y from Sub1

f. A, Y, Z from Sub1, B from Sub2, X, W from Sub3

3)

a. a[i][j][k] = a + (20*15*i + 15*j + k) * 2

b. array[i, j] = array + (20*(i-1) +(j-21)) * 8

c. iarray(i, j) = iarray + (50 * (j - 1) + (i – 1)) * 4 remember, column major order

4)

a. (a + ((b / (-c)3)1 * d)2)4 The unary – is lower then / or * and at the same level as +

this doesn’t make much sense though!

b. ((((not a)1 and b)3 or (c – d)2)4 xor e)5

c. (((a + b)2 b)4)5

5)

a. Can’t be done because the FORTRAN I DO loop can only use a constant for the step size increment

b. For i := 1, i*2, while (i j := 2 * k – 1;

when 3, 5 => j := 3 * k + 1;

when 4 => j := 4 * k – 1;

when 6, 7, 8 => j := k – 2;

c. (cond ((or (= k 1) (= k 2)) (setf j (- (* 2 k) 1)))

((or (= k 3) (= k 5)) (setf j (+ (* 3 k) 1)))

((= k 4) (setf j (- (* 4 k) 1)))

((or (= k 6) (= k 7) (= k 8)) (setf j (- k 2))))

NOTE: there is a case statement in Lisp, but this is easier

d. If k == 1 or k == 2:

j = 2 * k – 1

elif k == 3 or k == 5:

j = 3 * k + 1

elif k == 4:

j = 4 * k – 1

elif k == 6 or k == 7 or k == 8:

j = k – 2

NOTE: there is no case statement in Python so we have to use nested if-elif

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download