89

Cpt111 Array

  • Upload
    ct-anum

  • View
    20

  • Download
    0

Embed Size (px)

DESCRIPTION

just a slide explaining the basic of array

Citation preview

Page 1: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

ArraysCPT111 Principles of Programming

Bahari Belaton

School of Computer SciencesUniversiti Sains Malaysia

Week 12 & 13 - Dec 2012

B Belaton Arrays

Page 2: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Outlines of Sub-Topics1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 3: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 4: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Introduction to Array

An array is a collection of data items of the same type

Individual elements in array are accessed by an integer index orsubscript : v[i];

An example of an array variable age that stores 10 integervalues :

int age[10];

Indexing array start with 0, so if the array has a size of 10,then a valid index ranges from 0 - 9.

An array name is a pointer variable which stores the baseaddress (or o�set) of an array.

Array fall under the category of static storage class � �xed size.

B Belaton Arrays

Page 5: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

The syntax of array declaration is as follow :

type_name variable_name [ size ] ;type_name variable_name [ ] = {comma separated initial values};

Description of the syntax :

I type_name can be any built-in types such as int, float, char,

double, or user-de�ned type such as class

I variable_name any valid identi�er

I initial_size should be numeral value, this may include integervalue, expression that evaluate to integer, or symbolic constantprede�ned with integer value.

I initial_size is optional, an array declared with an emptyinitial_size component is considered unde�ned size (more onthis later).

B Belaton Arrays

Page 6: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

The syntax of array declaration is as follow :

type_name variable_name [ size ] ;type_name variable_name [ ] = {comma separated initial values};

Description of the syntax :

I type_name can be any built-in types such as int, float, char,

double, or user-de�ned type such as class

I variable_name any valid identi�er

I initial_size should be numeral value, this may include integervalue, expression that evaluate to integer, or symbolic constantprede�ned with integer value.

I initial_size is optional, an array declared with an emptyinitial_size component is considered unde�ned size (more onthis later).

B Belaton Arrays

Page 7: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

The syntax of array declaration is as follow :

type_name variable_name [ size ] ;type_name variable_name [ ] = {comma separated initial values};

Description of the syntax :

I type_name can be any built-in types such as int, float, char,

double, or user-de�ned type such as class

I variable_name any valid identi�er

I initial_size should be numeral value, this may include integervalue, expression that evaluate to integer, or symbolic constantprede�ned with integer value.

I initial_size is optional, an array declared with an emptyinitial_size component is considered unde�ned size (more onthis later).

B Belaton Arrays

Page 8: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

The syntax of array declaration is as follow :

type_name variable_name [ size ] ;type_name variable_name [ ] = {comma separated initial values};

Description of the syntax :

I type_name can be any built-in types such as int, float, char,

double, or user-de�ned type such as class

I variable_name any valid identi�er

I initial_size should be numeral value, this may include integervalue, expression that evaluate to integer, or symbolic constantprede�ned with integer value.

I initial_size is optional, an array declared with an emptyinitial_size component is considered unde�ned size (more onthis later).

B Belaton Arrays

Page 9: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Anatomical view of an array is illustrated below :

B Belaton Arrays

Page 10: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 11: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Working with arrays

There are two ways of initialising an array :

� Initialisation during declaration

� Initialisation through C++ statements (typically requires loopcontrol structure)

In the �rst method, an array is initialised in the declaration part - assuch it will be initialised once. The value to initialise an array hasto be provided explicitly in the declaration.

Example

#define SIZE 10 // same as const int SIZE = 10;

int u[SIZE];

int x[SIZE] = {1,2,3,4,5};

int y[SIZE-5] = {1,2,3,4,5};

int z[] = {1,2,3};

B Belaton Arrays

Page 12: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Working with arrays

There are two ways of initialising an array :

� Initialisation during declaration

� Initialisation through C++ statements (typically requires loopcontrol structure)

