Name : Vina Melinda
NIM : 1801380106
Untuk kali ini saya akan menjawab Assignment #8 Problem Set dan Review Questions dari Chapter 6 dari Sebesta.
Review Questions:
A derived type is a completely separate type that has the same characteristics as its base type. You cannot mix operands of a derived type with operands of the base type. If, for example, you used:
In C and C++, there are two ways a pointer to a record can be used to reference a field in that record. If a pointer variable p points to a record with a field named age, (*p).age can be used to refer to that field. The operator ->, when used between a pointer to a record and a field of that record, comfbines dereferencing and field reference. For example, the expression p -> age is equivalent to (*p).age. In Ada, p.age can be used, because such uses of pointers are implicitly dereferenced.
6. Q: What are the advantages of user-defined enumeration types?
A: Readability is enhanced very directly and named values are easily recognized, whereas coded values are not. In reliability this enumeration types provide two other advantages
1. No arithmetic operations are legal on enumeration types
2. No enumeration variable can be assigned a value outside its defined range.
7. Q: In what ways are the user-defined enumeration types of C# more reliable than those of C++?
A: C# enumeration types are like those of C++, except that they are never coerced to integer. So, operations on enumeration types are restricted to those that make sense. Also, the range of values is restricted to that of the particular enumeration type.
8. Q: What are the design issues for arrays?
A: 1. What types are legal for subscripts
2. Are subscripting expressions in element references range checked?
3. When are subscript ranges bound?
4. What does array allocation take place?
5. Are ragged or rectangular multidimensioned arrays allowed, or both?
6. Can arrays be initialized when they have their storage allocated?
7. What kinds of slices are allowed, if any?
2. Are subscripting expressions in element references range checked?
3. When are subscript ranges bound?
4. What does array allocation take place?
5. Are ragged or rectangular multidimensioned arrays allowed, or both?
6. Can arrays be initialized when they have their storage allocated?
7. What kinds of slices are allowed, if any?
9. Q: Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic, and heap-dynamic arrays. What are the advantages of each?
A: 1. Static: subscript ranges are statically bound and storage allocation is static (before run-time). Advantage: efficiency (no dynamic allocation).
2. Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time. Advantage: space efficiency.
3. Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time). Advantage: flexibility (the size of an array need not be known until the array is to be used).
4. Fixed heap-dynamic: similar to fixed stack-dynamic: storage binding is dynamic but fixed after allocation. Advantage: space efficiency, storage is allocated from heap, and binding is done when requested.
5. Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times. Advantage: flexibility (arrays can grow or shrink during program execution).
2. Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time. Advantage: space efficiency.
3. Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time). Advantage: flexibility (the size of an array need not be known until the array is to be used).
4. Fixed heap-dynamic: similar to fixed stack-dynamic: storage binding is dynamic but fixed after allocation. Advantage: space efficiency, storage is allocated from heap, and binding is done when requested.
5. Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times. Advantage: flexibility (arrays can grow or shrink during program execution).
10. Q: What happens when a nonexistent element of an array is referenced
in Perl?
A: A reference to a nonexistent element in Perl yields undef, but no error is reported.
Problem Set:
6. Q: Explain all of the differences between Ada’s subtypes and derived types.
A: A subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. For example:
subtype Week_Days is Integer range 1..7;
Since this is a subtype, you can (for example) add 1 to a weekday to get the next weekday.
Since this is a subtype, you can (for example) add 1 to a weekday to get the next weekday.
A derived type is a completely separate type that has the same characteristics as its base type. You cannot mix operands of a derived type with operands of the base type. If, for example, you used:
type Week_Day is new Integer range 1..7;
Then you would not be able to add an integer to a weekday to get another weekday. To do manipulations on a derived type, you'd normally define those manipulations yourself (e.g., create a package). At the same time, a derived type does "inherit" all the operations of its base type (even some that may not make sense) so you do still get addition.
Then you would not be able to add an integer to a weekday to get another weekday. To do manipulations on a derived type, you'd normally define those manipulations yourself (e.g., create a package). At the same time, a derived type does "inherit" all the operations of its base type (even some that may not make sense) so you do still get addition.
7. Q: What significant justification is there for the -> operator in C and C++?
A: The only justification for the -> operator in C and C++ is writability. It is slightly easier to write p -> q than (*p).q.
8. Q: What are all of the differences between the enumeration types of C++
and those of Java?
A: In C++, an enumeration is just a set of named, integral constants. 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.
Also, C++ will implicitly convert enum values to their integral equivalent, whereas the conversion must be explicit in Java.
Also, C++ will implicitly convert enum values to their integral equivalent, whereas the conversion must be explicit in Java.
9. Q: 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?
A: Advantage:
Unconstrained variant records in Ada allow the values of their variants to change types during execution.
Disadvantage:
However, the type of the variant can be changed only by
assigning the entire record, including the discriminant. This disallows inconsistent records because if the newly assigned record is a constant data aggregate, the value of the tag and the type of the variant can be statically checked for consistency.
Unconstrained variant records in Ada allow the values of their variants to change types during execution.
Disadvantage:
However, the type of the variant can be changed only by
assigning the entire record, including the discriminant. This disallows inconsistent records because if the newly assigned record is a constant data aggregate, the value of the tag and the type of the variant can be statically checked for consistency.
10. Q: 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.
A: 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))
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))