Chapter 6 Part 1.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Arrays and records Programming Language Design and Implementation
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
1 Programming Languages Implementation of Data Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.
6-1 Chapter 6: Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
Data Structures – Week #1 Introduction. February 17, 2012Borahan Tümer, Ph.D.2 Goals We will learn methods of how to (explicit goal) organize or structure.
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
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.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Implementing Subprograms
The C++ Data Types Fundamental Data Types
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Data Types In Text: Chapter 6.
VBA - Excel VBA is Visual Basic for Applications
Dr. Vamsi Paruchuri University of Central Arkansas
Data Types I Subject : T0152 – Programming Language Concept
Chapter 6 – Data Types CSCE 343.
CMP 339/692 Programming Languages Day 14 Tuesday, March 20, 2012
Arrays Low level collections.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Computer Programming BCT 1113
Pointers, Enum, and Structures
Structures and Unions.
Module 2 Arrays and strings – example programs.
CS 363 – Chapter 8 Composite data types Records
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Implementing Subprograms
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Data.
Data Structures – Week #1
Chapter 6 Data Types.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
COMP26120: Algorithms and Imperative programming
Chapter 6 Data Types.
Topic 3-b Run-Time Environment
Chapter 1: Introduction to Data Structures(8M)
MSIS 655 Advanced Business Applications Programming
Chapter 6 Data Types.
CS222/CS122C: Principles of Data Management Lecture #2 Storing Data: Disks and Files Instructor: Chen Li.
Programming Language C Language.
Structures In C Programming By Rajanikanth B.
Course Overview PART I: overview material PART II: inside a compiler
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Arrays and records Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Structure (i.e. struct) An structure creates a user defined data type
C++ Array 1.
Arrays and records Programming Language Design and Implementation
Arrays, Casting & User Defined Variables
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Implementing Subprograms
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Structures, Unions, and Enumerations
Arrays and Pointers.
Chapter 6 Data Types.
Presentation transcript:

Chapter 6 Part 1

Array Implementation 1D Array … 1 2 k-1 k n M Memory Base Address … 1 2 k-1 k n M 1D Array Memory Base Address Address(list[k]) = address(list[1]) + (k-1) * element_size Generalization: Address(list[k]) = address(list[lower_bound]) + (k - lower_bound) * element_size Example: Array A with following information: 10 elements Element size = 10 bytes Base address = 1500 Lower bound = 0 Address( A[5]) = ? Address( A[5]) =1500 + 5*10 = 1550

2D Arrays 1) Row Major Order 2) Column Major Order 2 common ways for mapping 2D arrays to 1D: 1) Row Major Order 2) Column Major Order Ex: 3 4 7 6 2 5 1 3 8 Would be stored as 3, 4, 7, 6, 2, 5, 1, 3, 8 Ex: 3 4 7 6 2 5 1 3 8 Would be stored as 3, 6, 1, 4, 2, 3, 7, 5, 8

2D Array - Row Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 j-1 j n … i-1 i m . . .

2D Array - Row Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 j-1 j n … i-1 i m . . .

2D Array - Row Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 j-1 j n … i-1 i m . . . Address(a[i,j]) = address(a[1,1]) + ((((number of rows above i)* (row size)) + (number of elements left j)) * element size) = address(a[1,1]) + ((((i-1)* n) + (j-1)) * element_size) Generalization: Address(a[i,j]) = address(a[row_lb,col_lb]) + ((((i-row_lb)* n) + (j-col_lb)) * element_size)

2D Array - Row Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Row Major Order 1 2 j-1 j n … i-1 i m . . . Example: Array A with following information: 15 rows and 5 columns Element size = 10 bytes Base address = 1000 Row lower bound =column lower bound = 0 Address( A[2,3]) = ? Address( A[2,3]) =1000 + (((2-0)*5) + (3-0)) *10 = 1130

2D Array - Column Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 j-1 j n … i-1 i m . . .

2D Array - Column Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 j-1 j n … i-1 i m . . .

2D Array - Column Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 j-1 j n … i-1 i m . . . Address(a[i,j]) = address(a[1,1]) + ((((number of columns left j)* (col size)) + (number of elements above i)) * element size) = address(a[1,1]) + ((((j-1)* m) + (i-1)) * element_size) Generalization: Address(a[i,j]) = address(a[row_lb,col_lb]) + ((((j-col_lb)* m) + (i-row_lb)) * element_size)

2D Array - Column Major Order In general: address of an element = base address + element size * number of elements that precede it. 2D Array - Column Major Order 1 2 j-1 j n … i-1 i m . . . Example: Array A with following information: 15 rows and 5 columns Element size = 10 bytes Base address = 1000 Row lower bound =column lower bound = 0 Address( A[2,3]) = ? Address( A[2,3]) =1000 + (((3-0)*15) + (2-0)) *10 = 1470

Address relative to the beginning of the record Records Record Array Used when a collection of data is heterogeneous. All the data values have the same type. Different fields are not processed in the same way. Processed in the same way. Number of components is fixed. Fixed or variable. Field names are static. Subscripts are dynamic. Faster access. Slower access. Compile-time descriptor Record Name Type Offset . . . Address Field 1 Address relative to the beginning of the record Field n

Example: Employee record Compile-time descriptor Example: Employee record Field Name Field Type Field Size Name String 20 bytes Salary Double 8 bytes Year Integer 4 bytes Record Name Type = String Offset = 0 Salary Type = double Offset = 0+20 = 20 Year Type = integer Offset = 20 + 8 = 28 Base Address Field 1 Field 2 Address (Name) = base address + 0 Address (Salary) = base address + 20 Address (Year) = base address + 28 Field 3 Base Address Name Salary Year M 20 bytes 8 bytes 4 bytes 20 28 Memory Relative address = offset

Union A union is a type that may store different type values at different times during execution.

Variant Records A single record type with several variants. Specification: In Pascal: type PayType=(Salaried, Hourly); var Employee:record ID : integer; Dept: array[1..3] of char; Age : integer; case PayClass : PayType of Salaried:(MonthlyRate:real; StartDate :integer); Hourly :(HourRate:real; Reg :integer; Overtime:integer) end Common part Variant part

In C: (use the ‘union’ keyword) typedef enum {Salaried,Hourly} PayType; typedef struct { int ID; char Dept[3]; int Age; PayType PayClass; PayVariant pay; } EmployeeRecord; EmployeeRecord Employee; float MonthlyRate; int StartDate; } SalariedType ; float HourRate; int Reg, Overtime; } HourlyType ; typedef union { SalariedType sal_type; HourlyType hour_type; } PayVariant ; Common part Variant part

Implementation: