Download presentation
Presentation is loading. Please wait.
Published byVictor Clarke Modified over 9 years ago
1
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem 1 2013/2014
2
At the end of this lecture, you should be able to Differentiate passing array by value and passing array by reference Pass individual elements from array to a function Pass an entire array to a function 2
3
We can use function to process arrays in large program Pass by value The variable ‘s value is passed to a formal parameter Changing the value of the local parameter inside the function does not effect the value of the variable outside the function Refer to passing individual elements Pass by reference Passing an array means that the starting address of the array is passed to the formal parameter The parameter inside the function references to the same array that is passed to the function NO new arrays are created Refer to passing the whole array 3
4
By passing individual elements Same as passing any ordinary variable to a function By passing the whole array When we need the function to operate on the whole array 4
5
Passing individual element Array element must matches the function parameter type When as a value parameter, function cannot change the value of the element in the calling function E.g : a function, print_square receives an integer and prints its square, using an array, we can loop through the array and pass each element in turn to print_square 5 1-D array : Passing individual elements
6
6 Passing individual element - Example 1
7
7 #include using namespace std; void print(int a); // function prototype void main() { int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1}}; for(i = 0; i < 4; i++) { for(j = 0; j < 3; j++) print( ary1[i][j] ); cout << endl; } // end for } // end main void print(int a) { cout << a << “\t“; } // end print Passing an individual element - Example 2
8
Passing the whole array When we use large arrays in functions, by passing each value we need an extra memory to do so E.g : if an array containing 20k elements were passed by value to a function, another 20k elements would have to be allocated in the function and each element would have to be copied from one array to another Instead of passing the whole array, C++ passes the address of the array 8 1-D array : Passing the whole array
9
An array name is the address of the first element in the array Because of the name of array is in fact its address, passing an array name allows the called function to refer to the array back in the calling function Two rules associated with passing the whole array The function must be called by passing only the name of the array In the function definition, the formal must be an array type, the size of the array doesn’t need to be specified. If provided, it is ignored by the compiler. A function can change the elements in array by passing the array name without the constant modifier 9 1-D array : Passing the whole array
10
Passing the whole arrays for updating - Example 1 10 Arrays are passed by reference only The symbol & is not used when declaring an array as a formal parameter C++ does not allow functions to return a value of the type array
11
#include using namespace std; #define SIZE 7 void reverseOrder(int [], int); // function prototype void main() { int numbers[SIZE]={1,2,3,4,5,6,7}; reverseOrder(numbers, SIZE); } // end main void reverseOrder(int list[], int arraySize) { for (int i = arraySize-1; i >= 0; i--) cout << list[i] << "\t"; } // end reverseOrder 11
12
Passing array by reference could lead to errors if your function changed accidentally Put const keyword before the array parameter to tell the compiler that the array can’t be changed 12
13
13 Passing the whole array as constants : Example
14
Passing an individual element - Pass the individual element by indexing the array name with the row number and the column number Passing a row Pass the whole row by indexing the array name with only the row number Passing the whole array Use the array name as the actual parameter 14 Passing the two-dimensional array to function
15
#include using namespace std; void print(int a); void main() { int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1}; for(int i=0; i<4; i++){ for(int j=0; j<3; j++) print(ary1[i][j]); cout << endl; } void print(int a) { cout << a << “\t“; } 2D Array: Passing an individual element - Example 15
16
2D Array : Passing a row - Example 16
17
2D Array : Passing the whole array – Example 1 17
18
#define NUMBER_OF_STUDENTS 5 // const int NUMBER_OF_STUDENTS=5; #define NUMBER_OF_QUESTIONS 10 // const int NUMBER_OF_QUESTIONS=10; void checkAnswer(char[], char[][NUMBER_OF_QUESTIONS], int, int, int[]); // function prototype void main() { char key[NUMBER_OF_QUESTIONS] = {'C','C','B','A','D','B','A','B','C','D' }; char answer[NUMBER_OF_STUDENTS][NUMBER_OF_QUESTIONS]= { {'C','D','C','C','B','B','A','B','C','B'}, {'C','C','B','C','D','B','A','B','A','B'}, {'D','D','C','A','D','B','A','B','A','D'}, {'B','C','C','C','B','B','B','B','C','C'}, {'C','C','C','A','D','B','C','B','C','D'} }; int checked[NUMBER_OF_STUDENTS]; checkAnswer(key, answer, NUMBER_OF_STUDENTS, NUMBER_OF_QUESTIONS, checked); // passing array numbers } // end main 18 2D Array : Passing the whole array - Example 2
19
void checkAnswer(char key[], char answer[][NUMBER_OF_QUESTIONS], int numStud, int numQues, int checked[]) { int i, j; // declaring index for (i = 0; i < numStud; i++) // initializing checked (zeros) checked [i] = 0; for (i = 0; i < numStud; i++) { for (j = 0; j < numQues; j++) { if (answer[i][j] == key[j]) checked[i] += 1; } // inner for cout << "Student " << i << "'s marks : " << checked[i] << "/" << NUMBER_OF_QUESTIONS << "." << endl; } // end for } // end checkAnswer 19 cont. eg. 2D Array : Passing the whole array
20
To pass an array to a function, just use the array name: showScores(tests); To define a function that takes an array parameter, use empty [] for array argument: void showScores(int []); // function prototype void showScores(int tests[]) // function header
21
When passing an array to a function, it is common to pass array size so that function knows how many elements to process: showScores(tests, ARRAY_SIZE); Array size must also be reflected in prototype, header: void showScores(int [], int); // function prototype void showScores(int tests[], int size) // function header 7-21
22
(Program Continues)
23
Program 7-14 (Continued)
24
Can you describe the difference between passing array by value and reference? Do you know how to pass single values from an array to a function and pass a whole array to a function? Do you know the purpose of using constant in argument list of a function when passing an array? 24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.