






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture will cover your concepts on the topic of Arrays in C++.
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







C++ provides a data structure, the array , which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows โ type arrayName [ arraySize ]; This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement โ double balance[10];
You can initialize C++ array elements either one by one or using a single statement as follows โ double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array โ If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write โ double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0}; You will create exactly the same array as you did in the previous example. balance[4] = 50.0; The above statement assigns element number 5th^ in the array a value of 50.0. Array with 4th^ index will be 5th, i.e., last element because all arrays have 0 as the index of their
first element which is also called base index. Following is the pictorial representaion of the same array we discussed above โ
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example โ double salary = balance[9]; The above statement will take 10th^ element from the array and assign the value to salary variable. Following is an example, which will use all the above-mentioned three concepts viz. declaration, assignment and accessing arrays โ #include
C++ allows multidimensional arrays. Here is the general form of a multidimensional array declaration โ type name[size1][size2]...[sizeN]; For example, the following declaration creates a three dimensional 5. 10. 4 integer array โ int threedim[5][10][4];
The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list of one-dimensional arrays. To declare a two- dimensional integer array of size x,y, you would write something as follows โ type arrayName [ x ][ y ]; Where type can be any valid C++ data type and arrayName will be a valid C++ identifier. A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array a , which contains three rows and four columns can be shown as below โ Thus, every element in array a is identified by an element name of the form a[ i ][ j ] , where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns. int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 / {4, 5, 6, 7} , / initializers for row indexed by 1 / {8, 9, 10, 11} / initializers for row indexed by 2 */ };
The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example โ int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example โ int val = a[2][3]; The above statement will take 4th^ element from the 3rd^ row of the array. You can verify it in the above digram. #include
return 0 ; } When the above code is compiled and executed, it produces the following result โ Array values using pointer *(p + 0) : 1000 *(p + 1) : 2 *(p + 2) : 3. *(p + 3) : 17 *(p + 4) : 50 Array values using balance as address *(balance + 0) : 1000 *(balance + 1) : 2 *(balance + 2) : 3. *(balance + 3) : 17 *(balance + 4) : 50 In the above example, p is a pointer to double which means it can store address of a variable of double type. Once we have address in p, then *p will give us value available at the address stored in p, as we have shown in the above example.
C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index. If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.
Formal parameters as a pointer as follows โ void myFunction(int *param) { . . . }
Formal parameters as a sized array as follows โ void myFunction(int param[10]) { . . .
Formal parameters as an unsized array as follows โ void myFunction(int param[]) { . . . } Now, consider the following function, which will take an array as an argument along with another argument and based on the passed arguments, it will return average of the numbers passed through the array as follows โ double getAverage(int arr[], int size) { int i, sum = 0 ; double avg; for (i = 0 ; i < size; ++i) { sum += arr[i]; } avg = double(sum) / size; return avg; } Now, let us call the above function as follows โ #include
// a pointer to an int. int p; p = getRandom(); for ( int i = 0 ; i < 10 ; i++ ) { cout << "(p + " << i << ") : "; cout << *(p + i) << endl; } return 0 ; } When the above code is compiled together and executed, it produces result something as follows โ 624723190 1468735695 807113585 976495677 613357504 1377296355 1530315259 1778906708 1820354158 667126415 *(p + 0) : 624723190 *(p + 1) : 1468735695 *(p + 2) : 807113585 *(p + 3) : 976495677 *(p + 4) : 613357504 *(p + 5) : 1377296355 *(p + 6) : 1530315259 *(p + 7) : 1778906708 *(p + 8) : 1820354158 *(p + 9) : 667126415