Sunday, November 2, 2014

Chapter 6 Programming Language Concepts R Sebesta

Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #6 dari Chapter 6 Programming Language Concepts R Sebesta


Review Questions

6. What are the advantages of user-defined enumeration types?
*User-defined enumeration types are most useful when a data type can take one of a small, discrete set of values, each of which have some meaning that is not a number. A favorite example in textbooks is a type for the suit of a playing card. There are four options: SPADE, CLUB, HEART, DIAMOND, and so we would make an enumeration type with these four entries. The main advantages are efficiency in storage (compared to e.g. storing these values as strings) and more readable code.

7. In what ways are the user-defined enumeration types of C# more reliable than those of C++?
*Since C# enumeration types are not coerced into integer types, as the C++ does so, it's more reliable.

8. What are the design issues for arrays?
*-What types are legal for subscripts?
-Are subscripting expressions in elemnet refrence range checked?
-When are subscript ranges bound?
-When does allocation take place?
-What is the maximum number of subscripts?
-Can array objects be initialized?
-Are any kind of slices supported?

9. Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic, and heap-dynamic arrays. What are the advantages of each?
*Static array is an array which the subscript ranges are statically bound and storage allocation is static (before run-time). The advantage is efficiency (no dynamic allocation).

Fixed stack-dynamic array is an array which the subscript ranges are statically bound, but the allocation is done at declaration time. The advantage is space efficiency

Stack-dynamic array is an array which the subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time). The advantage is flexibility (the size of an array need not be known until the array is to be used).

Fixed heap-dynamic array is similar to fixed stack-dynamic, storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack). The advantage of fixed heap-dynamic array is flexibility (the array’s size always fits the problem).

Heap-dynamic array is an array which the binding of subscript ranges and storage allocation is dynamic and can change any number of times. The advantage is flexibility (arrays can grow or shrink during program execution).

10. What happens when a nonexistent element of an array is referenced in Perl?
*If an r-value is required, undef is returned. If an l-value is required, the array is extended, then the newly created but undefined element is returned. No error is reported.



Problem Set

6. Explain all of the differences between Ada’s subtypes and derived types.
*Ada’s subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. While Ada’s derived type is a completely separate type that has the same characteristics as its base type. We can’t mix operands of a derived type with operands of the base type.

7. What significant justification is there for the -> operator in C and C++?
*The only justification for -> operator in C and C++ is writability. It is slightly easier to write p->q than (*p).q.

8. What are all of the differences between the enumeration types of C++ and those of Java?
*In C++, an enumeration is just a set of named, integral constants. Also, C++ will implicitly convert enum values to their integral equivalent. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration. Java will explicitly convert enum values to their integral equivalent.

9. The unions in C and C++ are separate from the records of those languages, rather than combined as they are in Ada. What are the advantages and disadvantages to these two choices?
*

Union separated from records
Union combined with records
Advantages
-Reduce developer overhead (less checking)
-Goes with the C++ design (C++ is not a weak strong typing language)
-Support type checking
-Goes with the Ada design (Ada is strong typing language)
Disadvantages
-No type checking of references
-May end with wrong or unexpected results
-Increase developer overhead
-Using more functions to resolve type compatibility

10. Multidimensional arrays can be stored in row major order, as in C++, or in column major order, as in Fortran. Develop the access functions for both of these arrangements for three-dimensional arrays.
*Let the subscript ranges of the three dimensions be named min(1), min(2), min(3), max(1), max(2), and max(3). Let the sizes of the subscript ranges be size(1), size(2), and size(3). Assume the element size is 1.
Row Major Order:
 location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((i-min(1))*size(3) + (j-min(2)))*size(2) + (k-min(3))
Column Major Order:
 location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((k-min(3))*size(1) + (j-min(2)))*size(2) + (i-min(1))

Chapter 5 Programming Language Concepts R Sebesta

Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #5 dari Chapter 5 Programming Language Concepts R Sebesta


