Chapter 2 Arrays and Structures
Chapter 2 Arrays and Structures
representation (possible)
Implemented by using consecutive memory.
In mathematical terms, we call this a
correspondence or a mapping.
person1.sex_info.sex = male;
person1.sex_info.u.beard =
FALSE;
and
person2.sex_info.sex = female;
person2.sex_info.u.children = 4;
Saturday)
(Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)
(basement, lobby, mezzanine, first, second)
(1941, 1942, 1943, 1944, 1945)
(a1, a2, a3, , an-1, an)
Implementation
Polynomial Addition
2.3 (6/10)
sparse
A(x) = 2x +1
B(x) = x4+10x3+3x2+1
1000
poly
A
B
<start, finish>
<0,1>
<2,5>
Analysis: O(n+m)
where n (m) is the number
of nonzeros in A (B).
Problem:
Compaction is required
when polynomials that are no longer needed.
(data movement takes time.)
sparse matrix
data structure?
5*3
15/15
8/36
6*6
transpose
row, column in
ascending order
columns
elements
Scan the array
columns times.
The array has
elements elements.
==> O(columns*elements)
Solution:
Cost:
Additional row_terms and
starting_pos arrays are required.
Let the two arrays row_terms
and starting_pos be shared.
columns
elements
columns
elements
transpose
Example:
Sa
Sb
Sc
d
e
f
g
Sd
Se
Sf
Sg
1
-1
0
4
2
6
BT =
a[0]
[1]
[2]
[3]
[4]
[5]
2
0
0
1
1
1
3 5
0 1
2 2
0 -1
1 4
2 6
3 -1
0 0
2 0
0
0
5
B =
3
-1
0
bt[0]
bt[1]
bt[2]
bt[3]
bt[4]
3
0
0
2
2
3 4
0 3
1 -1
0 2
2 5
0
0
0
2
0
5
b[0]
b[1]
b[2]
b[3]
b[4]
3
0
0
1
2
3 4
0 3
2 2
0 -1
2 5
ab
2.5 Representation of
multidimensional array (1/5)
The internal representation of multidimensional
arrays requires more complex addressing
formula.
If an array is declared a[upper0][upper1][uppern],
then it is easy to see that the number of elements in
the array is: n 1
upper
i 0
2.5 Representation of
multidimensional array (2/5)
2.5 Representation of
multidimensional array (3/5)
Row major order: A[i][j] : + i*upper1 + j
Column major order: A[i][j] : + j*upper0 + i
row0
row1
rowu0-1
col0
A[0][0]
A[1][0]
+ u1
A[u0-1][0]
+(u0-1)*u1
col1
A[0][1]
+ u0
A[1][1]
. . .
A[u0-1][1]
...
colu1-1
A[0][u1-1]
+(u1-1)* u0
A[1][u1-1]
...
A[u0-1][u1-1]
...
2.5 Representation of
multidimensional array (4/5)
To represent a three-dimensional array, A[upper0]
[upper1][upper2], we interpret the array as upper0
two-dimensional arrays of dimension
upper1upper2.
To locate a[i][j][k], we first obtain + i*upper1*upper2 as
the address of a[i][0][0] because there are i two
dimensional arrays of size upper1*upper2 preceding
this element.
+ i*upper1*upper2+j *upper2+k
as the address of a[i][j][k].
2.5 Representation of
multidimensional array (5/5)
Generalizing on the preceding discussion, we can
obtain the addressing formula for any element A[i0]
[i1][in-1] in an n-dimensional array declared as:
A[upper0][upper1][uppern-1]
The address for A[i0][i1][in-1] is:
Two strings are joined together by strcat(s, t), which stores the
result in s. Although s has increased in length by five, we have
no additional space in s to store the extra five characters. Our
compiler handled this problem inelegantly: it simply overwrote
the memory to fit in the extra five characters. Since we declared
t immediately after s, this meant that part of the word house
disappeared.