1) Enumerated types 2) Records and structures 3) Accessing fields in records/structures 4) Operations on records/structures 5) Variant records 6) Dynamic.

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags,Etc.
Kernighan/Ritchie: Kelley/Pohl:
COMP205 IMPERATIVE LANGUAGES
Chapter 9 Imperative and object-oriented languages 1.
Chapter 10.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
ISBN Chapter 6 Data Types: Structured types.
1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes”
TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion.
1) Linked records/structures 2) Linked lists, examples in Ada and C 3) Processing linked lists COMP205 IMPERATIVE LANGUAGES 8. COMPOUND (HIGHER LEVEL)
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes.
Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.
1) Linked records/structures 2) Linked lists, examples in Ada and C 3) Processing linked lists COMP205 IMPERATIVE LANGUAGES 8. COMPOUND (HIGHER LEVEL)
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Runtime Environments Compiler Construction Chapter 7.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Stack and Heap Memory Stack resident variables include:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter Structured Data 11. Combining Data into Structures 11.2.
Structured Data Objects (Structs). array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Data Types In Text: Chapter 6.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
COMP205 IMPERATIVE LANGUAGES
CS1010 Programming Methodology
COMP205 IMPERATIVE LANGUAGES
Module 2 Arrays and strings – example programs.
Programming Languages and Paradigms
Presentation transcript:

1) Enumerated types 2) Records and structures 3) Accessing fields in records/structures 4) Operations on records/structures 5) Variant records 6) Dynamic and static arrays of records/structures COMP205 IMPERATIVE LANGUAGES 7. COMPOUND (HIGHER LEVEL) DATA TYPES III

Standard High Level Types (Reminder): 1) Arrays 2) Strings 3) Enumerated types 4) Records/Structures 5) Unions

ENUMERATED TYPES An enumerated type is a user defined discrete type where the values are itemised. Ada example: C example: type DAYS_T is (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY); NEWDAY: DAYS_T:= WEDNESDAY; typedef enum days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } DAYS_T; DAYS_T newDay = Wednesday;

The values in a enumerated type are "ordered" so that the value listed first is least and the value listed last is greatest (an enumerated type is a discrete type). Conceptually at least, each value for an enumeration also has an ordinal value. This allows comparison of enumerated type values using operators such as (greater than). In C simple arithmetic can be performed on enumerated type values (e.g. finding successors, predecessors, etc.).

void main(void) { typedef enum days { monday=1, tuesday, wednesday, thursday, friday, saturday, sunday } DAYS_T; DAYS_T newDay1 = monday, newDay2; printf("newDay1 = %d\n",newDay1); printf("Successor to newDay1 = %d\n",newDay1+1); newDay2 = newDay1+2; printf("newDay2 = %d\n",newDay2); if (newDay2 == 3) printf("newDay2 = Wednesday\n"); } newDay1 = 1 Successor to newDay1 = 2 newDay2 = 3 = Wednesday

ADA ATTRIBUTES FOR ENUMERATED TYPES First - gives first value of the enumeration. Last - gives last value of the enumeration pred(X) - gives the predecessor of the enumerated value X (which must not be the first value). succ(X) - gives the successor of the enumerated value X (which must not be the last value). Some languages (e.g. Pascal) support an attribute ord(X) which returns the ordinal value of X.

procedure EXAMPLE is type DAYS is (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY); package DAYS_INOUT is new enumeration_io(DAYS); use DAYS_INOUT; NEWDAY: DAYS:= WEDNESDAY; begin put("DAYS'FIRST:= "); put(DAYS'FIRST); new_line; put("DAYS'LAST:= "); put(DAYS'LAST); new_line; put("DAYS'PRED(NEWDAY):= "); put(DAYS'PRED(NEWDAY)); new_line; put("DAYS'SUCC(SATURDAY):= "); put(DAYS'SUCC(SATURDAY)); new_line; if (NEWDAY < THURSDAY) then put("if NEWDAY < THURSDAY, NEWDAY:= MONDAY, TUESDAY or WEDNESDAY"); new_line; end if; end EXAMPLE ; DAYS’FIRST:= MONDAY DAYS’LAST:=SUNDAY DAYS’PRED(NEWDAY:= TUESDAY DAYS’SUCC(SATURDAY:= SUNDAY if NEWDAY < THURSDAY, NEWDAY:= MONDAY, TUESDAY or WEDNESDAY

FURTHER EXAMPLES OF ENUMERATED TYPES A well known enumerated type (although not often recognised as such) is the Boolean type. NOTE: the type boolean is actually a built in (enumerated) type in some imperative languages (Ada, Pascal). It is also possible to consider any discrete type (e.g. integer, character) to be an enumerated type. Ada type BOOLEAN is (FALSE, TRUE); typedef enum {false, true} BOOLEAN; C

RECORDS and STRUCTURES 1) Overview 2) Accessing fields in records and structures 3) Operations on records and structures 4) Examples in C and Ada 5) Variant records 6) Dynamic and static arrays of records and structures

