Array Record
Array Record
Array accessing
An array is an ordered sequence of identical
objects.
The ordering is determined by a scalar data object
(usually integer or enumeration data). This value is
called the subscript or index, and written as A[I]
for array A and subscript I.
Multidimensional arrays have more than one
subscript. A 2-dimensional array can be modeled as
the boxes on a rectangular grid.
The L-value for array element A[I,J]is given by the
accessing formula
2
J*d2
Array example
Given following array:
var A: array [7..12, 14..16] of real;
Give dope vector if array stored beginning at location 500.
d2 = 4 (real data)
d1 = (16-14+1) * 4 = 3 * 4 = 12
VO = 500 (7 * 12) (14 * 4) = 500 - 84 - 56 = 360
L-value(A[I,J]) = 360 + 12* I + 4* J
1. VO can be a positive or
negative value, and can have
an address that is before,
within, or after the actual
storage for the array:
2. In C, VO = since bounds start at 0.
Example: char A[25]
L-value(A[I]) = VO + (I-L1) * d1 = + I
* 1 = + I
8
Slices
Array slices
Given array : A[L1:U1, L2:U2]: Give d1, d2, and VO for
vector:
More on slices
Diagonal slices:
VO = L-value(A[L1,L2])
- d1*L1 - d2*L2
M1 = d1 + d2
Other
possibilities:
11
Associative arrays
Access information by name without having a
predefined ordering or enumeration:
Example: Names and grades for students in a class:
NAME[I] = name of Ith student
GRADE[I] = Grade for Ith student
Associative array: Use Name as index:
CLASS[name] will be grade.
Problem: Do not know enumeration before obtaining
data so dope vector method of accessing will not
work.
Implemented in Perl and in SNOBOL4 (as a table)
12
Perl example
%ClassList = (Michelle, `A', Doris, `B', Michael,
`D');
# % operator makes an associative array
Doris
B
Michael
D
Michelle
A
$I=
$I=
$I=
$I=
$I=
$I=
0
1
2
3
4
5
$y[$I]
$y[$I]
$y[$I]
$y[$I]
$y[$I]
$y[$I]
=
=
=
=
=
=
13
Records / Structs in C
Representation:
A sequence of objects:
record { A: object;
B: object;
C: object }
14
C Structure Example
struct animal_struct {
char dog;
unsigned long cat;
unsigned short pig;
char fox;
};
15
C Structure Example
struct animal_struct {
char dog; /* 1 byte */
unsigned long cat; /* 4 bytes */
unsigned short pig; /* 2 bytes */
char fox; /* 1 byte */
};
= 11 / 12 bits
16
struct animal_struct {
char dog; /* 1 byte */
u8 __pad0[3]; /* 3 bytes */
unsigned long cat; /* 4 bytes */
unsigned short pig; /* 2 bytes */
char fox; /* 1 byte */
Why?
u8 __pad1; /* 1 byte */
Memory banks
};
17
18
struct animal_struct {
unsigned long cat; /* 4 bytes */
unsigned short pig; /* 2 bytes */
char dog; /* 1 byte */
char fox; /* 1 byte */
};
For example 2?
19
Union types
typedef union { int X;
float Y;
char Z[4];} B;
B P;
Similar to records, except all have overlapping
(same) L-value.
20
Variant records
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
(Pascal Code)
21
22
Lists
Ordered sequence of objects
First member = head of List
Specification:
Rarely of fixed length
Rarely homogeneous
Without explicit attributes for the list members
LISP:
(Fn_name Data_1 Data_2 Data_n)
23
Lists (continued)
Implementation
Operations
Join => cons
Head
Tail
24
Lists (continued)
Implementation
Node
Type:
Head:
list / atom
25
Lists (continued)
Creation
Values: a, b, c
26
Lists (continued)
Implementation
27
Lists (continued)
Implementation
28