In the �rst method, an array is initialised in the declaration part - assuch it will be initialised once. The value to initialise an array hasto be provided explicitly in the declaration.

Example

#define SIZE 10 // same as const int SIZE = 10;

int u[SIZE];

int x[SIZE] = {1,2,3,4,5};

int y[SIZE-5] = {1,2,3,4,5};

int z[] = {1,2,3};

B Belaton Arrays

Page 13: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Working with arrays

There are two ways of initialising an array :

� Initialisation during declaration

� Initialisation through C++ statements (typically requires loopcontrol structure)

In the �rst method, an array is initialised in the declaration part - assuch it will be initialised once. The value to initialise an array hasto be provided explicitly in the declaration.

Example

#define SIZE 10 // same as const int SIZE = 10;

int u[SIZE];

int x[SIZE] = {1,2,3,4,5};

int y[SIZE-5] = {1,2,3,4,5};

int z[] = {1,2,3};

B Belaton Arrays

Page 14: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Working with arrays

There are two ways of initialising an array :

� Initialisation during declaration

� Initialisation through C++ statements (typically requires loopcontrol structure)

In the �rst method, an array is initialised in the declaration part - assuch it will be initialised once. The value to initialise an array hasto be provided explicitly in the declaration.

Example

#define SIZE 10 // same as const int SIZE = 10;

int u[SIZE];

int x[SIZE] = {1,2,3,4,5};

int y[SIZE-5] = {1,2,3,4,5};

int z[] = {1,2,3};

B Belaton Arrays

Page 15: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Description of the example :

u is an array of integer of size 10. It is uninitialised hence bydefault (for integer type only) it contains zero values

x also an array of integer (size 10), but the �rst �ve elementshave been initialised with value 1, 2, 3, 4, and 5 respectively.The remaining �ve elements has the default value i.e. zero.

y is same as x, except it size is 5 (SIZE - 5), and all of itselements have been initialised with value 1, 2, 3, 4, and 5.This case illustrate the use of an expression to determine thearray size.

z also an array of integer, but it size is unde�ned. In such caseit size is determined by the number of values initialised to it.In this example, it has a size of 3 because of the three valuesinitialised to its element.

B Belaton Arrays

Page 16: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Description of the example :

u is an array of integer of size 10. It is uninitialised hence bydefault (for integer type only) it contains zero values

x also an array of integer (size 10), but the �rst �ve elementshave been initialised with value 1, 2, 3, 4, and 5 respectively.The remaining �ve elements has the default value i.e. zero.

y is same as x, except it size is 5 (SIZE - 5), and all of itselements have been initialised with value 1, 2, 3, 4, and 5.This case illustrate the use of an expression to determine thearray size.

z also an array of integer, but it size is unde�ned. In such caseit size is determined by the number of values initialised to it.In this example, it has a size of 3 because of the three valuesinitialised to its element.

B Belaton Arrays

Page 17: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Description of the example :

u is an array of integer of size 10. It is uninitialised hence bydefault (for integer type only) it contains zero values

x also an array of integer (size 10), but the �rst �ve elementshave been initialised with value 1, 2, 3, 4, and 5 respectively.The remaining �ve elements has the default value i.e. zero.

y is same as x, except it size is 5 (SIZE - 5), and all of itselements have been initialised with value 1, 2, 3, 4, and 5.This case illustrate the use of an expression to determine thearray size.

z also an array of integer, but it size is unde�ned. In such caseit size is determined by the number of values initialised to it.In this example, it has a size of 3 because of the three valuesinitialised to its element.

B Belaton Arrays

Page 18: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Description of the example :

u is an array of integer of size 10. It is uninitialised hence bydefault (for integer type only) it contains zero values

x also an array of integer (size 10), but the �rst �ve elementshave been initialised with value 1, 2, 3, 4, and 5 respectively.The remaining �ve elements has the default value i.e. zero.

y is same as x, except it size is 5 (SIZE - 5), and all of itselements have been initialised with value 1, 2, 3, 4, and 5.This case illustrate the use of an expression to determine thearray size.

z also an array of integer, but it size is unde�ned. In such caseit size is determined by the number of values initialised to it.In this example, it has a size of 3 because of the three valuesinitialised to its element.

B Belaton Arrays

Page 19: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Alternatively, an array is initialised in a C++ program eitherexplicitly or implicitly. This approach has advantage compared tothe previous method, because array can be initialised many times asrequired (not once).

Example : Explicit (left) & Implicit (right) element assignment

#define SIZE 10 | #define SIZE 10

|

int x[SIZE]; | int x[SIZE], i;

|

x[0]=1; x[1]=2; x[2]=3; | for(i=0; i < SIZE; i++)

x[4]=4; x[5]=5; | x[i] = i + 1;

I Explicit � tedious and ine�cient for large array; unavoidable incases if the value to be assigned has not particular pattern toderive �implicitly�

I Implicit � more concise and perhaps e�cient (using loopcontrol structure) provided there is certain pattern e.g.increment by 1.

B Belaton Arrays

Page 20: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Alternatively, an array is initialised in a C++ program eitherexplicitly or implicitly. This approach has advantage compared tothe previous method, because array can be initialised many times asrequired (not once).

Example : Explicit (left) & Implicit (right) element assignment

#define SIZE 10 | #define SIZE 10

|

int x[SIZE]; | int x[SIZE], i;

|

x[0]=1; x[1]=2; x[2]=3; | for(i=0; i < SIZE; i++)

x[4]=4; x[5]=5; | x[i] = i + 1;

I Explicit � tedious and ine�cient for large array; unavoidable incases if the value to be assigned has not particular pattern toderive �implicitly�

I Implicit � more concise and perhaps e�cient (using loopcontrol structure) provided there is certain pattern e.g.increment by 1.

B Belaton Arrays

Page 21: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Alternatively, an array is initialised in a C++ program eitherexplicitly or implicitly. This approach has advantage compared tothe previous method, because array can be initialised many times asrequired (not once).

Example : Explicit (left) & Implicit (right) element assignment

#define SIZE 10 | #define SIZE 10

|

int x[SIZE]; | int x[SIZE], i;

|

x[0]=1; x[1]=2; x[2]=3; | for(i=0; i < SIZE; i++)

x[4]=4; x[5]=5; | x[i] = i + 1;

I Explicit � tedious and ine�cient for large array; unavoidable incases if the value to be assigned has not particular pattern toderive �implicitly�

I Implicit � more concise and perhaps e�cient (using loopcontrol structure) provided there is certain pattern e.g.increment by 1.

B Belaton Arrays

Page 22: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 23: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Accessing Array Element

Generally, there are two ways to access (retrieve or assign) arrayelement :

� Using index or subscript notation,

� Using o�set or base address notation

Example

int x[10], total, i, j;

total = x[1] + x[9];

i = 3; j = 4;

total = x[i] + x[j];

x[j+4] = 100 * x[i];

In the �rst expression, the value of 2thelement (x[1]) of array x is retrieved andadded to the retrieved value of 10th element(x[9]), than the result is assigned to variabletotal.

In the last expression, 100 is multiplied withthe retrieved value of 4th element (because i

was initially assigned to 3) of array x. Theresult of is assigned to the 9th element(because j = 4) of array x.

Warning: Choosing an index outside the valid range of array's sizewill result in runtime error.

B Belaton Arrays

Page 24: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Accessing Array Element

Generally, there are two ways to access (retrieve or assign) arrayelement :

� Using index or subscript notation,

� Using o�set or base address notation

Example

int x[10], total, i, j;

total = x[1] + x[9];

i = 3; j = 4;

total = x[i] + x[j];

x[j+4] = 100 * x[i];

In the �rst expression, the value of 2thelement (x[1]) of array x is retrieved andadded to the retrieved value of 10th element(x[9]), than the result is assigned to variabletotal.

In the last expression, 100 is multiplied withthe retrieved value of 4th element (because i

was initially assigned to 3) of array x. Theresult of is assigned to the 9th element(because j = 4) of array x.

Warning: Choosing an index outside the valid range of array's sizewill result in runtime error.

B Belaton Arrays

Page 25: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Accessing Array Element

Generally, there are two ways to access (retrieve or assign) arrayelement :

� Using index or subscript notation,

� Using o�set or base address notation

Example

int x[10], total, i, j;

total = x[1] + x[9];

i = 3; j = 4;

total = x[i] + x[j];

x[j+4] = 100 * x[i];

In the �rst expression, the value of 2thelement (x[1]) of array x is retrieved andadded to the retrieved value of 10th element(x[9]), than the result is assigned to variabletotal.

In the last expression, 100 is multiplied withthe retrieved value of 4th element (because i

was initially assigned to 3) of array x. Theresult of is assigned to the 9th element(because j = 4) of array x.

Warning: Choosing an index outside the valid range of array's sizewill result in runtime error.

B Belaton Arrays

Page 26: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Accessing Array Element

Generally, there are two ways to access (retrieve or assign) arrayelement :

� Using index or subscript notation,

� Using o�set or base address notation

Example

int x[10], total, i, j;

total = x[1] + x[9];

i = 3; j = 4;

total = x[i] + x[j];

x[j+4] = 100 * x[i];

In the �rst expression, the value of 2thelement (x[1]) of array x is retrieved andadded to the retrieved value of 10th element(x[9]), than the result is assigned to variabletotal.

In the last expression, 100 is multiplied withthe retrieved value of 4th element (because i

was initially assigned to 3) of array x. Theresult of is assigned to the 9th element(because j = 4) of array x.

Warning: Choosing an index outside the valid range of array's sizewill result in runtime error.

B Belaton Arrays

Page 27: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Accessing Array Element

Generally, there are two ways to access (retrieve or assign) arrayelement :

� Using index or subscript notation,

� Using o�set or base address notation

Example

int x[10], total, i, j;

total = x[1] + x[9];

i = 3; j = 4;

total = x[i] + x[j];

x[j+4] = 100 * x[i];

In the �rst expression, the value of 2thelement (x[1]) of array x is retrieved andadded to the retrieved value of 10th element(x[9]), than the result is assigned to variabletotal.

In the last expression, 100 is multiplied withthe retrieved value of 4th element (because i

was initially assigned to 3) of array x. Theresult of is assigned to the 9th element(because j = 4) of array x.

Warning: Choosing an index outside the valid range of array's sizewill result in runtime error.

B Belaton Arrays

Page 28: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro ArrayArray InitialisationWorking with array

Using o�set notation

I Access to array's element can be done relative to o�set or baseaddress of an array, i.e. array's name.

I This approach is best explained if we draw a �hypothetical�memory map diagram of the program (see below)

Example-------------------------

int x[10], total, i, j;

total = *(x+1) + *(x+2);

i = 3; j = 4;

total = *(x+i) + *(x+j);

-------------------------

total = ∗(x + 1) + ∗(x + 2)

= ∗(1200 + 1) + ∗(1200 + 2)

= ∗(1202) + ∗(1204)= 20 + 30⇒ 50

total = ∗(x + 3) + ∗(x + 4)

= ∗(1200 + 3) + ∗(1200 + 4)

= ∗(1206) + ∗(1208)= 40 + 50⇒ 90.

var value addr

i 3 1000

j 4 1002

total ?? 1004

... ?? ...

... ?? ...

x[0] 10 1200

x[1] 20 1202

x[2] 30 1204

x[3] 40 1206

x[4] 50 1208

... ?? ...

... ?? ...

B Belaton Arrays

Page 29: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 30: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Introduction to 2D Array

� Typical example of 2D array is matrix or table which consist ofrows (horizontal) and column (vertical).

� Declaring 2D array is not much di�erent than declaring 1Darray, except now we've additional [] pair to indicate the extradimension.

Example#define ROW 6

#define COLUMN 4

int x[ROW][COLUMN];

col 0 col 1 col 2 col 3x[0][0] x[0][1] x[0][2] x[0][3] row 0x[1][0] x[1][1] x[1][2] x[1][3] row 1x[2][0] x[2][1] x[2][2] x[2][3] row 2x[3][0] x[3][1] x[3][2] x[3][3] row 3x[4][0] x[4][1] x[4][2] x[4][3] row 4x[5][0] x[5][1] x[5][2] x[5][3] row 5

B Belaton Arrays

Page 31: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 32: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Same as 1D, that is initialisation can be done either during arraydeclaration or via program statements.

Example 1 : int x[2][3] = {{1,2,3},{4,5,6}};

I x is a 2D integer array (2 rows and 3 columns), initialised explicitly with value 1,2, 3, 4, 5, and 6 respectively. The inner {} pairs distinguishes the two rows.

x[0][0] x[0][1] x[0][2]

1 2 3

4 5 6

x[1][0] x[1][1] x[1][2]

B Belaton Arrays

Page 33: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Same as 1D, that is initialisation can be done either during arraydeclaration or via program statements.

Example 1 : int x[2][3] = {{1,2,3},{4,5,6}};

I x is a 2D integer array (2 rows and 3 columns), initialised explicitly with value 1,2, 3, 4, 5, and 6 respectively. The inner {} pairs distinguishes the two rows.

Example 2 : int y[][3] = {1,2,3,4,5,6,7};

I y is also a 2D integer array, but its row is determined by the number of itemsinitialised to it ⇒ it has 3 columns.

y[0][0] y[0][1] y[0][2]

1 2 3

4 5 6

7 0 0

y[2][0] y[2][1] y[2][2]

B Belaton Arrays

Page 34: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Same as 1D, that is initialisation can be done either during arraydeclaration or via program statements.

Example 1 : int x[2][3] = {{1,2,3},{4,5,6}};

I x is a 2D integer array (2 rows and 3 columns), initialised explicitly with value 1,2, 3, 4, 5, and 6 respectively. The inner {} pairs distinguishes the two rows.

Example 3 : int z[2][3] = {{1,2},{4}};

I z is also a 2D integer array, where only three elements have been explicitlyinitialised. These are 1st (z[0][0]), 2nd (z[0][1]) and 4th (z[1][0])elements.

z[0][0] z[0][1] z[0][2]

1 2 0

4 0 0

z[1][0] z[1][1] z[1][2]

B Belaton Arrays

Page 35: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Nested Loops �2D Array Initialisation

To initialise 2D array, used nested for loops. Why?

Example of Initializing 2D array x using nested for loops

int x[10][12];

int i,j;

for(i=0; i < 10; i++)

for(j=0; j < 12; j++)

x[i][j] = j + 1;

Initialised array x with value 1 to 12 for each column in therow.

Note also that the inner for loop varies faster i.e. the columnpart, then followed by the outer for loop i.e. the row part.

B Belaton Arrays

Page 36: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 37: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Passing array to function

We can do this in two di�erent ways :

� Pass each element of array to function (call by value method), or

� Pass the base address of an array - or array's name (call byreference)

Call by value vs. call by reference (for array)

Call be reference is preferred because we only need to pass single valuei.e. the base address. Only need to pass the array's name and size of thearray to the function

B Belaton Arrays

Page 38: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Passing array to function

We can do this in two di�erent ways :

� Pass each element of array to function (call by value method), or

� Pass the base address of an array - or array's name (call byreference)

Call by value vs. call by reference (for array)

Call be reference is preferred because we only need to pass single valuei.e. the base address. Only need to pass the array's name and size of thearray to the function

B Belaton Arrays

Page 39: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Passing array to function

We can do this in two di�erent ways :

� Pass each element of array to function (call by value method), or

� Pass the base address of an array - or array's name (call byreference)

Call by value vs. call by reference (for array)

Call be reference is preferred because we only need to pass single valuei.e. the base address. Only need to pass the array's name and size of thearray to the function

B Belaton Arrays

Page 40: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Call by value � passing individual element

Example of passing array's elements to function

int main()

{

int x[100] = {1,2,3,4,5,6};

modifyArray(x[0],x[1],x[2],x[3],x[4],x[5]);

cout <<x[0]<<x[1]<<x[2]<<x[3]<<x[4]<<x[5]<< endl;

return 0;

}

void modifyArray(int x1, int x2, int x3, int x4, int x5, int x6)

{

cout << "Total = " << x1+x2+x3+x4+x5+x6 << endl;

x1 += 1; x2 += 1; x3 += 1; x4 += 1; x5 += 1; x6 += 1;

}

The number of parameter to be passed to function is determined by the size ofan array.Thus if all 100 elements to be processed, then the function header of functionmodifyArray() need to include 100 formal parameters !!

B Belaton Arrays

Page 41: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to 2D ArrayInitialise 2D ArrayPassing Array to function

Call by reference � passing array name

Example below shows an alternative and better approach to passingarray to function :

Array name (base address/o�set) passed to a function

int main() {

int x[100] = {1,2,3,4,5,6};

modifyArray(x,100);

cout <<x[0]<<x[1]<<x[2]<<x[3]<<x[4]<<x[5]<< endl;

}

void modifyArray(int x[], int size) {

int i,total;

for(i=0; i < size; i++) {

total += x[i];

x[i] += 1;

}

cout<<"Total = " << total << endl;

}

B Belaton Arrays

Page 42: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 43: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Character vs Strings

I Character arrays are array of values of character type char

I The char type denotes an individual character constant(delimited by single quotes) e.g. char input = `y';

I Note that `y' is di�erent from "y" (a string containing asingle character).

I Each character is encoded as an integer value � ASCIIencoding scheme, e.g. character `y' is encoded as number121

B Belaton Arrays

Page 44: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Declaring a Character Array

The syntax of character array declaration is as follow :

char variable_name [ size ] ;char variable_name [ ] = {string of characters};

Below is an example of character array declaration (andinitialisation) :

char greeting[] = "Hello"; greeting = H [0]

e [1]

l [2]

l [3]

o [4]

\0 [5] L99zero terminator

Note : In the above example zero terminator character isautomatically inserted.

B Belaton Arrays

Page 45: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Declaring a Character Array

The syntax of character array declaration is as follow :

char variable_name [ size ] ;char variable_name [ ] = {string of characters};

Below is an example of character array declaration (andinitialisation) :

char greeting[] = "Hello"; greeting = H [0]

e [1]

l [2]

l [3]

o [4]

\0 [5] L99zero terminator

Note : In the above example zero terminator character isautomatically inserted.

B Belaton Arrays

Page 46: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Declaring a Character Array

The syntax of character array declaration is as follow :

char variable_name [ size ] ;char variable_name [ ] = {string of characters};

Below is an example of character array declaration (andinitialisation) :

char greeting[] = "Hello"; greeting = H [0]

e [1]

l [2]

l [3]

o [4]

\0 [5] L99zero terminator

Note : In the above example zero terminator character isautomatically inserted.

B Belaton Arrays

Page 47: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 48: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Initialising Character Array

When creating/initialising your own character arrays, crucial to add`\0' character (see below) :

Example : Crucial to insert `\0' character

char mystring[5];

for (i=0; i,4; i++)

mystring[i] = greeting[i];

mystring[4] = `\0'; // Add zero terminator

