0% found this document useful (0 votes)
11 views

Data Types

Uploaded by

John Ramiro
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Data Types

Uploaded by

John Ramiro
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

DATA

TYPES
INTRODUCTION 6.1
Data types : A collection of data values and a set of predefined operations

Discriptor: Collection of attributes of a variable and can be a difference between Static and Dynamic
attributes.

The difference between Static vs Dynamic Attributes/Typing

Static: Fix or bound in compiling time


Example:
Before & After: int X = 10;

Dynamic: Not fixed or bound UNTIL run time.


Example:
Before: int X = 10;
After: string X = “mewmew”
6.2
PRIMITIVES DATA TYPES
• imitive Data
Type : Set of
basic data
types from
which all other
data types are
constructed.
6.2

• Numeric Data Type “Numeric Primitive”: (short, int, long, float, double)

• Textual Primitive (Byte and Char):

• Integer (byte, short, int, long): A representation of a range in mathematical


integers with a positive or negative, zero integers by means of two’s
complement

• Floating Point (float, double): Represents fractions and exponents


6.2

Complex (array, map, struc, nested): these nested data structures composed of
primitive data types or other complex types
6.2
Example:
• Struct

• Array
6.2

• Decimal: stores a fixed number of decimal digits

• Boolean Types: A range value that only has two functions, true or false.

• Character Types (char, varchar, long varchar): string of alphabetic characters


with upper or lower cases
6.3
CHARACTER STRING
TYPES
Character String Type: consists of sequences of characters and used to label
output and input of all kinds of data

Flaws of Designing Character String Types

• Should it be having special kind of character array?


• Should it be static or dynamic length
6.3
String and Their Operation (assignment, catenation, substring reference,
comparison, pattern matching)
Substring reference: reference to a substring which are called slices

Ex. Char str{}’ = “Puppies”


Regular expression (pattern matching expression)

example: /[A-Za-z] [A-Za-z\d]+/


String length option:
• Static length
• Dynamic length
6.4

USER-DEFINED ORDINAL
TYPES
An ordinal type is one in which the range of possible values can be easily
associated with the set of positive integers

TWO USER-DEFINED ORDINAL TYPES


1. Enumeration
2. Subrange
6.4

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.
Enumeration types provide a way of defining and grouping collections of
named constants, which are called enumeration constants

Example:
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
6.4
THE DESIGN ISSUES
Coercion of Enumeration Values to Integers
DESIGNS
6.4
In languages that do not have enumeration types, programmers usually simulate them
with integer values.
Example:
int red = 0;
int blue = 1;
6.4
SOLUTION

Make red and blue named constants instead of variables.


6.4
SUBRANGE TYPE
A subrange type is a contiguous subsequence of an ordinal
type.

Example:
type
MyRange = 20..50;
ADA’s DESIGN 6.5
In Ada, subranges are included in the category of types called subtypes

Example:
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

Example:
subtype Weekdays is Days range Mon..Fri;

Example:
Day1 : Days;
Day2 : Weekdays;

Day2 := Day1;
6.5

ARRAYS AND INDICES


Arrays are referenced using a two-level syntactic
mechanism:
· Aggregate name
· Subscript or Indices
6.5
EXAMPLE:
6.5
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:


6.5
Static Array
· subscript ranges are statically bound and storage
allocation is static (done before run time).

EXAMPLE:
6.5
Fixed Stack-Dynamic Array

· subscript ranges are statically bound, but the


allocation is done at declaration elaboration time
during execution.

Stack-Dynamic Array
· both the subscript ranges and the storage allocation
are dynamically bound at elaboration time.
6.5
Fixed Heap-Dynamic Array
· similar to a fixed stack-dynamic array, in that the
subscript ranges and the storage binding are both
fixed after storage is allocated.

Heap-Dynamic Array
· subscript ranges and storage allocation is dynamic
and can change any number of times during the
array’s lifetime.
6.5
RECTANGULAR AND JAGGED
ARRAYS
• Rectangular array is a multi-dimensioned array in which all of the
rows have the same number of elements and all of the columns have
the same number of elements.
6.5
Jagged Array is one in which the lengths of the rows
need not be the same.
6.5
SLICES
Slice of an array is some substructure of that array.
6.5

IMPLEMENTATION OF
ARRAY TYPES
Implementing arrays requires considerably more compile-time
effort than does implementing primitive types.
6.6

ASSOCIATIVE ARRAYS
Associative array is an unordered collection of data elements that are
indexed by an equal number of values called keys.
In the case of non-associative arrays, the indices never need to be stored
(because of their regularity).
6.6
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:


· Key
· Hashes
6.6
KEY AND HASHES
6.6
ASSOCIATIVE ARRAYS
Array Categories

Static Arrays: Subscript ranges and storage allocation are static.


Fixed Stack-Dynamic Arrays: Subscript ranges are static, allocation is dynamic.
Stack-Dynamic Arrays: Both subscript ranges and allocation are dynamically
bound.
Fixed Heap-Dynamic Arrays: Subscript ranges and allocation are fixed, but
dynamic.
Heap-Dynamic Arrays: Subscript ranges and allocation are dynamic.
6.6

Examples in Programming Languages

C, C++, Java, Ada, C#: Static and fixed stack-dynamic arrays.


Perl: Dynamic arrays with unusual syntax.
JavaScript: Dynamic arrays with sparse support.
Python, Ruby, Lua: Dynamic arrays with specific growth methods.
Fortran 95+: Array initialization using array aggregates.
C, C++, Java, C#: Array initialization syntax and implications.
6.7
RECORD TYPES
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.

