Download presentation
Presentation is loading. Please wait.
1
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results in a difficult to maintain and error-prone program. Instead use arrays Array = a named location in memory for many values of the same type, stored sequentially and accessed through an index Compare: variable = a named location in memory for one value.
2
2 Arrays Think of an array a sequence of boxes, each one holding a value Example: An array named scores that holds the quiz scores of 5 students: element 0element 1element 2element 3element 4 11 10 12 9 11 scores
3
3 Arrays Array declaration syntax element_type array_name [ size ] ; element_type is the type of the elements stored in the array. array_name is the name of the array. Naming rules are the same as with variables. size must be a constant (known at compile time) no variables allowed you cannot change the array size at runtime use a #define directive to define the size.
4
4 Arrays Declaring and initializing an array Example 1: int scores[5]; the elements of scores have not been initialized. They may be initialized one at a time (usually in a for loop: for (int i = 0; i < 5; i++) { cout << “Enter score: “; cin >> scores[i]; } Example 2: int scores[5] = {11, 10, 12, 9, 11}; the elements are initialized at declaration time. When this is done, the size of the array may be omitted from the declaration.
5
5 Arrays Accessing the elements of an array. To access the element at index i specify the name of the array and the index enclosed in brackets. Example: scores[3] = 11; CAUTION! Indexing always starts at zero and ends at size-1 int scores[5]; scores[0] = 11; scores[1] = 12; scores[2] = 10; scores[3] = 9; scores[4] = 11; The compiler will NOT catch a wrong index. Typing scores[10] or scores[-1] can corrupt your data.
6
6 Arrays How exactly do they work? int scores[5]; says: I need a chunk of memory big enough to hold 5 integer variables. That’s a total of 5*4 = 20 bytes. scores itself represents the memory address where this chunk of memory begins. Think of it as a special kind of constant that holds the address of a location in memory. Example: int main () { int arr[2]; cout << arr << endl; return 0; } The output of this program is: 0xbfffb240 (the address of arr in hexadecimal)
7
7 Arrays How exactly do they work? scores[i] represents the contents in box #i starting at address scores. In other words, it represents the contents at address scores + i * sizeof(int)
8
8 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a function as one of its arguments. int main () { int nums[3] = {3, 6, 5}; int sq; sq = square( nums[2] ); cout << nums[2] << “squared is “ << sq << endl; return 0; } int square( int x ) { return x * x; } copy (pass by value) Output : 5 squared is 25
9
9 Arrays & functions To pass the entire array as an argument: In the function prototype specify that the argument is an array by placing brackets after the name. Do NOT put the size of the array inside the brackets. Example: void printArray ( int arr[ ] ) ; Call the function using ONLY the array name as an argument. Example: int nums[5] = {1,2,3,4,5}; printArray( nums );
10
10 Arrays & functions int main () { int scores[5] = {11,12, 10, 9, 11}; printArray( scores, 5 ); return 0; } void printArray(int arr[], int size) { for (int i=0; i<size; i++) cout << arr[i] << “ “; cout << endl; } The parameters are again passed by value. However, the value of scores (which is copied onto arr ) is the address where the array begins. As a result, both arr and scores refer to the same location in memory! This allows a function to modify the elements of an array that is passed as an argument.
11
11 Arrays & functions int main () { int scores[5] = {11,12, 10, 9, 11}; cleanArray( scores, 5); printArray( scores, 5 ); return 0; } void printArray(int arr[], int size) { for (int i=0; i<size; i++) cout << arr[i] << “ “; cout << endl; } void cleanArray(int arr[], int size) { for (int i=0; i<size; i++) arr[i] = 0; } Output : 0 0 0 0 0 This actually modifies the values stored in scores.
12
12 Multidimensional arrays 1D array: 2D array: 3D array: 1 row of 5 elements 3x5 array. Think of it as an array of arrays ( = an array of three elements which are arrays of five elements) 3x3x1 array.
13
13 Multidimensional arrays How are they stored in memory? What are the indices of the elements? 12 34 56 123456 row 1 row 2 row 3 3x2 array 0,00,10,2 1,01,11,21,2 0,3 1,3 2x4 array rowcolumn
14
14 Multidimensional arrays Declaration: similar to 1D, but specify #rows, #columns Example: double stats[10][4]; // a 10x4 array of doubles Initialization: similar to 1D. Use nested for-loops or initialize at declaration. Example 1: Example 2: for (int i = 0; i < 10; i++) for (int j = 0; j < 4; j++) stats[ i ][ j ] = 0.0; int grid[3][2] = { {0, 1}, {1, 1}, {0, 0} }; row 1 row 2 row 3
15
15 Multidimensional arrays CAUTION! When a multidimensional array is an argument to a function, the size of all but the first subscript MUST be specified. Example: void initArray( int arr[][10], int numrows); // prototype... int main() { int nums[5][10]; initArray(nums, 5); // function call... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.