Beruflich Dokumente
Kultur Dokumente
Arrays
CPT111 Principles of Programming
Bahari Belaton
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
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Introduction to Array
An
array
subscript
values :
v[i]; age
index
or
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)
type_name type_name
variable_name variable_name
type_name can be any built-in types such as int, float, char, double, or user-dened type such as class
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)
type_name type_name
variable_name variable_name
type_name can be any built-in types such as int, float, char, double, or user-dened type such as class
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)
type_name type_name
variable_name variable_name
type_name can be any built-in types such as int, float, char, double, or user-dened type such as class
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)
type_name type_name
variable_name variable_name
type_name can be any built-in types such as int, float, char, double, or user-dened type such as class
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)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
once.
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)
once.
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)
once.
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)
once.
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)
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,
- 5),
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.
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)
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,
- 5),
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.
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)
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,
- 5),
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.
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)
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,
- 5),
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.
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)
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
| | | | | |
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)
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
| | | | | |
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)
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
| | | | | |
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
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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)
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)
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)
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)
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)
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)
... ...
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
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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.
[]
Example
#define #define
ROW COLUMN
6 4
int x[ROW][COLUMN];
col 0
col 1
col 2
col 3
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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)
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)
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)
for
loops. Why?
for
for
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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)
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)
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)
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)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Character vs Strings
char
char
`y'
is dierent from
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)
char char
variable_name variable_name
greeting =
H e l l o \0
zero terminator
zero terminator
character is
automatically inserted.
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
char char
variable_name variable_name
greeting =
H e l l o \0
zero terminator
zero terminator
character is
automatically inserted.
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
char char
variable_name variable_name
greeting =
H e l l o \0
zero terminator
zero terminator
character is
automatically inserted.
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
`\0'
in program crashing
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
string
member
string
c_str
that converts a character array containing digits into its integer value.
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
Outline
Array Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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 Data Type Introduction to array Initialising arrays Accessing Array Element
time permitted)
B Belaton
Arrays
Array Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 Data Type Two-Dimensional Array Array of Characters Processing Array (if time permitted)
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)
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)
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)
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)
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)
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