Records have been part of all of the most popular programming languages, except pre-90
versions of Fortran, since the early 1960s, when they were introduced by COBOL.

Definition: The fundamental difference between a record and an array is that recordele-
ments, or fields, are not referenced by indices. Instead, the fields are named with
identifiers, and references to the fields are made using these identifiers.
6.7
Example: 01 EMPLOYEE-RECORD.
02 EMPLOYEE-NAME.
05 FIRST PICTURE IS X(20). 05 MIDDLE PICTURE IS X(10).
05 LAST PICTURE IS X(20).
02 HOURLY-RATE PICTURE IS 99V99.

Evaluation:
Records are frequently valuable data types in programming languages. The Design of record types is
straightforward, and their use is safe.

Implementation of Record Types:

The fields of records are stored in adjacent memory locations. But because the sizes of the fields are not
necessarily the same, the access method used for arrays is not used for records. I
6.8
TUPLE TYPES
A tuple is a data type that is similar to a record, except that the elements are not named

A tuple is created by assigning a tuple value, whichisa list of expressions separated by


commas and delimited by parentheses, toename in a let statement

Tuples are used in Python, ML, and F# to allow functions to returnmul-tiple values.

tuples are closely related to its lists, except that tuples are immutable. A tuple is created
by assigning a tuple literal, as in the following example
6.8

Example:

>>>> cars = ('Audi', 'mercedes', 'bmw')


>>>> cars
('Audi, 'mercedes', 'bmw')
>>>>
6.9
LIST TYPES
Lists were first supported in the first functional programming language, LISP.
They have always been part of the functional languages, but in recent years they
have found their way into some imperative languages.

The fundamental list operations in Scheme are two functions that take listsapart
and two that build lists. The CAR function returns the first element of itslist
parameter.
Example 1:
A list of 10 natural numbers.
6.9
1 2 3 4 5 6 7 8 9 10
0123456789

Example 2:
A list with different types of items

Items = [ 1, 2, 'N', "Go", 3.14159]

A list comprehension is an idea derived from set notation. It first appeared in the functional
programming language Haskell

The mechanics of a list comprehension is that a function is applied to each of the elements of a
given array and a new array is constructed from results
6.10
UNION TYPES

A union is a type whose variables may store different


type values at different times during program
execution.
DESIGN ISSUES 6.10
Should type checking be required?
6.10

FREE UNIONS
In some programming languages like C and C++, unions are often
called "free unions" because there's no built-in support for
checking the type of data stored in them.
6.10

DISCRIMINATED UNIONS
Each union has a special indicator called a tag or discriminant,
which tells the program what type of data is currently stored in the
union.
6.10
6.11
POINTER AND REFERENCE
TYPES
Pointer Type : variables have range of values that consist of memory addresses and special value

2 distinct uses of pointers


Provides power of indirect addressing which is common in assembly language program
Manges dynamic storage

Design issues of pointer types:


• The scope and lifetime of a pointer variable
• What is the lifetime of a heap-dynamic variable
• Are pointers restricted to the type of value
• Are pointers used for dynamic storage management
• Should pointer/reference types have language support
Pointer operations: has two fundamental pointer operations, assignment and dereferencing 6.11
Dangling Pointer (dangling reference): contains the address of a heap-dynamic variable that has been
deallocated

Pointers in Ada: these types of pointers are called access type

Reference type: variable similar to a pointer, with one fundamental difference, pointers refers to addresses
while reference refers to the object of value in memory

Implementation of Pointer and Reference Types:

Heap management:
• Single size Cells
• Reference counters
• Counter mark-sweep
• Eager approach & Lazy approach
6.12
TYPE CHECKING
Type checking is the activity of
ensuring that the operands of an
operator are of compatible
types. A compatible type is one
that either works directly with
an operator or can be
automatically changed by the
computer to work with it. This
automatic conversion is called
coercion.

Ex.
6.12

There are two types of type checking: statically and dynamically.


Static checking is preferred as it catches errors earlier, but dynamic
checking is necessary in languages with dynamic type binding like
JavaScript and PHP. Dynamic type binding requires type checking at
run time. While static binding is compile-time, certain language
features like variant records and unions in languages like Ada, C, C+
+, ML, Haskell, and F# necessitate dynamic checking.
6.12
EXAMPLE: STATIC
EXAMPLE: DYNAMIC 6.12
6.13
STRONG TYPING
The concept of strong typing in programming languages, which ensures the detection
of type errors, became prominent during the structured-programming revolution of
the 1970s. Strong typing is characterized by the ability to detect type errors either at
compile time or run time. Ada is nearly strongly typed but allows suspension of type
checking in specific cases. C and C++ are not strongly typed due to union types,
while ML and F# are examples of strongly typed languages. Java and C# are strongly
typed like Ada, but with explicit type casting. Coercion, such as in Java, can weaken
the benefits of strong typing by allowing type errors to go undetected. Languages
with less coercion, like Ada, ML, and F#, are more reliable in error detection
compared to languages like C and C++.
6.14
TYPE EQUIVALENCE
Type Compatibility refers to whether two types of data can work together in
an expression, sometimes allowing automatic conversion between types
(coercion). While Type Equivalence means that two types are considered
equivalent if they can be used interchangeably without any automatic
conversion.
6.15
THEORY AND DATA
TYPES
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy