Scripting Languages CS 5142, Fall 2013 hw01


Reading Assignments

Concept Questions

hw01_1 Precedence and associativity

1a.
(2 points) What does the VBA expression 5 - 10 \ 3 * 3 + 5 evaluate to?
1b.
(4 points) Add parentheses to the expression so that it still produces the same value, but does not rely on precedence or associativity for disambiguation.
1c.
(4 points) Describe an experiment that demonstrates that the exponentiation operator, ^, in VBA is left-associative.

hw01_2 Gradual typing

2a.
(4 points) Add explicit type annotations to the following VBA program.
Function Product(Arr())
  Dim Result, I
  Result = 1
  For I = LBound(Arr) To UBound(Arr)
    Result = Result * Arr(I)
  Next I
  Product = Result
End Function
Sub Main_hw01_2()
  Dim A(2)
  A(0) = -2: A(1) = -1: A(2) = 3
  Debug.Print Product(A)
End Sub
2b.
(2 points) Why might it be useful to omit the types in the original version of the code?
2c.
(2 points) Why might it be useful to add types to some code after writing the initial version?
2d.
(2 points) Do you know any other programming language where you can omit the types initially, and add them later as needed?

Programming exercises

hw01_3 Adding arrays

(10 points) Write a subroutine AddArrays that adds two arrays. The parameters are the two arrays. After the subroutine returns, the first array should contain element-by-element sum, and the second array should be unmodified. For example, consider the following test driver:
Sub Main_hw01_3()
  Dim A(2), B(2)
  A(0) = 2: A(1) = 3: A(2) = 1
  B(0) = 3: B(1) = -2: B(2) = 1
  AddArrays A, B
  For I = LBound(A) To UBound(A)
    Debug.Print A(I)
  Next I
End Sub
The program should print 5, 1, and 2. If the array ranges are mis-matched, your subroutine should only add matching indices, and ignore the rest.

hw01_4 Checking matrix symmetry

(10 points) Write a function IsSymmetric that checks whether a two-dimensional array is a symmetric matrix. A matrix is symmetric if A(I,J)==A(J,I) for all I,J. You can assume that the two dimensions have the same index range. For example,
Sub Main_hw01_4()
  Dim A(1, 1) As Integer
  A(0, 0) = 2: A(0, 1) = 3
  A(1, 0) = 3: A(1, 1) = 4
  Debug.Print IsSymmetric(A)
  A(0, 1) = 0
  Debug.Print IsSymmetric(A)
End Sub
The program should print True, False.
http://www.cs.cornell.edu/Courses/cs5142/2013fa/hw01.html