Data Structures, Algorithms, & Applications in C++
Chapter 7, Exercise 9
- (a)
-
C++ uses the array of arrays representation. When
m = 10
and n = 2, we have
1 one-dimensional array of size
10 (the elements of this array are C++ pointers)
and
10 one-dimensional arrays of size
2 (the elements of these arrays are integers).
So, the total space is
1 x 10 x 4 + 10 x 2 x 4 = 120 bytes.
When a single one-dimensional array is used (i.e., when the elements
are mapped using (say) row-major order), the space required
is 10 x 2 x 4 = 80 bytes.
The ratio is 120 / 80 = 1.5.
For general m and n,
the array of arrays representation requires
1 x m x 4 + m x n x 4 = 4mn + 4m
bytes.
The one-dimensional mapping takes
m x n x 4 = 4mn
bytes.
- (b)
-
The ratio is
(4mn + 4m) / (4mn) = 1 + 4/n.
This is maximum when n is least.
For a two-dimensional array, n >= 2.
So, the maximum value of the ratio is
1 + 4/2 = 3.