Homework 2
CS 3411 - Programming Language Concepts - Fall '98
Due: 1:30pm Thursday 5 November (Follow
handin instructions on the class Web page.)
Got Questions? Try office hours, email or the class
newsgroup.
-
Develop access formulas for 0-based 3-d matrices stored in 1) row-major
and 2) column-major order. In other words, given a three-dimensional array
A, give a formula for computing the address of element A[i, j, k]. Assume
that the array is stored in memory starting at location BASE and that the
array consists of elements of size ESIZE (bytes).
-
Consider the following C program:
int fun( int *i ) {
*i +=
5;
return
4;
}
void main() {
int x
= 3;
x = x
+ fun(&x);
}
What is the value of x after the assignment statement in main,
assuming:
1) operands are evaluated left to right.
2) operands are evaluated right to left.
-
Design a scheme to allow record fields to be accessed using indices
in addition to field names. Make sure your scheme allows you to uniquely
refer to subfields in nested records and that record field "indices" can
be computed at run-time using arbitrary expression (like array indices).
(Hint: You may need to keep around additional "bookkeeping" information
at run-time to make this possible.)
-
Describe a notation for referring to arbitrary "slices" of two-dimensional
arrays. You may describe a notation used in an existing language or
create your own notation. Make sure that your notation allows you to describe
"subregions" and not just entire rows and columns.
-
Tagged-variants (also known as discriminated unions) attempt
to make unions typesafe by introducing a tag field that should always indicate
which variant was last assigned. Pascal supports a tag field but it is
up to the programmer to ensure that the tag has the correct value. Ada
has a mechanism for forcing the tag and variant to always correspond. Devise
a syntax that keeps tags and variants consistent. Can you scheme be statically
type-checked or does it require dynamic type-checking. Explain.
Here is an example of an Ada variant record:
type NODE (TAGE : BOOLEAN) is
record
case
TAG is
when true => COUNT : INTEGER;
when false => SUM : FLOAT;
end
case;
end record;
-
Some languages (SIMSCRIPT, Algol) have chosen to implement 2-d arrays
using a 1-d array of pointers, each of which points to a 1-d array
of elements. What are the advantages and disadvantages of this scheme compared
to the more standard row-major and column-major representations?