You are on page 1of 89

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Arrays
CPT111 Principles of Programming

Bahari Belaton

School of Computer Sciences Universiti Sains Malaysia


Week 12 & 13 - Dec 2012

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outlines of Sub-Topics

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro Array Array Initialisation Working with array

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working 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

subscript
values :

v[i]; age

index

or

An example of an array variable

that stores 10 integer

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 base address (or oset) of an array. Array fall under the category of static storage class  xed size.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

The syntax of array declaration is as follow :

type_name type_name

variable_name variable_name

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

Description of the syntax :

type_name can be any built-in types such as int, float, char, double, or user-dened type such as class

variable_name any valid identier


initial_size should be numeral value, this may include integer

value, expression that evaluate to integer, or symbolic constant predened with integer value. this later).

initial_size is optional, an array declared with an empty initial_size component is considered undened size (more on

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

The syntax of array declaration is as follow :

type_name type_name

variable_name variable_name

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

Description of the syntax :

type_name can be any built-in types such as int, float, char, double, or user-dened type such as class

variable_name any valid identier


initial_size should be numeral value, this may include integer

value, expression that evaluate to integer, or symbolic constant predened with integer value. this later).

initial_size is optional, an array declared with an empty initial_size component is considered undened size (more on

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

The syntax of array declaration is as follow :

type_name type_name

variable_name variable_name

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

Description of the syntax :

type_name can be any built-in types such as int, float, char, double, or user-dened type such as class

variable_name any valid identier


initial_size should be numeral value, this may include integer

value, expression that evaluate to integer, or symbolic constant predened with integer value. this later).

initial_size is optional, an array declared with an empty initial_size component is considered undened size (more on

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

The syntax of array declaration is as follow :

type_name type_name

variable_name variable_name

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

Description of the syntax :

type_name can be any built-in types such as int, float, char, double, or user-dened type such as class

variable_name any valid identier


initial_size should be numeral value, this may include integer

value, expression that evaluate to integer, or symbolic constant predened with integer value. this later).

