Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 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.

Similar presentations


Presentation on theme: "1 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."— Presentation transcript:

1 1 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

2 2 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 );

3 3 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.

4 4 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.

5 5 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) 3x3x2 array.

6 6 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

7 7 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

8 8 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... }


Download ppt "1 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."

Similar presentations


Ads by Google