0% found this document useful (0 votes)
40 views15 pages

PPL-Unit 2 Part 4

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views15 pages

PPL-Unit 2 Part 4

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Principle of programming languages

Unit-II: array, associative arrays, record,


tuple types, list types, union types
Array
•An array is a homogeneous aggregate of data
elements
•In many languages, such as C, C++, Java, Ada, and
C#, all of the elements of an array are required to be of
the same type.

Design Issues
The primary design issues specific to arrays are the
following:
• 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 jagged or rectangular multidimensioned arrays
allowed, or both?
Array
Arrays and Indices
•Specific elements of an array are referenced by means
of a two-level syntactic mechanism, where the first part
is the aggregate name, and the second part is a
possibly dynamic selector consisting of one or more
items known as subscripts or indices.
Syntactic representation
array_name(subscript_value_list)
Example in c
Int a[10];
For example, in Ada
type Week_Day_Type is (Monday, Tuesday, Wednesday,
Thursday, Friday);
type Sales is array (Week_Day_Type) of Float;
Array
Subscript Bindings and Array Categories
•The binding of the subscript type to an array variable
is usually static, but the subscript value ranges are
sometimes dynamically bound.
•There are five categories of arrays, based on the
binding to subscript ranges, the binding to storage, and
from where the storage is allocated.
Static array
•A static array is one in which the subscript ranges are
statically bound and storage allocation is static.
•Arrays declared in C and C++ functions that include
the static modifier are static.
Advantage
•Static arrays are efficienct: No dynamic allocation or
deallocation is required.
Array
Disadvantage
•In this static arrays the storage for the array is fixed
for the entire execution time of the program.
fixed stack-dynamic array
•A fixed stack-dynamic array is one in which the
subscript ranges are statically bound, but the allocation
is done at declaration elaboration time during
execution.
•Arrays that are declared in C and C++ functions
(without the static
specifier) are examples of fixed stack-dynamic arrays.
Advantage
•fixed stack-dynamic arrays over static arrays is space
efficiency.
Disadvantage
•fixed stack-dynamic arrays requires allocation and
Array
stack-dynamic array
•A stack-dynamic array is one in which both the
subscript ranges and the storage allocation are
dynamically bound at elaboration time.
•Ada arrays can be stack dynamic
Advantage
•stack-dynamic arrays over static and fixed stack-
dynamic arrays is flexibility.

fixed heap-dynamic array


•A fixed heap-dynamic array is similar to a fixed stack-
dynamic array, in that the subscript ranges and the
storage binding are both fixed after storage is
allocated.
•The differences are that both the subscript ranges and
storage bindings are done when the user program
Array
•C and C++ also provide fixed heap-dynamic arrays.
The standard C library functions malloc and free, which
are general heap allocation and deallocation operations
•C++ uses the operators new and delete to manage
heap storage.
Advantage
•The advantage of fixed heap-dynamic arrays is
flexibility
Disadvantage
•The disadvantage is allocation time from the heap,
which is longer than allocation time from the stack

heap-dynamic array
•A heap-dynamic array is one in which the binding of
subscript ranges and storage allocation is dynamic and
can change any number of times during the array’s
Array
Advantage
•The advantage of heap-dynamic arrays over the others is
flexibility:
Arrays can grow and shrink during program execution as
the need for space changes.
Disadvantage
•The disadvantage is that allocation and deallocation take
longer
and may happen many times during execution of the
program

Array Initialization:

•In Fortran 95+ example


Integer, Dimension (3) :: List = (/0, 5, 5/)
Array
•character strings in C and C++ are implemented as
arrays of char
Ex: char name [] = "freddie";
•Arrays of strings in C and C++ can also be initialized with
string literals. In this case, the array is one of pointers to
characters
Ex: char *names [] = {"Bob", "Jake", "Darcie"};
•In Java, similar syntax is used to define and initialize an
array of references to String objects
Ex: String[] names = ["Bob", "Jake", "Darcie"];
•Ada provides two mechanisms for initializing arrays in
the declaration
statement: 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
Array
Array Operations
•The most common array operations are assignment,
catenation, comparison for equality and inequality, and
slices
•The C-based languages do not provide any array
operations
•Perl supports array assignments but does not support
comparisons, Ada allows array assignments
•Python’s arrays are called lists. Python provides array
assignment

Rectangular and Jagged Arrays


•A rectangular array is a multidimensioned array in which
all of the rows have the same number of elements and all
of the columns have the same number of elements.
Array

•C, C++, and Java support jagged arrays but not


rectangular arrays
For example,
myArray[3][7]
•Fortran, Ada, C#, and F# support rectangular arrays. In
these cases, all subscript expressions in references to
elements are placed in a single pair of brackets. For
example,
myArray[3, 7]
Array
Slices
•A slice of an array is some substructure of that array.
•For example, if A is a matrix, then the first row of A is one
possible slice, as are the last row and the first column.
•It is important to realize that a slice is not a new data
type.
•it is a mechanism for referencing part of an array as a
unit
•Consider the following Python declarations:
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
•For example,
mat[1] refers to the second row of mat. mat[0][0:2] refers
to the first and second element of the first row of mat,
which is [1, 2]
Associative Arrays
Associative Arrays
•An associative array is an unordered collection of
data elements that areindexed by an equal number of
values called keys.
•Associative arrays are also supported directly by Python,
Ruby

Structure and Operations


•In Perl, associative arrays are called hashes, because
in the implementation their elements are stored and
retrieved with hash functions.
• The namespace for Perl hashes is distinct: Every hash
variable name must begin with a percent sign (%).
• Each hash element consists of two parts: a key, which is
a string, and
Associative Arrays
Hashes can be set to literal values with the assignment
statement, as in
%salaries = ("Gary" => 75000, "Perry" => 57000,
"Mary" => 55750, "Cedric" => 47850);
scalar variable names begin with dollar signs ($).
For example,
$salaries{"Perry"} = 58850;
A new element is added using the same assignment
statement form. An element can be removed from the
hash with the delete operator, as in
delete $salaries{"Gary"};
The entire hash can be emptied by assigning the empty
literal to it, as in
@salaries = ();
Associative Arrays
•The size of a Perl hash is dynamic: It grows when an
element is added and shrinks when an element is
deleted, and also when it is emptied by assignment of the
empty literal. The exists operator returns true or false,
depending on whether its operand key is an element in
the hash. For example,
if (exists $salaries{"Shelly"}) . . .
•Python’s associative arrays, which are called
dictionaries
•PHP’s arrays are both normal arrays and associative
arrays.

You might also like