Arrays Arrays deal with storage of data, which can be processed later. Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name. Arrays Singe dimensional array Multi dimensional array (two dimensional array) Single Dimensional Arrays int Data [8] [0] [1] [2] [3] [4] [5] [6] [7] Array of 8 integer elements. The above declaration allocates 32 bytes of memory to the array Data. Accessing the array elements The following notation is used to access the array elements name of the array[associated index] Data[0],Data[1]., Data[7] are variables which have the same name Data, but are distinguished by the index associated with each element. In C++ array indexing starts from 0 by default. index(subscript) = (position of the variable in the array ) 1;
index of the last element = (length of the array ) 1; thus the array index varies from 0.(length of array) 1. Referencing array elements a = 4, b = 3; c[a+b] = c[5] + 3; adds 3 to the element at the 6 th position in the array and assigns it to the element stored at position 6. Index of an array cannot be a floating point number. for e.g. c[2.445] is invalid array reference. Index of an array cannot be negative for e.g. c[-4] is invalid array reference. Declaring an array Syntax type name of the array[length of the array] e.g. int c[12]; // declares an array of integers similarly char c[12]; float c[12]; int i = 23, int h[i] is valid array declaration. Initializing an Array I) int c[12]; for(int i = 0;i<12;i++) c[i] = i+1; assigns c[0] = 1,c[1] = 2,..c[11] = 12 II) int c[12] = 1, 2, 3, 4, 5, 6; assigns c[0] = 1,c[1] = 2,..c[5] = 6 also int c[ ]= 1, 2, 3, 4, 5, 6; this declaration has the same effect as the previous declaration, but in this
case the C++ compiler determines the size of the array. To assign one array to another, the assignment should take effect on element by element basis. for(int i = 0 ; i<=11; ++i) c[i] = a[i]; To assign an array element to a variable, the following statement is used a = c[2]; Operations on array elements x = c[6] /2.0; //divides the element at position 7 with 2 and assigns it to x; cout<<c[0] + c[4] + c[7] <<endl; refer to example[26] of the in class example section on the class website. Multidimensional Arrays (Two Dimensional Arrays) Two dimensional array can be visualized as a matrix where each element has a unique row-index and column- index associated with it. int Data[3][3]; row number 0 1 2 Array indices Colnumbers 0 [0][0] [0][1] [0][2] 1 [1][0] [1][1] [1][2] 2 [2][0] [2][1] [2][2] Declaring a two dimensional array int Data[3][3]; A matrix of dimension (3X3)
Initializing a two-dimensional array I) int Data[3][3] = 1,2,34,5,67,8,9); Data 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 II) for (int i = 0; i<=2;i++) for(int j = 0; j<=2; j++) a[i][j] = i*3 + j +1 Data 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 i = row index j = column index Refer to the inclass example[28]. Applications of Multidimensional arrays 1) Matrix manipulations ( refer to inclass example[31] ) a) inverse of a matrix. b) transpose of a matrix. 2) Solutions of a set of linear equations using Gauss methods. 3) Pattern Generation. Passing Arrays as a Parameter to functions In C++ arrays cannot be passed by value to the function i.e a complete chunk of memory cannot be passed to the function by value,but it is allowed to pass the array by reference to the function. Thus the address of the array needs to be passed to the function. The address of the array is the name of the array and it points to the first location of the array.
Data [0] [1] [2] [3] [4] [5] [6] [7] The address of the array Data is the name Data. Example: #include <iostream.h> void printarray (int arg[], int length) for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "\n"; int main () int firstarray[] = 5, 10, 15; int secondarray[] = 2, 4, 6, 8, 10; printarray (firstarray,3); printarray (secondarray,5); return 0; Output 5 10 15 2 4 6 8 10 It is a good practice to pass the length of the array to the function, otherwise a logic to determine the length of the array has to incorporated in the program. It is also possible to pass multidimensional arrays to functions
#include<iostream.h> void printarray(int [][3]); int main() int a[2][3] = 1,2,34,5,6; cout<< values in the array by row are : <<endl; printarray(a); return 0; 2-D Array passed by reference to the void function printarray void printarray(int b[][3]) for(int i = 0;i<2;++i) for(int j = 0;j<3;j++) cout<<b[i][j]<< ; cout<<endl; Output: Values in the array a by row are 1 2 3 4 5 6 References: [1] C++ for Engineers and Scientists, Gary J. Bronson. [2] C++ How To Program, Deitel and Deitel. [3] Object Oriented Programming in Turbo C++, Robert Lafore. [4] www.cplusplus.com