Terminator character is used by many string functions (instandard library) as marker to indicate end of string e.g.strlen, strcpy, strcat, etc.

Failing to include `\0' may result in program crashing

B Belaton Arrays

Page 49: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Intro to char arrayInitialising Char Array

Character Array vs. String

} Character array is a legacy inherited from C, hence it is still inused in C++ but when there is option always used string

class. Why?

} To convert a string into a character array use c_str memberfunction of the string class.

} For instance, in cstdlib header, there is a functionint atoi(const char s[]);

that converts a character array containing digits into itsinteger value.

Example showing how to use c_str() member function

char year[] = "1999"; | string year = "1999";

int y = atoi(year); // y is integer 1999 | int y = atoi(year.c_str());

B Belaton Arrays

Page 50: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 51: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Processing array

Array is useful data structure (known as linear data structure),because it concisely store and group data of similar type, aswell as providing e�cient method for accessing data stored init for further processing.What kind of operations that we can do to array datastructure :� Sorting the contents of array in user de�ned order such as

ascending or descending,� Searching the contents of array for a speci�c value� Concatenating or merging the contents of two arrays (esp. for

character arrays).� Adding or Deleting array elements, etc.

Here we will concentrate on the two most commonmanipulations to array data structure i.e. sorting andsearching.

B Belaton Arrays

Page 52: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Outline1 Array Data Type

Introduction to arrayInitialising arraysAccessing Array Element

2 Two-Dimensional ArrayIntroduction to 2D arrayInitialising 2D arrayPassing Array to function

3 Array of CharactersIntroduction to character arrayInitialising character array

4 Processing Array (if time permitted)What kind of array processing?Searching & Sorting

B Belaton Arrays

Page 53: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 54: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 55: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 56: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 57: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 58: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 59: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Sorting Technique � Bubble Sort (Concept)

Sorting is a process of rearranging the content of array so that givenan arbitrary size of unsorted array, the result will be the same arraywith its contents sorted in a particular order i.e. ascending ordescending.

There are many sorting techniques, some works well in speci�capplications while others are designed to tackle general sortingproblem. Below is pseudocode (simplify version) of Bubble Sortmethod for sorting elements of array in ascending order :

1: Make several passes through the array (N - 1 times)2: In each pass compare the successive pair of array3: If they're not in ascending order, swap them4: Otherwise leave them in their original position5: Do 2 until the array is sorted

B Belaton Arrays

Page 60: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 61: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 62: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 63: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 64: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 65: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 66: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 67: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 68: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

B Belaton Arrays

Page 69: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 89

x[8] 45

x[9] 37

B Belaton Arrays

Page 70: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 89

x[8] 45

x[9] 37

B Belaton Arrays

Page 71: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 89

x[9] 37

B Belaton Arrays

Page 72: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 89

x[9] 37

B Belaton Arrays

Page 73: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

B Belaton Arrays

Page 74: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

B Belaton Arrays

Page 75: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 68

x[8] 37

x[9] 89

B Belaton Arrays

Page 76: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 68

x[8] 37

x[9] 89

B Belaton Arrays

Page 77: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

B Belaton Arrays

Page 78: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

B Belaton Arrays

Page 79: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

B Belaton Arrays

Page 80: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

B Belaton Arrays

Page 81: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

B Belaton Arrays

Page 82: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Bubble sort example

Ori

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 89

x[7] 68

x[8] 45

x[9] 37

P1

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 68

x[7] 45

x[8] 37

x[9] 89

P2

x[0] 2

x[1] 4

x[2] 6

x[3] 8

x[4] 10

x[5] 12

x[6] 45

x[7] 37

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

B Belaton Arrays

Page 83: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Searching Techniques (Concept)

Searching is a process that inspect the content of an arbitrary sizedarray either to �nd a match with the search value (equality test) orto �nd matches of values that satis�ed certain prede�ned condition(greater than, less than, etc).

Similar to sorting, there also loads of searching techniques, here wewill focus on the two simple searching methods known as linearsearch and binary search.

The simplest (and hence ine�cient) method to �nd if a particularvalue (key value) matches one (or more) elements of an array is bysearching and comparing from the start of the array, only stop if amatch is found or if the end of array is reached.

This method known as linear search is suitable for small unsortedarray. In the next slide an example of linear search function whichreturn the index to array x if match is found or -1 if no match isfound.

B Belaton Arrays

Page 84: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Simple Linear Search

Linear Search

int linearSearch(int x[], int key, int size) {

int i;

for(i=0; i < size; i++)

if (key == x[i])

return i;

return -1;

}

A reasonably fast (and to some degree e�cient) searching techniqueis called Binary search. This method is fast because it is relying onthe fact that the target array we wanted to do the searching must

be already sorted.

Hence, it simpli�es the search mechanism by sub-dividing the searchspace into half (binary) each time the search operation is performed.Binary search can also be implemented using recursive functionconcept.

B Belaton Arrays

Page 85: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Binary Searching Technique

Sample binarySearch function - not a complete example

int binarySearch(int b[], int key, int low, int high){

int mid;

while (low <= high) {

mid = (low + high) / 2;

if (key == b[mid]) return mid;

else if (key < b[mid]) high = mid - 1;

else low = mid + 1;

}

return -1;

}

In the function binarySearch(), low is the lower boundary ofsearch space, and high is the upper boundary of search space. Ateach iteration of the while loop low or high updated accordinglybased on the test condition in the if statement. If match is found,the index to array b is returned, otherwise -1 i returned to thecalling function.

B Belaton Arrays

Page 86: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Binary Searching Technique � Illustration

Search key value = 8

Ori

1st

x[0] 2 ←low

x[1] 6

x[2] 4

x[3] 8

x[4] 10 ←mid

2nd

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89 ←high

P1

1stx[0] 2 ←low

x[1] 6 ←mid

2ndx[2] 4

x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P2

x[0] 2

x[1] 6

1st x[2] 4 ←low,mid

2nd x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8 ←low,mid,high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

B Belaton Arrays

Page 87: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Binary Searching Technique � Illustration

Search key value = 8

Ori

1st

x[0] 2 ←low

x[1] 6

x[2] 4

x[3] 8

x[4] 10 ←mid

2nd

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89 ←high

P1

1stx[0] 2 ←low

x[1] 6 ←mid

2ndx[2] 4

x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P2

x[0] 2

x[1] 6

1st x[2] 4 ←low,mid

2nd x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8 ←low,mid,high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

B Belaton Arrays

Page 88: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Binary Searching Technique � Illustration

Search key value = 8

Ori

1st

x[0] 2 ←low

x[1] 6

x[2] 4

x[3] 8

x[4] 10 ←mid

2nd

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89 ←high

P1

1stx[0] 2 ←low

x[1] 6 ←mid

2ndx[2] 4

x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P2

x[0] 2

x[1] 6

1st x[2] 4 ←low,mid

2nd x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8 ←low,mid,high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

B Belaton Arrays

Page 89: Cpt111 Array

Array Data TypeTwo-Dimensional Array

Array of CharactersProcessing Array (if time permitted)

Array's OperationSearch & Sort

Binary Searching Technique � Illustration

Search key value = 8

Ori

1st

x[0] 2 ←low

x[1] 6

x[2] 4

x[3] 8

x[4] 10 ←mid

2nd

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89 ←high

P1

1stx[0] 2 ←low

x[1] 6 ←mid

2ndx[2] 4

x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P2

x[0] 2

x[1] 6

1st x[2] 4 ←low,mid

2nd x[3] 8 ←high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

P3

x[0] 2

x[1] 6

x[2] 4

x[3] 8 ←low,mid,high

x[4] 10

x[5] 12

x[6] 37

x[7] 45

x[8] 68

x[9] 89

B Belaton Arrays