CHAPTER 2 Arrays and Vectors
Introduction to Arrays An array is a sequence of variable that can store value of one particular data type. An array is a consecutive group of memory locations that all have the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array.
Declaring Arrays Format: type array_name[size]; Consider this code. int test[85]; The above code creates an array which can hold 85 elements of int type.
Array Elements The maximum number of elements an array can hold depends upon the size of an array. Consider this code below: int age[5]; This array can hold 5 integer elements. Notice that the first element of an array is age[0] not age[1]. This array has 5 elements and notice fifth is age[4] not age[5].
Array Initialization Array can be initialized at the time of declaration. For example float test[5] = {12, 3, 4, -3, 9}; It is not necessary to write the size of an array during array declaration. float test[] = {12, 3, 4, -3, 9};
Example Program Enter 5 numbers: 4 -3 5 2 First number: 4 #include <iostream> using namespace std; int main() { int n[5]; cout<<"Enter 5 numbers: "; for (int i = 0; i < 5; ++i) { cin>>n[i]; } cout<<"First number: "<<n[0]<<endl; cout<<"Last number: "<<n[4]; return 0; Output: Enter 5 numbers: 4 -3 5 2 First number: 4 Last number: 0
Passing Arrays to functions Arrays can be passed to a function as an argument. Consider this example to pass one-dimensional array to a function: #include <iostream> using namespace std; void display(int marks[5]); int main() { int marks[5] = {88, 76, 90, 61, 69}; display(marks); return 0; } void display(int m[5]) { cout<<"Displaying marks: “ <<endl; for (int i = 0; i <5; ++i) cout<<"Student "<<i+1<<": “ <<m[i]<<endl; }
Passing Arrays to functions Output: Displaying marks: Student 1: 88 Student 2: 76 Student 3: 90 Student 4: 61 Student 5: 69 When an array is passed as an argument to a function, only the name of an array is used as argument. display(marks); Also notice the difference while passing array as an argument rather than variable. void display(int m[5]); The C++ programming language handles passing array to a function in this way to save memory and time.
Multidimensional Arrays C++ allows programmer to create array of an array known as multidimensional arrays. Consider this example: int x[3][4]; Here, x is a two dimensional array. This array can hold 12 (3x4) elements.
Multidimensional Arrays You can think this array as table with 3 row and each row has 4 column.
Multidimensional Array Initialization You can initialize a multidimensional array in more than one way. Consider this examples to initialise two dimensional array. int test[2][3] = {2, 4, -5, 9, 0, 9}; Better way to initialise this array with same array elements as above. int test[2][3] = { {2, 4, 5}, {9, 0 0}};
Example Program #include <iostream> using namespace std; int main() { int test[3][2] = { {2, -5}, {4, 0}, {9, 1} }; for(int i = 0; i < 3; ++i) for(int j = 0; j < 2; ++j) cout<< "test["<< i << "][" << ;j << "] = " << test[i][j]<<endl; } return 0; Output: test[0][0] = 2 test[0][1] = -5 test[1][0] = 4 test[1][1] = 0 test[2][0] = 9 test[2][1] = 1