Pages

Sunday, 14 April 2013

Chapter 8

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (8 numbers, quite randomly in Review Qustion and 3 or 4 numbers in Problem Set) in Chapter 8 in "Concept of Programming Language" book.


Review Question

1. What is the definition of control structure?
Control structure is a control statement and the collection of the statements whose execution it controls.

6. What is unusual about Phyton's design of compound statements?
Phyton uses indentation to specify compound statements.

7. Under what circumtances must an F# selector have an else clause?
If the "if" expression does return a value, it must have an else clause.

9. What are the design issues for multiple-selection statements?

  • What is the form and type of the expression that controls the selection?
  • How are the selectable segments specified?
  • Is execution flow through the structure restricted to include just a single selectable segement?
  • How are the case of values specified?
  • How should unrepresented selector expression values be handled, if at all?

14. What are the design issues for all iterative control statements?

  • How is the iteration controlled?
  • Where should the control mechanism appear in the loop statement?


15. What are the design issues for counter-controlled loop statements?

  • What are the type and scope of the loop variable?
  • Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect loop control?
  • Should the loop parameters be evaluated only once, or once for every iteration?

19. What does the range function in Phyton do?
range function is used for most simple counting loops in Phyton.

22. What is the main reason user-located loop control statements were invented?
In some situation, it is convenient for a programmer to choose a location for loop control other than the top bottom of the loop body.

Problem Set
2. Phyton uses indentation to specify compound statements. Give an example in support of this statement.
If x < y :
  x = y
  print "This is in compound statement"

6. In C language, a control statement can be implemented using a nested if else, as well as by using switch statement. Make a list of differences in the implementation of a nested if else and a switch statement. Also suggest under what circumstances the id else control stricture is used and in which condition the switch case is used.

If else structure
  • Used when the variable that checked have some range, like 0-50, 51-64, A-E, etc.
  • Used when the logic expression to compare the variable is many.
  • When there's only if statement without else, when the variable isn't valid at the logic expression there, the compound statements is skipped.
Switch case structure
  • Used when the variable that checked is exact value, like 1, 40, A, C, etc.
  • Used when the logic expression is just one but have a different output value each option.
  • When there's switch statement without default, when the variable isn't valid at any values there, the compound statements is skipped.


18. State one of the main legitimate needs for gotos.
Control flow transfers out of a loop where there's a large amount of lexical context that the loop required.

Saturday, 13 April 2013

Chapter 7


An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (7 numbers, quite randomly in Review Qustion and 5 or 6 numbers in Problem Set) in Chapter 7 in "Concept of Programming Language" book.


Review Question
2. What is a ternary operator?
It means, the operator have three operands.

3. What is a prefix operator?
It means the operators precede their operands.

6. What associativity rules are used by APL?
Right to left for all operators.

7. What is the difference between the way operators are implemented in C++ and Ruby?
The difference is in Ruby, all the arithmetic, relational, and assignment operator, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods while in C, it doesn't.

8. Define functional side effect.
It is a side effect of a function. Occurs when the function changes either one of its parameters or a global variable.

18. What is short-circuit evaluation?
A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators.

24. What two languages include multiple assignments?
Perl and Ruby.

Problem Set
1. When might you want the compiler to ignore type differences in an expression?
When I want to evaluate a string as a number and vice versa.

13. Let the function fun be defined as

int fun(int *k){
  *k += 4;
  return 3 * (*k) - 1;
}

Suppose fun is used in a program as follows:

void main(){
  int i = 10, j = 10, sum1, sum2;
  sum1 = (i/2) + fun (&i);
  sum2 = fun(&j) + (j/2);
}

What are the values of sum1 and sum2
a. if the operands in the expression are evaluated left to right?
b. if the operands in the expression are evaluated right to left?

a. sum1 = (10/2) + (3 * 10 - 1) = 5 + 29 = 34
    sum2 = (3 * 14 - 1) + (14/2) = 41 + 7 = 49


b. sum1 = (14/2) + (3 * 14 - 1) = 7 + 41 = 49
    sum2 = (3 * 10 - 1) + (10/2) = 29 + 5 = 34


15. Explain why it is difficult to eliminate functional side effects in C?
In C, it only have one functions, meaning that all subprograms return only one value. To eliminate the side effects and still provide subprograms that return more than one value, the values need to be placed in a struct and the struct returned. Access to global in functions would also have to be disallowed.


18. Should an optimizing compiler for C or C++ be allowed to change the order of subexpressions in a Boolean expression? Why or why not?
Because of short-circuit evaluation, the order of subexpressions is important.

20. 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
a. Operands are evaluated left to right.
b. Operands are evaluated right to left.


a. x = 3 + 4 = 7

b. x = 8 + 4 = 12

Sunday, 7 April 2013

Chapter 6


An assignment from Mr. Tri Djoko Wahyono (as always). As what the title said, it is the answer for some question (16 numbers, quite randomly in Review Qustion and 5 or maybe 6 numbers in Problem Set) in Chapter 3 in "Concept of Programming Language" book.


Review Question
1. What is a descriptor?
A descriptor is the collection of the attribute of a variable.

2. 
What are the advantages and disadvantages of decimal data types?
Decimal types have the advantages of being able to precisely store decimal values, at least those within a restricted range which cannot  be done with floating-point.
The disadvantages of decimal types are that the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful.