Review Questions

6. What is the l-value of a variable? What is the r-value?
*The l-value of a variable is the address of that variable. An l-value represents a storage region's "locator" value.
The r-value of a variable is the data (value) of that variable. All l-values are r-values but not all r-values are l-values.

7. Define binding and binding time.
* A binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol.
Binding time is the time at which a binding takes place.

8. After language design and implementation [what are the four times bindings can take place in a program?]
*-compile time (bind a variable to a type in C or Java)
-link time
-load time (bind a C or C++ static variable to a memory cell)
-run time (bind a nonstatic local variable to a memory cell)

9. Define static binding and dynamic binding.
*Static binding is a binding which occurs before run time and remains unchanged throughout program execution.
Dynamic binding is a binding which occurs during execution or can change during execution of the program.

10. What are the advantages and disadvantages of implicit declarations?
*Advantages:
-Can make it easier for the programmer to write code, since he/she doesn’t have to also write the declarations
-Maintainability can be easier too, since the information about a variable’s type is not written down in a part of the program distant from where the variable is used
-Readability can be better since a reader can probably infer the variable’s name or its context


Disadvantage:
-Reliability will probably suffer since the programmer may not always realize the type that the compiler assigned a variable



Problem Set

6. Consider the following JavaScript skeletal program:
// The main program var x;
function sub1() {
  var x;
  function sub2() {
    . . .
  }
}
function sub3() {
  . . . 
}    Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
a. Assuming static scoping, in the following, which dec- laration of x is the correct one for a reference to x?
 i. sub1: sub1
ii. sub2: sub1
iii. sub3: main
b. Repeat part a, but assume dynamic scoping.
i. sub1: sub1
ii. sub2: sub1
iii. sub3: sub1

7. Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1?
var x; function sub1() {
  document.write("x = " + x + "<br />");
}
function sub2() {
  var x;
  x = 10;
  sub1();
}
x = 5;
sub2();

*Static scope: x=5
Dynamic scope: x=10

8. Consider the following JavaScript program:
var x, y, z; function sub1() {
  var a, y, z;
  function sub2() {
    var a, b, z;
    . . .
  }
  . . .
}
function sub3() {
  var a, x, w;
  . . .
}
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assum- ing static scoping is used.
*sub1: a(sub1) y(sub1) z(sub1) x(main) 
sub2: a(sub2) b(sub2) z(sub2) y(sub1) x(main) 
sub3: a(sub3) x(sub3) w(sub3) y(main) z(main)

9. Consider the following Python program:
x = 1; y = 3;
z = 5;
def sub1():
  a = 7;
  y = 9;
  z = 11;
  . . .
def sub2():
  global x;
  a = 13;
  x = 15;
  w = 17;
  . . .
  def sub3():
    nonlocal a;
    a = 19;
    b = 21;
    z = 23;
    . . .
. . .
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assum- ing static scoping is used.
* Variable           Where Declared

In sub1:
a                     sub1
y                     sub1
z                     sub1
x                     main

In sub2:
a                     sub2
x                     sub2
w                    sub2
y                     main
z                     main

In sub3:
a                     sub3
b                     sub3
z                     sub3
w                    sub2
x                     sub2
y                     main

10. Consider the following C program:
void fun(void) {  int a, b, c; /* definition 1 */
  . . .
  while (. . .) {
    int b, c, d; /*definition 2 */   
. . .  à1
    while (. . .) {
int c, d, e; /* definition 3 */   
 . . . à 2
     }  
 . . .  -à3
    } 
. . .  à4
}
For each of the four marked points in this function, list each visible vari- able, along with the number of the definition statement that defines it.
* Point 1: a:1 b:2 c:2 d:2
Point 2: a:1 b:2 c:3 d:3 e:3
Point 3: a:1 b:2 c:2 d:2
Point 4: a:1 b:1 c:1