Anda di halaman 1dari 15

Computer Programming and Applications

Arrays

Dr. Simon P.H. Lui Electrical & Electronic Engineering


Array.doc

ENGG1002 / Arrays

Arrays
Assume that you have three variables and want to compute the sum of them: #include <iostream> // array_p1.cpp using namespace std; void main() { int i, total=0; int data0, data1, data2; data0 = 10; data1 = 12; data2 = 14; total += data0; total += data1; total += data2; cout << "Total : " << total << endl; } Obviously, it's highly inefficient if the number of variables is 1000, say.

ENGG1002 / Arrays

The problem can be solved by using arrays. An array is a set of consecutive memory locations that have the same name and type Each item in an array is called an element The number of elements in an array is called the dimension of the array Like any other variables, you have to declare an array before using it : // an array of int int data_list[3];

This declares data_list to be an array of 3 elements : data_list[0], data_list[1] and data_list[2] To reference an element of an array, you use a number called the index (an integer inside [ ] ) e.g. index of data_list[1] is 1 The element is also called an indexed variable C++ starts counting from 0, not 1. The three elements are numbered 0-2. data_list[3] is invalid.

ENGG1002 / Arrays

Example : #include <iostream> // array_p2.cpp using namespace std; void main() { int i, total=0; total += data0; int data_list[3]; total += data1; data_list[0] = 10; total += data2; compare data_list[1] = 12; with data_list[2] = 14; for (i=0; i<=2; i++) { total += data_list[i]; } cout << "Total : " << total << endl; }

output of program : Total : 36

ENGG1002 / Arrays

Initializing Arrays
C++ allows arrays to be initialized in the declaration statement : int x[3] = { 12, 24, 669 }; which can also be done by execution-time statements: int x[3]; x[0] = 12; x[1] = 24; x[2] = 669; If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. int x[3] = { 12 }; // x[1], x[2] are 0 If no dimension is given, C++ will determine the dimension from the number of elements in the initialization list : int x[] = { 40, 60, 80 }; cout << sizeof(x) << endl; // sizeof return no. of bytes output of program : 12 (not 3 ! why ?)

ENGG1002 / Arrays

Example:
#include <iostream> // array_p3.cpp using namespace std; void main() { int i, min=99999, max=-99999; int data_list[] = {14, 7, 28, -4, 22}; int no_elements = sizeof(data_list)/sizeof(int); for (i=0; i<no_elements; i++) { if (data_list[i] < min) min = data_list[i]; if (data_list[i] > max) max = data_list[i]; } cout << "Min: " << min << ", max: " << max << endl; }

Output:
Min: -4, max: 28

ENGG1002 / Arrays

Array index out of range


Array index out of range could be a very serious problem. E.g. #include <iostream> // array_p4.cpp using namespace std; int main() { int i = 100; char a[] = {'a', 'b'}; cout << "i: " << i << endl; // no problem a[2] = 'c'; // no error reported cout << "i: " << i << endl; // wrong output } Output:
i: 100 i: 99

wrong!

ENGG1002 / Arrays

The statement a[2] = 'c' overwrites the content of the variable i accidentally a i ... 'a' 'b' 'c'

size of a = 2 overwrite the last byte of i; hence change the value of i

size of i = 4

100
...

C++ will not report any error during compilation or when the program is executed The bug can be extremely difficult to discover It is the programmer's responsibility to ensure all indexes are within its bound, i.e. from 0 to array size - 1

ENGG1002 / Arrays

Passing array to function


An indexed variable is just the same as an ordinary variable, so you can pass an indexed variable to a function by value or by reference. E.g. #include <iostream> // array_p5.cpp using namespace std; void func1(int var_byvalue, int &var_byref, int indexed_byvalue, int &indexed_byref) { var_byvalue = 66; var_byref = 77; indexed_byvalue = 88; indexed_byref = 99; } int main() { int a = 100, b = 200; int arr[] = {300, 400}; cout << "a: " << a << endl << "b: " << b << endl;

ENGG1002 / Arrays

10

cout << "arr[0]: " << arr[0] << endl; cout << "arr[1]: " << arr[1] << endl; func1(a, b, arr[0], arr[1]); cout << "After calling func1\n"; cout << "a: " << a << endl << "b: " << b << endl; cout << "arr[0]: " << arr[0] << endl; cout << "arr[1]: " << arr[1] << endl; } Output:
a: 100 b: 200 arr[0]: 300 arr[1]: 400 After calling func1 a: 100 changed b: 77 arr[0]: 300 arr[1]: 99

ENGG1002 / Arrays

11

But if you pass the entire array to a function, the parameter is neither pass-by-value nor pass-by-reference; they are called array parameters When the element of an array parameter is changed inside the function, the change is effected in the original array. Hence array parameter is like pass-by-reference. E.g. #include <iostream> // array_p5.cpp #include <iostream> // array_p6.cpp using namespace std; void func2(int var_byvalue, int &var_byref, int arr_as_param[]) { var_byvalue = 66; var_byref = 77; arr_as_param[0] = 88; // change array elements arr_as_param[1] = 99; } int main() { int a = 100, b = 200;

ENGG1002 / Arrays

12

int arr[] = {300, 400}; cout << "a: " << a << endl << "b: " << b << endl; cout << "arr[0]: " << arr[0] << endl; cout << "arr[1]: " << arr[1] << endl; func2(a, b, arr); cout << "After calling func2\n"; cout << "a: " << a << endl << "b: " << b << endl; cout << "arr[0]: " << arr[0] << endl; cout << "arr[1]: " << arr[1] << endl; Output:
a: 100 b: 200 arr[0]: 300 arr[1]: 400 After calling func1 a: 100 changed b: 77 arr[0]: 88 arr[1]: 99

ENGG1002 / Arrays

13

Multidimensional Arrays
Arrays can have more than one dimension. e.g. int matrix[2][4]; Total number of elements of above = 2 x 4 = 8 To access an element, use notation : matrix[0][2] = 43; // NOT matrix[0,2] int matrix[2][4] can be thought as a declaration of an array of dimension 2, whose elements are arrays of dimension 4. It can be initialized as follows : int matrix[2][4] = { {1, 2, 3, 4}, {10, 20, 30, 40} }; cout << matrix[1][2]; // output 30
matrix [0][0] [0][1] [0][2] [0][3]

1
[1][0]

2
[1][1]

3
[1][2]

4
[1][3]

10

20

30

40

ENGG1002 / Arrays

14

you can also initialize matrix[2][4] by : int matrix[2][4] = { 1, 2, 3, 4, 10, 20, 30, 40 }; // no { } inside cout << matrix[1][2]; // output 30 C++ allows you to use as many dimensions as needed. e.g. int four_dim[10][12][4][8];

ENGG1002 / Arrays

15

Further Information: Section 7.1, 7.2, 7.4, "Problem Solving with C++"

Anda mungkin juga menyukai