3. What are the design issues for character string types?

  • Should strings be simply a special kind of character array or a primitive type?
  • Should strings have static or dynamic?

4. Describe the three string length  options.
The first option, when the length can be static and set when the string is created, it is called a static length string.
The second option is to allow strings to have varying length up to a declared and fixed maximum set by the variable''s definition, which is called a limited dynamic length string.
The third option is to allow strings to have varying length with no maximum, which is called a dynamic length string.

5. Define ordinal, enumeration, and subrange types.

  • An ordinal type is one in which the range if possible values can be easily associated with the set of positive integers.
  • An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated, in definition.
  • A subrange type is a contiguous subsequence of an ordinal type.

8. What are the design issues for arrays?

  • What types are legal for subscripts?
  • Are subscripting expressions in element references range checked?
  • When are subscript ranges bound?
  • When does array allocation take place?
  • Are ragged or rectangular multidimensioned arrays allowed, or both?
  • Can arrays be intialized when they have their storage allocated?
  • What kinds of slices are allowed, if any?

13. What languages support array slices with stepsizes?
Phyton, Perl, and Ruby.
21. What is the purpose of level numbers in COBOL records?
Level numbers indicate by their relative values the hierarchical structure of the record.

24. Are the tuples of Phyton mutable?
No, Phyton includes an immutable tuple type.

35. What are the design issues for pointer types?

  • What are the scope and lifetime of a pointer variable?
  • What is the lifetime of a heap-dynamic variable (the  value a pointer references)?
  • Are pointers restricted as to the type of value to which they can point?
  • Are pointers used for dynamic storage management, indirect addressing, or both?
  • Should the language support pointer types, reference types, or both?


43. What is a compatible type?
A compatible type is one that either as legal for the operator or is allowed under language rules to be implicitly converted by compiler-generated code (or the interpreter) to a legal type.


44. Define type error.
A type error is the application of an operator to an operand of an inappropriate type.


45. Define strongly typed?
Strongly typed is a type of language which type errors are always detected.


49. Why are C and C++ bot strongly typed?
C and C++ are not strongly typed languages because both include union types, which are not type checked.


52. What is the primary advantage of name type equivalence?
Name type equivalence is easy to implement.



53. What is the primary disadvantage of structure type equivalence?
Structure type equivalent is more difficult to implement.





Problem Set
2. How are negative integers stored in memory?
A negative integer could be stored in a bit that indicate that it is a negative number, but not lend itself to computer arithmetic.

7. Compare the pointer and reference type variable in C++.
In C++, pointers can be used in the same ways as addresses are used in assembly languages. it is similar to a reference type, with one important and fundamental difference: A pointer refers to an object or a value in memory.

19. Any type defined with typedef is type equivalent to its parent type. How does the use of typedef differ in C and C++?
Any type defines with typedef is type equivalent to its parent type. One exception to C using name type equivalence for structures, enumeration, and files, in which case structural type equivalence is used. C++ is like C except there is no exception for structures and unions defined in different files.

21. In what way is dynamic type checking better than static type checking?
It is better to detect errors at compile time than at run time, because the earlier correction is usually less costly. The penalty for static checking is reduced programmer flexibility. Fewer shortcut and tricks are possible.

22. Explain how the dangling-pointer problem can be removed using the locks-and-keys approach.
The best solution to the dangling-pointer problem is to take deallocation of heap-dynamic variables out of the hands of programmers. If programs cannot explicitly deallocate heap-dynamic variables, there will be no dangling-pointers.

Chapter 5


An assignment from Mr. Tri Djoko Wahyono (as always). As what the title said, it is the answer for some question (6 numbers, quite randomly in Review Qustion and 3 numbers in Problem Set) in Chapter 5 in "Concept of Programming Language" book.


Review Question
1. What are the design issues for names?
  • Are names case sensitive?
  • Are the special words of the language reserved words or keywords?
2. What is the potential danger of case-sensitive names?
The potential danger of case-sensitive names is its readability, because names that look very similar in fact denote different entities.

3. In what way are reserved words better than keywords?
Reserved words are better than keywords because the ability to redefine keywords can be confusing.

7. Define binding and binding time?
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.

9. Define static binding and dynamic binding.
It is a static binding if it first occurs before run time begins and remains unchanged throughout program execution. If the binding first occurs during run time or can change in the course of program exception, it is called dynamic binding.

23. What are the advantages of named constants?
Named constants are useful as aids to readability and program reliability.

Problem Set
1. Decide which of the following identifier names is valid in C language. Support your decision.

a.  _Student
b.  int
c.  Student
d.  123Student
e.  Student123

For (a), (b), and (d), are an invalid variable names, because in C language, a variable name can't :
> Begin with special characters (@,(,),_, etc.).

> Same as reserved words or keyword (such int, char, main, etc.).
> Begin with number.

7. Assume the following JavaScript program was interpreted using static-scooping rules. What value of x is displayed in function sub1? Under dynamic-scooping rules, what value of x is displayed in function sub1?
Under static-scooping rules, the value of x which is displayed is 5.

Under static-scooping rules, the value of x which is displayed is 10.


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 variable, along with the number of the definition statement that defines it.

Point 1 : a, b, and c of definition 1.
Point 2 : a of definition 1 - b of definition 2 - c, d, and e of definition 3.
Point 3 : a of definition 1 - b, c, and d of definition 2.
Point 4 : a, b, and c of definition 1.