Structured Data Types Date Types We have seen

  • Slides: 23
Download presentation
Structured Data Types

Structured Data Types

Date Types • We have seen various data types – Integer, Real, Character, Logical

Date Types • We have seen various data types – Integer, Real, Character, Logical • All these types define data values of different kinds – 128, 4500, 1, 0 (Integer) – 0. 5 E 19, 28. 0, 0. 214141 E 2 (Real) – hari, maithoo, cs 101, fortran (character strings) –. True. , . False. (logical) • The Data types define, besides values, – operations, relations and intrinsic functions • A summary given here - refer to books/manuals for details

Numerical Data Types Constants: 127, - 456, 12. 76, 56. 7 E 10, -

Numerical Data Types Constants: 127, - 456, 12. 76, 56. 7 E 10, - 0. 12 E-23 Note: decimal points in mantissa but not in exponent no commas allowed 12, 23, 600 Operations: +, *, /, Relations: >, <, >=, <=, ==, /= Intrinsic Functions: Real, Nint, Int, Abs, Mod, Exp, Log, Sign, Sin, Cos, Sinh, Cosh, …

Character Data Types Constants: ABab. Df, Fortran, CS 101 Note: capital and small letters

Character Data Types Constants: ABab. Df, Fortran, CS 101 Note: capital and small letters distinguished Operations: //, (n: m) Relations: equality operators, ==, /= Other comparison operators: < , <=, >, >= ASCII encoding: 8 bit encoding of characters A is 65, B is 66, etc a is 97, b is 98 etc

Lexicographic Ordering Collating Sequence: order in which characters occur in ASCII set Relation over

Lexicographic Ordering Collating Sequence: order in which characters occur in ASCII set Relation over individual characters based upon the collating sequence order eg. A < E, Z < a, d < s How to compare strings? – Dictionary ordering (Lexicographic ordering) – eg. AAA < AAB, AAA < AAAA, AAAA < AB Intrinsic Functions: Trim, Len,

Logical Data Types Constants: . true. , . false. Operations: . not. , .

Logical Data Types Constants: . true. , . false. Operations: . not. , . and. , . or. , . equiv, . nequiv.

Structured Data • All these data types (except character) are atomic – values are

Structured Data • All these data types (except character) are atomic – values are simple and non composite – scalar data types • Many real life entities are organized (or structured) collections of simpler data values: – students, accounts, employees (records with constituent items) – pack of cards, tables, vectors, matrices – queues, stacks, graphs • Referring to the constituent data values rather than the whole – results in loss of abstraction and clarity – cumbersome

Structured Data Types • Modern PL enable design of rich set of structured data

Structured Data Types • Modern PL enable design of rich set of structured data types to represent structured data • They provide constructs using which complex and structured data types can be built from scalar types • A PL provides – fixed primitive types for representation of simple data value – constructs for building organized collection from primitive types • hence called user defined data types

Single Dimensional Arrays • Array is one of the simplest and most often used

Single Dimensional Arrays • Array is one of the simplest and most often used structure • Examples: Vectors, list of students ordered by roll numbers, array of cars in a parking lot • Array data type is provided to represent such data • Typical array values are: – (32, 45, 67, 100, 780) - integer array – (4. 50, 345. 0 E 10, 28. 25 E-12) - real array – (Fortran, Pascal, C, C++, ADA) - character array – (. true. , . false. , . true. ) – logical array

Properties of Arrays • All entries in an array are values of identical types

Properties of Arrays • All entries in an array are values of identical types • The data values are of some type, called the base type • An array can contain one or more number of entries • Every entry is associated with an index • By default, the index of the first entry is 1, that of second 2, etc • The number of entries is called the size or length of the array • Any entry can be accessed directly by indicating its index – hence called random access data structure • Arrays are called single dimensional

Example • Let MARKS be an array (32, 24, 49, 20, 0) – base

Example • Let MARKS be an array (32, 24, 49, 20, 0) – base type of MARKS is integer – size or length of the array is 5 – MARKS(5) is 0 while MARKS(2) is 24 • Let Planets = (Venus, Earth, Mars, Saturn, Uranus, Neptune, Pluto) – base type is character – length is 7 – Planets(4) is Saturn

Array Declarations • Program Variables can store array values like any other basic values

Array Declarations • Program Variables can store array values like any other basic values • Typical declarations: – Integer, Dimension(5) : : Marks – Character(len=8), Dimension(7): : Planets • base type and length mentioned in declarations • The index of these arrays, also called as subscripts, start from 1 and end in the number specified • The subscripts can start anywhere and end anywhere; they should be specified then – Integer, Dimension(-8: 10): : Students – specifies the subscripts of Students to range from 8 to 10

Array References • Arrays can be referred in statements either as a whole or

Array References • Arrays can be referred in statements either as a whole or individually • Individual array elements are like any other variable of the base type • They can appear anywhere the variables can appear, eg. – – – Students(2) = 5 Marks(5) = Students(2) * 10 Students(i) = Student(i+1) Passed(j) =. TRUE. Planets(k) = Jupiter if (Students(i) > 0) then • Type matching same as variables of the base type

Whole array references • The whole array variable can also appear on both rhs

Whole array references • The whole array variable can also appear on both rhs and lhs, eg. – Planets = (/ Mars, Earth, Pluto, Neptune, Venus, Jupiter /) – Marks = Students • These are whole array assignments; the types of both lhs and rhs should match • Types of two array valued expressions match provided – they evaluate to arrays – of the same size and base type – subscript ranges may be different • The same constant value can be assigned to all elements of an array – Passed =. FALSE. – All the array entries are assigned the value false; useful for initialization

Array Initialization • Arrays can be initialized at declarations, eg. – integer, dimension(10) :

Array Initialization • Arrays can be initialized at declarations, eg. – integer, dimension(10) : : a = (/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 /) – initializes array a to (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) • Implied do loop (Shorthand notation for loops) can be used for initialization, eg. – a = (/ (2*i, i=1, 10) /) ! i must be declared before use – a= (/ (i, i, i = 1, 10, 2) /) • a is 1, 1, 3, 3, 5, 5, 7, 7, 9, 9

Reading/Writing of Arrays • Arrays may be read or printed as a whole, eg.

Reading/Writing of Arrays • Arrays may be read or printed as a whole, eg. – read *, a – print *, a • Values input/output on a single line • Implied do loops may also be used, eg. – read *, (a(i), i = 1, n) – values of a(1) to a(n), read on one line

Array Sections • Arrays can be referred as a whole, per entry or even

Array Sections • Arrays can be referred as a whole, per entry or even in parts • Parts of the arrays can be referred using sections, eg – Given an array Ex declared as follows integer dimension(100), Ex – Ex(8: 89: 2) - part of the array Ex(8), Ex(10), . . . , Ex(88) – Ex(10: 50) - references Ex(10), Ex(11), . . . , Ex(50) – Ex(28: : 5) - refer to Ex(28), Ex(33), . . . , Ex(98) – Ex(28: 2) - null array – Ex(1: 100) - whole section, referred directly as Ex itself

Array Bounds • Every array has bounds - lower and upper bounds • The

Array Bounds • Every array has bounds - lower and upper bounds • The above array Ex has 1 as lower bound and 100 as upper bound • Ex(i) is defined provided 1 <= i <= 100 • Any reference to Ex(-25) or Ex(111) is illegal and may produce unknown results • A reference is illegal or not can be detected, in general, at runtime only • Ex(j) exceeds bound or not depends on value of j - not known at compile time • Fortran compiler provides the option of detecting (at run-time) of array index exceeding bounds • If this option is on, it inserts code, which can produce run-time errors • If off, you have the responsibility for illegal references

Operations on Arrays • Array assignments involve expressions involving whole arrays • Operations are

Operations on Arrays • Array assignments involve expressions involving whole arrays • Operations are defined over arrays depending upon the base type • Operations are `lifted' from the base type – arithmetic operations over real and integer arrays – logical operations over logical arrays • Operations apply `point wise' to the array entries • Arrays involved should be of the same size

Example integer, dimension(50) : : A, B integer, dimension(51: 100) : : C read

Example integer, dimension(50) : : A, B integer, dimension(51: 100) : : C read *, A, B ! reads 100 values (A followed by B) C = 2*A + B + 5 ! C(i) = 2*A(i) + B(50+i) + 5 • operation is applied to corresponding elements in “parallel” • similar to component wise vector addition • other arithmetic operations (except **) applied similarly • scalar operation performed on each element

Relational Operators • whole array or array sections can be compared using relational operator

Relational Operators • whole array or array sections can be compared using relational operator • As usual, type and size constraints need to respected • Comparison to scalars also possible • Example: integer, dimension(50) : : a, b logical, dimension(50) : : c c = (a < b). and. ( a > a(1)) ! c(i) is. true. iff a(i) < b(i) and a(i) > a(1), 1<=i <=50 • similarly for other relational operators

Intrinsic Functions • Many intrinsic functions available for arrays • `Lifting' of Intrinsic functions

Intrinsic Functions • Many intrinsic functions available for arrays • `Lifting' of Intrinsic functions of base type to arrays • functions of integers and reals can be applied to integer or real arrays • functions applied pointwise - to all elements separately – size(a): number of elements in array a – maxval(a) and minval(a) • maximum and minimum value in integer or real array a

Intrinsic Functions (contd. ) • sum(a), product(a) – sum or product of elements of

Intrinsic Functions (contd. ) • sum(a), product(a) – sum or product of elements of integer or real array a • dot_product(a, b) – dot product of two vectors of same size • Array operations and intrinsic functions makes program more readable • More efficient also, as operations can be done in parallel