RECORDS/STRUCTURES Arrays are used to group together collections of data items of the same type. A record (Ada) or structure (C) is used to group together a "fixed" number of data items (not necessarily of the same type) into a single data item. The items in a record/structure are called fields or components (or sometimes members). Unlike arrays or enumerated types records/structures can be recursive, i.e. they can contain a component of the same type as the record/structure in which the component is contained.

ACCESSING COMPONENTS OF RECORDS/STRUCTURES The fields of a record/structure are accessed using the record name and the field name (used in this way the latter is referred to as a selector) linked by an operator. In both C and Ada this operator is a “.” (dot). However, in C, if a pointer to a structure is used instead of its name an "arrow" operator is used, ->.

ASSIGNMENT OF RECORDS/STRUCTURES In some imperative languages (C) each member of a structure must be assigned a value individually. Other imperative languages (Ada) support the use of aggregates. COMPARISON OF RECORDS Some imperative languages (Ada, Pascal) provide an operator to carry out comparison of records (Ada uses the = and /= operators).

procedure EXAMPLE is type DATE_T is record DAY: integer; MONTH: string(1..9); YEAR: integer; end record; BIRTHDAY: DATE_T:= (15,"May ",1993); begin putline("Birthday = "); put(BIRTHDAY.DAY); put(" "); put(BIRTHDAY.MONTH); put(BIRTHDAY.YEAR); new_line; end EXAMPLE ; Birthday = 15 May 1993 ADA RECORD EXAMPLE

typedef struct date { int day; char month[9]; int year; } DATE_T, *DATE_PTR_T; void output_structure(DATE_PTR_T); void main(void) { DATE_T stPatricksDay; stPatricksDay.day = 17; strcpy(stPatricksDay.month,"March"); stPatricksDay.year = 1996; output_structure(&stPatricksDay); } void output_structure(DATE_PTR_T datePtr) { printf("%d, ",datePtr->day); printf("%s, ",datePtr->month); printf("%d\n",datePtr->year); } C STRUCTURE EXAMPLE

VARIANT RECORDS It is sometimes useful to be able to specify a number of alternative fields for a record, this is referred to as a variant record. Such records are a feature of imperative languages such Pascal and Modula-2 (but not Ada or C). A variant record comprises a fixed part, with fields as in a normal record, and a variant part, in which alternative fields are specified. The group of alternative fields which are applicable in any given instance of the record type is indicated by the value assigned to a tag field which is contained in the fixed part of the record.

ARRAYS OF STRUCTURES/RECORDS It is often necessary, given a particular application, to create a number of records/structures of the same type in which to store data, e.g. an array of structures. typedef struct date { int day; char month[9]; int year; } DATE_T, *DATE_PTR_T; void main(void) { int numOfEl=3; DATE_T structArray[numOfEl]; /* Load array */ /* Output array */ }

void assignToStructure(DATE_PTR_T newArray, int index, int newDay, char *newMonth, int newYear) { newArray[index].day = newDay; strcpy(newArray[index].month,newMonth); newArray[index].year = newYear; } void output(DATE_PTR_T newArray, int index) { printf("%d %s %d\n",newArray[index].day, newArray[index].month, newArray[index].year); }

DYNAMIC ARRAYS OF RECORDS/STRUCTURES In many cases the number of elements in an array are known in advance, and consequently the amount of memory required is known in advance. We refer to such an array as a static array of structures/ records. In other cases we do not know how many elements are to be in the array until run time, i.e. a dynamic array of structures/records. In this case we must create sufficient memory at run time. To create a dynamic data item we must use an “allocator” which reserves space in memory for the data item.

REMINDER ON ALLOCATORS Ada Example: (Access value) C Example: (pointer) Remember that an allocator (e.g. new or malloc ) returns a reference (address), referred to as an access value in Ada and a pointer in C, which indicates the first byte of the allocated memory. new integer malloc(sizeof(integer))

typedef struct date { int day; char month[9]; int year; } DATE_T, *DATE_PTR_T; void main(void) { int numOfEl; DATE_PTR_T structArray; printf("Enter number of structures "); scanf("%d",&numOfEl); if ((structArray = (DATE_PTR_T) (malloc(sizeof(DATE_T) * numOfEl))) == NULL) { printf("Insufficient space\n"); exit(-1); } --- Rest of code here ---

SUMMARY 1) Enumerated types 2) Records and structures 3) Accessing fields in records/structures 4) Operations on records/structures (assignment, comparison) 5) Examples in C and Ada 6) Variant records 7) Dynamic and static arrays of records/structures