2.1 Structuring The Data, Computations and Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Sub Code: 210255

Principles of Programming Languages

SE Computer Engineering
(2019 Pattern)

1
UNIT-
UNIT-II
Structuring the Data, Computations and Program

By: Prof. S.N.Shelke

2
Good Thought

“ In a conflict between the heart and


the brain, follow your heart”

3
Unit II Contents

• Elementary Data Types :Primitive data Types, Character String types, User
Defined Ordinal Types, Array types, Associative Arrays, Record Types, Union Types,
Pointer and reference Type.
• Expression and Assignment Statements: Arithmetic expression, Overloaded
Operators, Type conversions, Relational and Boolean Expressions, Short Circuit
Evaluation, Assignment Statements, Mixed mode Assignment.
• Statement level Control Statements: Selection Statements, Iterative Statements,
Unconditional Branching.
• Subprograms: Fundamentals of Sub Programs, Design Issues for Subprograms,
Local referencing Environments, Parameter passing methods.
• Abstract Data Types and Encapsulation Construct: Design issues for
Abstraction, Parameterized Abstract Data types, Encapsulation Constructs, Naming
Encapsulations.
Elementary Data Types

• Primitive data Types,


• Character String types,
• User Defined Ordinal Types,
• Array types,
• Associative Arrays,
• Record Types,
• Union Types,
• Pointer and reference Type.
Primitive Data Types

1. Numeric Types-
 Integer,
 Floating-Point
 Complex
 Decimal

2. Boolean Types

3. Character Types
Character String types
Design Issues:
• Should strings be a special kind of character array or a primitive type?
• Should strings have static or dynamic length?

Strings and Their Operations


• assignment char str[] = "apples"; strcpy(dest, src);
• Catenation
• substring reference,
• comparison, and pattern matching.
String Length Options:
• static length string.
• Limited dynamic length string.
• Dynamic Length String
Evaluation
Implementation of Character String Types
Character String types
Implementation of Character String Types

2
User Defined Ordinal Types

Enumeration Type:
• An enumeration type is one in which all of the possible values, which are
named constants, are provided, or enumerated, in the definition
• Defining and grouping collections of named constants, which are called
enumeration constants. The definition of a typical enumeration type is
shown in the following C# example:
Array types
Design Issues
• 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 multidimensional arrays allowed, or both?
• Can arrays be initialized when they have their storage allocated?
• What kinds of slices are allowed, if any?
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.
array_name(subscript_value_list) → element
Sum := Sum + B(I);
Array types
Subscript Bindings and Array Categories
1. static array

2. fixed stack-dynamic array

3. fixed heap-dynamic array

4. Heap-dynamic array
Array types
Array Initialization:
int list [] = {4, 5, 7, 83};
char name [] = "freddie";
char *names [] = {"Bob", "Jake", "Darcie"};
String[] names = ["Bob", "Jake", "Darcie"];

Array Operations:
• The C-based languages do not provide any array operations, except through
the methods of Java, C++, and C#.
• Perl supports array assignments but does not support comparisons.
• F# includes many array operators in its Array module. Among these are
Array.append, Array.copy, and Array.length.
Array types
Array Operations (cont….):
• In APL, the four basic arithmetic operations are defined for vectors (single-
dimensioned arrays) and matrices, as well as scalar operands.
• For example,
A+B
A×B
is a valid expression, whether A and B are scalar variables, vectors, or matrices.

• APL includes a collection of unary operators for vectors and matrices, some of
which are as follows (where V is a vector and M is a matrix):
ϕV reverses the elements of V
ϕM reverses the columns of M
θM reverses the rows of M
ØM transposes M (its rows become its columns and vice versa)
÷M inverts M
Array types
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. Rectangular arrays model rectangular tables exactly.
• A jagged array is one in which the lengths of the rows need not be the
same. For example, a jagged matrix may consist of three rows, one with 5
elements, one with 7 elements, and one with 12 elements
• C, C++, and Java support jagged arrays but not rectangular arrays.
Array types
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.
Array types
Implementation of Array Types
list[k]
address(list[k]) = address(list[0]) + k * element_size
Address(list[k]) = ad dress(list[lower_bound]) +((k -lower_bound) * element_size)
Array types
Associative Arrays
• An associative array is an unordered collection of data elements that are indexed
by an equal number of values called keys.
• Non-associative arrays, the indices never need to be stored (because of their
regularity).
• In an associative array, however, the user-defined keys must be stored in the
structure. So each element of an associative array is in fact a pair of entities, a
key and a value.
Associative Arrays

Structure and Operations:


Record Types

Definitions of Records
• A record is an aggregate of data elements in which the individual
elements are identified by names and accessed through offsets from the
beginning of the structure.
Record Types

References to Record Field:


1. fully qualified reference: A fully qualified reference to a record field is one in

which all intermediate record names, from the largest enclosing record to the
specific field, are named in the reference.

Employee_Record.Employee_Name.Middle

2. elliptical references :

FIRST, FIRST OF EMPLOYEE-NAME, and FIRST OF EMPLOYEE-RECORD


Record Types
Union Types

• A union is a type whose variables may store different type values at


different times during program execution.
• As an example of the need for a union type, consider a table of
constants for a compiler, which is used to store the constants found in a
program being compiled
Union Types

• C and C++, the union construct is used to specify union structures. The
unions in these languages are called free unions, because
programmers are allowed complete freedom from type checking in their
use.
Pointer Type

• A pointer type is one in which the variables have a range of values that
consists of memory addresses and a special value, nil.
Reference Type

• A reference type variable is similar to a pointer, with one important


and fundamental difference: A pointer refers to an address in memory,
while a reference refers to an object or a value in memory.
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like