Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Part 1.

Similar presentations


Presentation on theme: "Chapter 6 Part 1."— Presentation transcript:

1 Chapter 6 Part 1

2 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]) = *10 = 1550

3 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: Would be stored as 3, 4, 7, 6, 2, 5, 1, 3, 8 Ex: Would be stored as 3, 6, 1, 4, 2, 3, 7, 5, 8

4 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 . . .

5 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 . . .

6 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)

7 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]) = (((2-0)*5) + (3-0)) *10 = 1130

8 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 . . .

9 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 . . .

10 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)

11 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]) = (((3-0)*15) + (2-0)) *10 = 1470

12 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

13 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 = = 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

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

15 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

16 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

17 Implementation:


Download ppt "Chapter 6 Part 1."

Similar presentations


Ads by Google