initial_size is optional, an array declared with an empty initial_size component is considered undened size (more on

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Anatomical view of an array is illustrated below :

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro Array Array Initialisation Working with array

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Working with arrays


There are two ways of initialising an array : Initialisation during declaration Initialisation through C++ statements (typically requires loop control structure) In the rst method, an array is initialised in the declaration part - as such it will be initialised

once.

The value to initialise an array has

to be provided explicitly in the declaration.

Example
#define SIZE 10 // same as const int SIZE = 10; int int int int u[SIZE]; x[SIZE] = {1,2,3,4,5}; y[SIZE-5] = {1,2,3,4,5}; z[] = {1,2,3};

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Working with arrays


There are two ways of initialising an array : Initialisation during declaration Initialisation through C++ statements (typically requires loop control structure) In the rst method, an array is initialised in the declaration part - as such it will be initialised

once.

The value to initialise an array has

to be provided explicitly in the declaration.

Example
#define SIZE 10 // same as const int SIZE = 10; int int int int u[SIZE]; x[SIZE] = {1,2,3,4,5}; y[SIZE-5] = {1,2,3,4,5}; z[] = {1,2,3};

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Working with arrays


There are two ways of initialising an array : Initialisation during declaration Initialisation through C++ statements (typically requires loop control structure) In the rst method, an array is initialised in the declaration part - as such it will be initialised

once.

The value to initialise an array has

to be provided explicitly in the declaration.

Example
#define SIZE 10 // same as const int SIZE = 10; int int int int u[SIZE]; x[SIZE] = {1,2,3,4,5}; y[SIZE-5] = {1,2,3,4,5}; z[] = {1,2,3};

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Working with arrays


There are two ways of initialising an array : Initialisation during declaration Initialisation through C++ statements (typically requires loop control structure) In the rst method, an array is initialised in the declaration part - as such it will be initialised

once.

The value to initialise an array has

to be provided explicitly in the declaration.

Example
#define SIZE 10 // same as const int SIZE = 10; int int int int u[SIZE]; x[SIZE] = {1,2,3,4,5}; y[SIZE-5] = {1,2,3,4,5}; z[] = {1,2,3};

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Description of the example :


u x
is an array of integer of size 10. It is uninitialised hence by

default (for integer type only) it contains zero values also an array of integer (size 10), but the rst ve elements

have been initialised with value 1, 2, 3, 4, and 5 respectively. The remaining ve elements has the default value i.e. zero.

is same as

x,

except it size is 5 (SIZE

- 5),

and all of its

elements have been initialised with value 1, 2, 3, 4, and 5. This case illustrate the use of an expression to determine the array size.

also an array of integer, but it size is undened. In such case

it size is determined by the number of values initialised to it. In this example, it has a size of 3 because of the three values initialised to its element.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Description of the example :


u x
is an array of integer of size 10. It is uninitialised hence by

default (for integer type only) it contains zero values also an array of integer (size 10), but the rst ve elements

have been initialised with value 1, 2, 3, 4, and 5 respectively. The remaining ve elements has the default value i.e. zero.

is same as

x,

except it size is 5 (SIZE

- 5),

and all of its

elements have been initialised with value 1, 2, 3, 4, and 5. This case illustrate the use of an expression to determine the array size.

also an array of integer, but it size is undened. In such case

it size is determined by the number of values initialised to it. In this example, it has a size of 3 because of the three values initialised to its element.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Description of the example :


u x
is an array of integer of size 10. It is uninitialised hence by

default (for integer type only) it contains zero values also an array of integer (size 10), but the rst ve elements

have been initialised with value 1, 2, 3, 4, and 5 respectively. The remaining ve elements has the default value i.e. zero.

is same as

x,

except it size is 5 (SIZE

- 5),

and all of its

elements have been initialised with value 1, 2, 3, 4, and 5. This case illustrate the use of an expression to determine the array size.

also an array of integer, but it size is undened. In such case

it size is determined by the number of values initialised to it. In this example, it has a size of 3 because of the three values initialised to its element.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Description of the example :


u x
is an array of integer of size 10. It is uninitialised hence by

default (for integer type only) it contains zero values also an array of integer (size 10), but the rst ve elements

have been initialised with value 1, 2, 3, 4, and 5 respectively. The remaining ve elements has the default value i.e. zero.

is same as

x,

except it size is 5 (SIZE

- 5),

and all of its

elements have been initialised with value 1, 2, 3, 4, and 5. This case illustrate the use of an expression to determine the array size.

also an array of integer, but it size is undened. In such case

it size is determined by the number of values initialised to it. In this example, it has a size of 3 because of the three values initialised to its element.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Alternatively, an array is initialised in a C++ program either explicitly or implicitly. This approach has advantage compared to the previous method, because array can be initialised many times as required (not once). Example : Explicit (left) & Implicit (right) element assignment

#define SIZE 10 int x[SIZE]; x[0]=1; x[1]=2; x[2]=3; x[4]=4; x[5]=5;

| | | | | |

#define SIZE 10 int x[SIZE], i; for(i=0; i < SIZE; i++) x[i] = i + 1;

Explicit  tedious and inecient for large array; unavoidable in cases if the value to be assigned has not particular pattern to derive implicitly Implicit  more concise and perhaps ecient (using loop control structure) provided there is certain pattern e.g. increment by 1.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Alternatively, an array is initialised in a C++ program either explicitly or implicitly. This approach has advantage compared to the previous method, because array can be initialised many times as required (not once). Example : Explicit (left) & Implicit (right) element assignment

#define SIZE 10 int x[SIZE]; x[0]=1; x[1]=2; x[2]=3; x[4]=4; x[5]=5;

| | | | | |

#define SIZE 10 int x[SIZE], i; for(i=0; i < SIZE; i++) x[i] = i + 1;

Explicit  tedious and inecient for large array; unavoidable in cases if the value to be assigned has not particular pattern to derive implicitly Implicit  more concise and perhaps ecient (using loop control structure) provided there is certain pattern e.g. increment by 1.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Alternatively, an array is initialised in a C++ program either explicitly or implicitly. This approach has advantage compared to the previous method, because array can be initialised many times as required (not once). Example : Explicit (left) & Implicit (right) element assignment

#define SIZE 10 int x[SIZE]; x[0]=1; x[1]=2; x[2]=3; x[4]=4; x[5]=5;

| | | | | |

#define SIZE 10 int x[SIZE], i; for(i=0; i < SIZE; i++) x[i] = i + 1;

Explicit  tedious and inecient for large array; unavoidable in cases if the value to be assigned has not particular pattern to derive implicitly Implicit  more concise and perhaps ecient (using loop control structure) provided there is certain pattern e.g. increment by 1.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro Array Array Initialisation Working with array

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Accessing Array Element


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

Using index or subscript notation, Using oset or base address notation


Example

int x[10], total, i, j; total i = 3; total x[j+4] = j = = x[1] + x[9]; = 4; x[i] + x[j]; 100 * x[i];

In the rst expression, the value of 2th element (x[1]) of array x is retrieved and added to the retrieved value of 10th element (x[9]), than the result is assigned to variable total. In the last expression, 100 is multiplied with the retrieved value of 4th element (because i was initially assigned to 3) of array x. The result of is assigned to the 9th element (because j = 4) of array x.

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

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Accessing Array Element


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

Using index or subscript notation, Using oset or base address notation


Example

int x[10], total, i, j; total i = 3; total x[j+4] = j = = x[1] + x[9]; = 4; x[i] + x[j]; 100 * x[i];

In the rst expression, the value of 2th element (x[1]) of array x is retrieved and added to the retrieved value of 10th element (x[9]), than the result is assigned to variable total. In the last expression, 100 is multiplied with the retrieved value of 4th element (because i was initially assigned to 3) of array x. The result of is assigned to the 9th element (because j = 4) of array x.

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

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Accessing Array Element


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

Using index or subscript notation, Using oset or base address notation


Example

int x[10], total, i, j; total i = 3; total x[j+4] = j = = x[1] + x[9]; = 4; x[i] + x[j]; 100 * x[i];

In the rst expression, the value of 2th element (x[1]) of array x is retrieved and added to the retrieved value of 10th element (x[9]), than the result is assigned to variable total. In the last expression, 100 is multiplied with the retrieved value of 4th element (because i was initially assigned to 3) of array x. The result of is assigned to the 9th element (because j = 4) of array x.

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

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Accessing Array Element


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

Using index or subscript notation, Using oset or base address notation


Example

int x[10], total, i, j; total i = 3; total x[j+4] = j = = x[1] + x[9]; = 4; x[i] + x[j]; 100 * x[i];

In the rst expression, the value of 2th element (x[1]) of array x is retrieved and added to the retrieved value of 10th element (x[9]), than the result is assigned to variable total. In the last expression, 100 is multiplied with the retrieved value of 4th element (because i was initially assigned to 3) of array x. The result of is assigned to the 9th element (because j = 4) of array x.

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

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Accessing Array Element


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

Using index or subscript notation, Using oset or base address notation


Example

int x[10], total, i, j; total i = 3; total x[j+4] = j = = x[1] + x[9]; = 4; x[i] + x[j]; 100 * x[i];

In the rst expression, the value of 2th element (x[1]) of array x is retrieved and added to the retrieved value of 10th element (x[9]), than the result is assigned to variable total. In the last expression, 100 is multiplied with the retrieved value of 4th element (because i was initially assigned to 3) of array x. The result of is assigned to the 9th element (because j = 4) of array x.

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

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro Array Array Initialisation Working with array

Using oset notation


Access to array's element can be done relative to oset or base address of an array, i.e. array's name. This approach is best explained if we draw a hypothetical memory map diagram of the program (see below)

Example

total = (x + 1) + (x + 2)
= 20 + 30 50 = (1202) + (1204)

------------------------int x[10], total, i, j; total = *(x+1) + *(x+2); i = 3; j = 4; total = *(x+i) + *(x+j); -------------------------

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

var i j total x[0] x[1] x[2] x[3] x[4]

... ...

total = (x + 3) + (x + 4)
= 40 + 50 90. = (1206) + (1208)

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

B Belaton

Arrays

... ...

value 3 4 ?? ?? ?? 10 20 30 40 50 ?? ??

addr 1000 1002 1004 ... ... 1200 1202 1204 1206 1208 ... ...

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro to 2D Array Initialise 2D Array Passing Array to function

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

Introduction to 2D Array

Typical example of 2D array is matrix or table which consist of rows (horizontal) and column (vertical). Declaring 2D array is not much dierent than declaring 1D array, except now we've additional dimension.

[]

pair to indicate the extra

Example

#define #define

ROW COLUMN

6 4

int x[ROW][COLUMN];

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

col 0

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

col 1

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

col 2

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

col 3

row 0 row 1 row 2 row 3 row 4 row 5

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro to 2D Array Initialise 2D Array Passing Array to function

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

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

Example 1 : int x[2][3] = {{1,2,3},{4,5,6}}; 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] 1 4 x[1][0] x[0][1] 2 5 x[1][1] x[0][2] 3 6 x[1][2]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

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

Example 1 : int x[2][3] = {{1,2,3},{4,5,6}}; 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}; y is also a 2D integer array, but its row is determined by the number of items initialised to it it has 3 columns.
y[0][0] 1 4 7 y[2][0] y[0][1] 2 5 0 y[2][1] y[0][2] 3 6 0 y[2][2]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

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

Example 1 : int x[2][3] = {{1,2,3},{4,5,6}}; 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}}; z is also a 2D integer array, where only three elements have been explicitly initialised. These are 1st (z[0][0]), 2nd (z[0][1]) and 4th (z[1][0]) elements.
z[0][0] 1 4 z[1][0] z[0][1] 2 0 z[1][1] z[0][2] 0 0 z[1][2]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing 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 row. Note also that the inner

with value 1 to 12 for each column in the

for

loop varies faster i.e. the column

part, then followed by the outer

for

loop i.e. the row part.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro to 2D Array Initialise 2D Array Passing Array to function

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

Passing array to function

We can do this in two dierent 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 by reference) Call by value vs. call by reference (for array) Call be reference is preferred because we only need to pass single value i.e. the base address. Only need to pass the array's name and size of the array to the function

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

Passing array to function

We can do this in two dierent 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 by reference) Call by value vs. call by reference (for array) Call be reference is preferred because we only need to pass single value i.e. the base address. Only need to pass the array's name and size of the array to the function

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

Passing array to function

We can do this in two dierent 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 by reference) Call by value vs. call by reference (for array) Call be reference is preferred because we only need to pass single value i.e. the base address. Only need to pass the array's name and size of the array to the function

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing 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 of an array. Thus if all 100 elements to be processed, then the function header of function modifyArray() need to include 100 formal parameters !!

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to 2D Array Initialise 2D Array Passing Array to function

Call by reference  passing array name


Example below shows an alternative and better approach to passing array to function :

Array name (base address/oset) 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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro to char array Initialising Char Array

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to char array Initialising Char Array

Character vs Strings

Character arrays are array of values of character type The

char

char

type denotes an individual character constant

(delimited by single quotes) e.g. Note that

`y'

is dierent from

char input = `y'; "y" (a string containing

single character). Each character is encoded as an integer value  ASCII encoding scheme, e.g. character

121

`y'

is encoded as number

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to char array Initialising Char Array

Declaring a Character Array


The syntax of character array declaration is as follow :

char char

variable_name variable_name

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

Below is an example of character array declaration (and initialisation) :

char greeting[] = "Hello";

greeting =

H e l l o \0

[0] [1] [2] [3] [4] [5]

zero terminator

Note : In the above example

zero terminator

character is

automatically inserted.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to char array Initialising Char Array

Declaring a Character Array


The syntax of character array declaration is as follow :

char char

variable_name variable_name

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

Below is an example of character array declaration (and initialisation) :

char greeting[] = "Hello";

greeting =

H e l l o \0

[0] [1] [2] [3] [4] [5]

zero terminator

Note : In the above example

zero terminator

character is

automatically inserted.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to char array Initialising Char Array

Declaring a Character Array


The syntax of character array declaration is as follow :

char char

variable_name variable_name

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

Below is an example of character array declaration (and initialisation) :

char greeting[] = "Hello";

greeting =

H e l l o \0

[0] [1] [2] [3] [4] [5]

zero terminator

Note : In the above example

zero terminator

character is

automatically inserted.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Intro to char array Initialising Char Array

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to char array Initialising 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 (in standard library) as marker to indicate end of string e.g.

strlen, strcpy, strcat, etc. Failing to include `\0' may result


B Belaton

in program crashing

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Intro to char array Initialising Char Array

Character Array vs. String


Character array is a legacy inherited from C, hence it is still in used in C++ but when there is option always used class. Why? To convert a

string
member

string

into a character array use

c_str

function of the string class. For instance, in

cstdlib header, there is a function int atoi(const char s[]);

that converts a character array containing digits into its integer value.

Example showing how to use c_str() member function


char year[] = "1999"; | int y = atoi(year); // y is integer 1999 | string year = "1999"; int y = atoi(year.c_str());

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Array's Operation Search & Sort

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Processing array
Array is useful data structure (known as linear data structure), because it concisely store and group data of similar type, as well as providing ecient method for accessing data stored in it for further processing. What kind of operations that we can do to array data structure :

Sorting the contents of array in user dened order such as ascending or descending, Searching the contents of array for a specic value Concatenating or merging the contents of two arrays (esp. for character arrays). Adding or Deleting array elements, etc.
sorting
and

Here we will concentrate on the two most common manipulations to array data structure i.e.

searching.

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline

Array's Operation Search & Sort

Array Data Type Introduction to array Initialising arrays Accessing Array Element

Two-Dimensional Array Introduction to 2D array Initialising 2D array Passing Array to function

Array of Characters Introduction to character array Initialising character array

Processing Array (if

time permitted)

What kind of array processing? Searching & Sorting

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Sorting Technique  Bubble Sort (Concept)


Sorting is a process of rearranging the content of array so that given an arbitrary size of unsorted array, the result will be the same array with its contents sorted in a particular order i.e. ascending or descending. There are many sorting techniques, some works well in specic applications while others are designed to tackle general sorting problem. Below is pseudocode (simplify version) of Bubble Sort method for sorting elements of array in ascending order :
1: 2: 3: 4: 5:

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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 6 4 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 6 4 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 6 4 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 89 68 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 89 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 89 45 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 89 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 89 37

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 68 45 37 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 68 45 37 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 68 37 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 68 37 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 37 68 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 37 68 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 37 68 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 37 68 89 P3 2 6 4 8 10 12 45 37 68 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 37 68 89 P3 2 6 4 8 10 12 45 37 68 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Bubble sort example


Ori 2 6 4 8 10 12 89 68 45 37 P1 2 4 6 8 10 12 68 45 37 89 P2 2 4 6 8 10 12 45 37 68 89 P3 2 6 4 8 10 12 37 45 68 89

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

B Belaton

Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Searching Techniques (Concept)


Searching is a process that inspect the content of an arbitrary sized array either to nd a match with the search value (equality test) or to nd matches of values that satised certain predened condition (greater than, less than, etc). Similar to sorting, there also loads of searching techniques, here we will focus on the two simple searching methods known as linear search and binary search. The simplest (and hence inecient) method to nd if a particular value (key value) matches one (or more) elements of an array is by searching and comparing from the start of the array, only stop if a match is found or if the end of array is reached. This method known as linear search is suitable for small unsorted array. In the next slide an example of linear search function which return the index to array x if match is found or -1 if no match is found.
B Belaton Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & 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 ecient) searching technique is called Binary search. This method is fast because it is relying on the fact that the target array we wanted to do the searching must be already sorted. Hence, it simplies the search mechanism by sub-dividing the search space into half (binary) each time the search operation is performed. Binary search can also be implemented using recursive function concept.
B Belaton Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & 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 of search space, and high is the upper boundary of search space. At each iteration of the while loop low or high updated accordingly based on the test condition in the if statement. If match is found, the index to array b is returned, otherwise -1 i returned to the calling function.
B Belaton Arrays

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Binary Searching Technique  Illustration


Search key value = 8
Ori x[0] 2 low x[1] 6 1st x[2] 4 x[3] 8 x[4] 10 mid x[5] 12 x[6] 37 2nd x[7] 45 x[8] 68 x[9] 89 high P1 x[0] 2 low 1st x[1] 6 mid x[2] 4 2nd 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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Binary Searching Technique  Illustration


Search key value = 8
Ori x[0] 2 low x[1] 6 1st x[2] 4 x[3] 8 x[4] 10 mid x[5] 12 x[6] 37 2nd x[7] 45 x[8] 68 x[9] 89 high P1 x[0] 2 low 1st x[1] 6 mid x[2] 4 2nd 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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Binary Searching Technique  Illustration


Search key value = 8
Ori x[0] 2 low x[1] 6 1st x[2] 4 x[3] 8 x[4] 10 mid x[5] 12 x[6] 37 2nd x[7] 45 x[8] 68 x[9] 89 high P1 x[0] 2 low 1st x[1] 6 mid x[2] 4 2nd 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

Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)

Array's Operation Search & Sort

Binary Searching Technique  Illustration


Search key value = 8
Ori x[0] 2 low x[1] 6 1st x[2] 4 x[3] 8 x[4] 10 mid x[5] 12 x[6] 37 2nd x[7] 45 x[8] 68 x[9] 89 high P1 x[0] 2 low 1st x[1] 6 mid x[2] 4 2nd 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

You might also like