Sie sind auf Seite 1von 26

UCCD1024

Data Structures And


Algorithmic Problem Solving

Arrays
What you will learn?

 We will review and enhance our concept of pointer type,


especially using array as examples.
 We will see the actual implementation of an array.
 The concept of separating the soul from the body is
used here again.
 We will cover the concept on 2D array as an example

L03: Array: a type supported by pointer 2


Lecture 3: Array

 Definitions
 Array storing user-defined type Objects
 Multi-dimension array

 Read textbook T2:


Ed 5: Chapter 9 / Ed 6: Chapter 8

L03: Array: a type supported by pointer 3


int ar1[8];
Array definition: pointer supported type
 An array is a collection of related data items (of same
type) arranged in consecutive memory cells like:
(pointer
of array) int int int int int int int int
ar1 20 -1 5 6 99 -2 7 10

 Each array has 2 parts (see above diagram):


 One array name , e.g. ar1 on the left. (it is actually a
pointer [the soul concept in previous lecture])
 The physical memory of the array on the right. [the
body concept in previous lecture]
 ar1 stores address of the array object on the right.
 ar1 is a constant, i.e. content cannot be changed.

L03: Array: a type supported by pointer 4


Array definition: pointer supported type
 Each array element can be accessed using the array
name and an index like “ar1[3] = 6;” (where 3 is the
index) in one operation.
 This is called random access, i.e. can access any
element in one operation and do not need to traverse
from location 0 to location 3.
 This is important when you have 10000000 array
elements and want to access the middle elements.

(pointer
of array)

ar1 20 -1 5 6 99 -2 7 10
ar1[0] ar1[2] ar1[4] ar1[6]
ar1[1] ar1[3] ar1[5] ar1[7]

L03: Array: a type supported by pointer 5


Revisiting the Person definition

 With the new understanding of array, we can redraw the


diagram of Person in detail as shown below to the right.
 Usually, we prefer drawing/using the shorter version on
the left for illustration.

Mary Mary

name Non-primitive
name
phone Non-primitive
phone
money primitive money
spouse Non-primitive spouse

L03: Array: a type supported by pointer 6


Lecture 3: Array
 Definitions

 Array storing user-defined type Objects


 Multi-dimension array

L03: Array: a type supported by pointer 7


Array storing user-defined type Objects
 Here, we are not referring to primitive type array, like
"int ar1[8];"
 Examples of the user-defined type array can be
“Person ar1[8];” or “Person *ar2[5];”
 We are combining user-defined type and array together
 How complicate this can be? (We will see on next few
pages)
 You need to manipulate such type of array in future.

L03: Array: a type supported by pointer 8


Array storing user-defined type Objects
 One example is given below to define and use an array
with elements of user-defined type.
 Line 2 creates an array of 5 elements of Person-type as
shown below.
 Each array element is initialized at line 5.

1 void main() {
2 Person ar1[5]; // a combination of array and struct type
3 Person *ar2[5]; // a combination of array and pointer type
4
5 for (int j=0; j<5; j++) strcpy(arr1[ j].name, "Mary Jones");
6 for (int j=0; j<5; j++) {
7 arr2[j] = new Person;
8 strcpy(arr2[j]->name, "Mary Jones");
9 } name name name name name
10 } phone phone phone phone phone
ar1 money money money money money
spouse spouse spouse spouse spouse

ar1[0] ar1[2] ar1[4]


L03: Array: a type supported by pointer ar1[1] ar1[3] 9
Array storing user-defined type Objects (better)
 Line 3 on the other hand defines an array of pointers as shown at the
bottom diagram.
 Each array element is of the type of ‘Person *’.
 Line 7 then allocates dynamic memory for each pointer to point at.
 This definition supports body sharing operation; hence more
powerful (flexible).

Dynamic memory
name name name name name
phone phone phone phone phone
1 void main() { money money money money money
2 Person ar1[5]; // a combination
spouse of array
spouse and struct
spousetype spouse spouse

3 Person *ar2[5]; // a combination of array and pointer type


4
5 for (int j=0; j<5; j++) strcpy(arr1[ j].name, "Mary Jones");
6 for (int j=0; j<5; j++) {
7 arr2[ j] = new Person;
8 strcpy(arr2[ j]->name, "Mary Jones");
9 }
10 }
ar2 ar2[0] ar2[1] ar2[2] ar2[3] ar2[4]

L03: Array: a type supported by pointer 10


Lecture 3: Array
 Definitions
 Array storing user-defined type Objects

 Multi-dimensional array
 This is an optional topic
 Most students can skip this
 The discussed technique will not be needed
in exam
 It is good to listen only if you want to know
more and be more powerful

L03: Array: a type supported by pointer 11


Arrays: a type supported by pointer

Run
array-static vs dynamic.cpp

Using VS, show static vs dynamic 1-D array allocation

L03: Array: a type supported by pointer 12


Sample Output

L03: Array: a type supported by pointer 13


Multi-dimensional array
 We are interested in 3D (or even higher dimensional)
matrix.

L03: Array: a type supported by pointer 14


Multi-dimensional array

 The following illustrate the concept of 2-dimensional (2D)


array. It can be generalized to higher dimension arrays.
 A 2D array, a, of 3 rows by 4 columns can be seen as:
Column 0 Column 1 Column 2 Column 3

Row 0
a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]

Row 1
a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]

Row 2
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]

 The first index is for row while the second index is for the
column.
L03: Array: a type supported by pointer 15
Multi-dimensional array

 The previous 2D array can be created by declaration


statement like “int a[3][4];”
 One can also create 2D arrays with different numbers of
columns as shown below.
 In C++, we need the concept on next page to create
such arrays.
1 2
 We usually do not
need to use such 3
arrays. 4 5 6
a b
C d e
f g h i
L03: Array: a type supported by pointer 16
Dynamic multi-dimensional array creation

 A 2D array (e.g. int a [3][4];) can be implemented by


C++ compiler as a collection of 1D arrays as

 Instead of giving names to 3 different arrays, we wish to


give one and only one name to the array, the final design
becomes (see next 2 pages)

L03: Array: a type supported by pointer 17


Dynamic multi-dimensional array creation

 A 2D array (e.g. int a [3][4];) can be implemented by


C++ compiler as a collection of 1D arrays as
a

 Instead of giving names to 3 different arrays, we wish to


give one and only one name to the array, the final design
becomes (see next 2 pages)

L03: Array: a type supported by pointer 18


Dynamic multi-dimensional array creation
a[0][0] a[0][3]
a[0]
a
a[1][3]

a[2] a[2][3]

 This design allows us to use only one name a and go


from a to any of the connected components
 Each location can be referred as
 a[0], a[1], a[2]
 a[0][0], a[0][1], a[0][2], ….
 Except a, none of other pieces of memory has a name.
L03: Array: a type supported by pointer 19
a[0][0] a[0][3]
a[0]
a
a[1][3]
a[2] a[2][3]
 Following the concept from previous page, a 2D array can be
created one component at a time as
 int **a; // create the name of a 2D array
 a = new (int *[3]); // create the green array of 3
// elements with each having type int *
 a[0] = new (int [4]); // create 1st row
 a[1] = new (int [4]); // create 2nd row
 a[2] = new (int [4]); // create 3rd row
 In total, 4 1-dimensional arrays have been created.

a[0]
a

a[2]
L03: Array: a type supported by pointer 20
 The detail meanings of the syntax are:
 First let’s look at creating a as ‘int **a;’
 According to the design below, a is a pointer since
it has an arrow coming out.
 From a, going through two arrows, one can reach
an integer. Hence, a is defined as ‘int **a;’ to
account for this observation.
 Here, one ‘*’ represents one arrow.

a[0]
a

a[2]
L03: Array: a type supported by pointer 21
 Next let’s look at creating the green array as ‘a = new (int
*[3]);’
 According to the diagram below, the green array is a
piece of dynamic memory (since it has no name) with 3
elements. Hence, we must use ‘new ([3])’.
 According to the diagram, each element is a pointer
and going through one arrow, one can reach an
integer. Hence the type of each element is ‘int *’. The
syntax becomes ‘new (int *[3])’.
 Finally, the address of this array should be placed in a.
Hence, we have ‘a = new (int *[3]);’

a[0]
a

a[2]
L03: Array: a type supported by pointer 22
 Finally, let’s look at creating the first purple array as ‘a[0] =
new (int [4]);’.
 According to the diagram below, the purple array is a
piece of dynamic memory (since it has no name) with 4
elements. Hence, we must use ‘new ([4])’.
 According to the diagram, each element is an integer.
Hence, the syntax becomes ‘new (int [4])’.
 Finally, the address of this array should be placed in
a[0]. Hence, we have ‘a[0] = new (int [4]);’

a[0]
a

a[2]
L03: Array: a type supported by pointer 23
Multi-dimensional array
 Obviously, we prefer simple way to create an array like
int a[3][4];
 The method from previous page is useful when we need
to create array dynamically, i.e. the dimensions [3][4] are
known only at run time.

L03: Array: a type supported by pointer 24


Summary

 We have reviewed the following concepts on array


 It is a type supported by pointers as for example like
(pointer
of array)

ar1 20 -1 5 6 99 -2 7 10
ar1[0] ar1[2] ar1[4] ar1[7]

 Array is important since it can do random access.


 Have learnt two examples to understand arrays with
elements of user-defined type on pages 11 and 12.
 We have gone through the internal makeup of a 2
dimensional array defined dynamically.

L03: Array: a type supported by pointer 25


Next lecture

 Pointer arithmetic.

L03: Array: a type supported by pointer 26

Das könnte Ihnen auch gefallen