Rabu, 26 November 2014

CHAPTER 7

Nama : Aditya Isnugraha
Class   : LM01
NIM    : 1801419606

Assignment from Tri Djoko Wahjono
CHAPTER 7

- REVIEW QUESTION

11. What is an overloaded operator? 
Answer :
 Operator that has different implementation depending on its arguments.


12. Define narrowing and widening conversions. 
Answer :
 Widening conversion is a conversion that converts a value to a type that can include least approximations of all values of the original type. Narrowing conversion is a conversion of data that might cause a loss of precision



13. In JavaScript, what is the difference between == and ===? 
Answer :
“==” operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It’s much similar Java’s equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other.


14. What is a mixed-mode expression? 
Answer :
 Mixed-mode expression: The expressions that allow them to design decisions concerning arithmetic expressions is whether an operator can have operands of different types.


15. What is referential transparency?
 Answer :
two expressions in the program that have the same value can be substituted for one
another anywhere in the program, without affecting the action of the program







 - PROBLEM SET

11. Write a BNF description of the precedence and associativity rules defined for the expressions in Problem 9. Assume the only operands are the names a,b,c,d, and e. 
Answer :
 <expr> → <expr> or <e1> | <expr> xor <e1> | <e1>

<e1> → <e1> and <e2> | <e2>

<e2> → <e2> = <e3> | <e2> /= <e3> | <e2> < <e3>

| <e2> <= <e3> | <e2> > <e3> | <e2> >= <e3> | <e3>

<e3> → <e4>

<e4> → <e4> + <e5> | <e4> – <e5> | <e4> & <e5> | <e4> mod <e5> | <e5>

<e5> → <e5> * <e6> | <e5> / <e6> | not <e5> | <e6>

<e6> → a | b | c | d | e | const | ( <expr> )


12. Using the grammar of Problem 11, draw parse trees for the expressions of Problem 9. 
Answer :



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 expressions are evaluated left to right? 
b. if the operands in the expressions are evaluated right to left?
Answer :
(a) (left -> right)

sum 1 = 46
sum 2 = 48

(b) (right -> left)

sum 1 = 48
sum 2 = 46


14. What is your primary argument against (or for) the operator precedence rules of APL? 
Answer :
 The operator precedence rules of the common imperative languages are nearly all the same, because they are based on those of mathematics.


15. Explain why it is difficult to eliminate functional side effects in C.
Answer :
One reason functional side effects would be difficult to remove from C is that all of C’s subprograms are functions, providing the ability of returning only a single data value.












Rabu, 05 November 2014

CHAPTER 6

Nama : Aditya Isnugraha
Class   : LM01
NIM    : 1801419606

Assignment from Tri Djoko Wahjono
CHAPTER 6

- REVIEW QUESTION

10. What happens when a nonexistent element of an array is referenced in Perl?
       Answer :
If you try to append non-existent elements from an array to another one, the initial array will grow as needed, even though the elements to append do not exist.


11. How does JavaScript support sparse arrays?
      Answer :
JavaScript objects are sparse, and arrays are just specialized objects with an auto-maintained length property (which is actually one larger than the largest index, not the number of defined elements) and some additional methods.


12. What languages support negative subscripts?
      Answer : Ruby and Lua support negative subscripts.



13. What languages support array slices with Stepsizes?
      Answer : Ruby, Python, Perl.



14. What array initialization feature is available in Ada that is not available in other common imperative languages?
       Answer :
Ada provides two mechanisms for initializing arrays in the declarations statements: by listing them in the order in which they are to be stored, or by directly assigning them to an index position using the => operator, which in Ada is called an arrow.


15. What is an aggregate constant?
       Answer : A parenthesized lists of values.

 
- PROBLEM SET


11. In the Burroughs Extended ALGOL language, matrices are stored as a single-dimensioned array of pointers to the rows of the matrix, which are treated as single-dimensioned arrays of values. What are the advantages and disadvantages of such a scheme?
      Answer :
The advantage of this scheme is that accesses that are done in order of the rows can be made very fast; once the pointer to a row is gotten, all of the elements of the row can be fetched very quickly. If, however, the elements of a matrix must be accessed in column order, these accesses will be much slower; every access requires the fetch of a row pointer and an address computation from there. Note that this access technique was devised to allow multidimensional array rows to be segments in a virtual storage management technique. Using this method, multidimensional arrays could be stored and manipulated that are much larger than the physical memory of the computer.


12. Analyze and write a comparison of C’s malloc and free functions with C++’s new and delete operators. Use safety as the primary consider- ation in the comparison.
      Answer :  new and delete are type safe (no need for casts), malloc and free are not. Also malloc returns a void* which then has to be cast to the appropriate pointer type. new returns the correct pointer type itself;  type safety.  malloc requires you to tell the number of bytes to allocate,  new figures it out itself.



13. Analyze and write a comparison of using C++ pointers and Java reference variables to refer to fixed heap-dynamic variables. Use safety and conve- nience as the primary considerations in the comparison.
      Answer :
 -In c and C++ pointer can be use the same way as addresses. This design offer
no solutions to the dangling pointer or lost heap-dynamic variable problems.
-Reference types, such as those in Java and C#, provide heap management
without the dangers of pointers.
so it's clear that Java has more safety for the heap-dynamic variables.


14. Write a short discussion of what was lost and what was gained in Java’s designers’ decision to not include the pointers of C++.
      Answer :
There's a copious amount of documentation out there on this subject so there's no reason to write at length. I should also point out that pointers are a feature of both C and C++.

It's obvious that preventing the use of pointers significantly bolstered the amount of security that programmers could get 'for free' given that issues like stack overruns, memory corruption and inadequate/incorrect freeing of memory (though this is more an attribute of GC than lack of pointers) were largely nullified.

Given that programmers tend to be kind of ornery and resistant to change, preventing a means for direct memory access and easy manipulation of the data within rubbed a lot of programmers the wrong way. Many pointed out that the use of pointers was one of the ways that C/C++ were able to be such a high-level language while still maintaining considerable speed. While assembly language and machine code were still de rigeur amongst people that were looking to squeeze the most amount of speed and memory out of a given program, things like pointers and compilers that effectively optimized C and C++ code did a lot to win people over, not to mention that it's a hell of a lot easier to read.

With Java, however, a lot of those same programmers felt that the security that was gained was offset by the decreases in speed and the fact that a lot of the earlier JIT compilers were pretty pokey didn't help that. For all their faults, Sun's been pretty adamant about listening to the community so they've made a lot of improvements regarding in-place optimization, HotSpot, conditional compilation, etc.

Another problem that came about is the fact that removing pointers made existing C and C++ code hard to port. That's generally pretty true of any new or significantly changed language, however. Eventually people got a handle on how to best replicate pointer usage, tips and tricks got passed around and things improved..
  


15. What are the arguments for and against Java’s implicit heap stor- age recovery, when compared with the explicit heap storage recovery required in C++? Consider real-time systems.
      Answer :
Implicit eliminates the creation of dangling pointers. Disadv: cpu-time to do recovery, sometimes when there’s plenty of heap storage so recovery isn’t necessary.