Professional Documents
Culture Documents
Adebimpe Adetoba
Adebimpe Adetoba
Data Types
ISBN 0-321—49362-1
Primitive Data Types
1-2
Primitive Data Types: Integer
1-3
Primitive Data Types: Complex
1-4
Primitive Data Types: Decimal
• Advantage: accuracy
1-5
Primitive Data Types: Boolean
• Simplest of all
1-6
Primitive Data Types: Character
1-7
Character String Types
1-8
Character String Types Operations
• Typical operations:
– Assignment and copying
– Comparison (=, >, etc.)
– Catenation
– Substring reference
– Pattern matching
1-9
Character String Type in Certain
Languages
• C and C++
– Not primitive
• Java
– Primitive via the String class
• C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
1-12
Array Types
1-13
Array Indexing
• Index Syntax
– Fortran and Ada use parentheses
• Ada explicitly uses parentheses to show
uniformity between array references and function
calls because both are mappings
– Most other languages use brackets
1-14
Arrays Index (Subscript) Types
range checking
1-15
Array Initialization
1-16
Array Initialization
1-17
Heterogeneous Arrays
1-18
Arrays Operations
• APL provides the most powerful array processing
operations for vectors and matrixes
1-19
Rectangular and Jagged Arrays
1-20
Rectangular and Jagged Arrays
1-22
Slice Examples
• Python
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
+ ((k-lower_bound) * element_size)
1-24
Locating an Element in a Multi-
dimensioned Array
•General format
Location (a[I,j]) = address of a [row_lb,col_lb] +
(((i - row_lb) * n) + (j - col_lb)) * element_size
1-25
Associative Arrays
1-26
Associative Arrays in Perl
delete $hi_temps{"Tue"};
1-27
Record Types
• A record is a possibly heterogeneous
aggregate of data elements in which the
individual elements are identified by names
1-28
Tuple Types
• A tuple is a data type that is similar to a
record, except that the elements are not
named
• ML
val myTuple = (3, 5.8, ′apple′);
- Access as follows:
#1(myTuple) is the first element
- A new tuple type can be defined
type intReal = int * real;
• F#
let tup = (3, 5, 7)
let a, b, c = tup This assigns a tuple to a
tuple pattern (a, b, c)
1-30
List Types
• Lists in Lisp and Scheme are delimited by
parentheses and use no commas
(A B C D) and (A (B C) D)
1-31
List Types (continued)
1-32
List Types (continued)
• List Operations in ML
– Lists are written in brackets and the elements are
separated by commas
• F# Lists
– Like those of ML, except elements are
separated by semicolons and hd and tl are
methods of the List class
• Python Lists
– The list data type also serves as Python’s
arrays
– Elements can be of any type
– Create a list with an assignment
myList = [3, 5.8, "grape"]
1-34
List Types (continued)
1-35
Unions Types
• Design issue
– Should type checking be required?
1-36
Unions in F#
• Defined with a type statement using OR
type intReal =
| IntValue of int
| RealValue of float;;
intReal is the new type
IntValue and RealValue are constructors
1-37
Evaluation of Unions
1-38
Pointer and Reference Types
1-39
Pointer Operations
• Two fundamental operations: assignment and
dereferencing
1-40
Pointer Assignment Illustrated
float stuff[100];
float *p;
p = stuff;
1-44
Reference Types
Language examples:
- ML and F# are
1-49
Name Type Equivalence
• Name type equivalence means the two
variables have equivalent types if they are in
either the same declaration or in declarations
that use the same type name
1-51
Class Activities
i. What is a descriptor?
ii. What are the advantages and disadvantages of decimal
data types?
iii. What are the design issues for character string types?
iv. Describe the three string length options.
v. Define ordinal, enumeration, and subrange types.
vi. What are the advantages of user-defined enumeration
types?
vii. In what ways are the user-defined enumeration types of
C# more reliable than those of C++?
viii. What are the design issues for arrays?
ix. Define static, fixed stack-dynamic, stack-dynamic, fixed
heap-dynamic, and heap-dynamic arrays. What are the
advantages of each? 1-52
Programming Assignments
i. Design a set of simple test programs to determine the type
compatibility rules of a C compiler to which you have access.
Write a report of your findings.
1-53
Programming Assignments
iv. Write a Perl program that uses a hash and a large number of
operations on the hash. For example, the hash could store
people’s names and their ages. A random-number generator
could be used to create three-character names and ages, which
could be added to the hash. When a duplicate name was
generated, it would cause an access to the hash but not add a
new element. Rewrite the same program without using hashes.
Compare the execution efficiency of the two. Compare the ease
of programming and readability of the two.