|
|
|
|
|
|
|
Array |
|
Input and output array values |
|
Array as function arguments |
|
Individual array elements as function arguments |
|
May be passed by values |
|
May be passed by reference |
|
Complete array as function arguments |
|
Always passed by reference |
|
|
|
|
|
|
|
|
Individual array elements can be treated the
same way as other data type |
|
|
|
Input Examples |
|
const int NUMBER = 5; |
|
double temp[NUMBER]; |
|
cin
>>temp[0]>>temp[1]>>temp[2]>>temp[3]>>temp
[4]; |
|
|
|
for ( int i = 0 ; i < NUMBER ; i ++ ) |
|
{ |
|
cout << \n Enter a temperature: ; |
|
cin >> temp[I]; |
|
} |
|
|
|
|
|
|
|
Output Examples |
|
cout << temp[0]; // output the value of
the first element |
|
|
|
// the following loop print out all the elements
in an array |
|
for ( int i = 0 ; i < NUMBER ; i ++ ) |
|
{ |
|
cout << \nEnter a temperature:
; |
|
cin >> temp[i]; |
|
} |
|
|
|
|
Individual array elements are passed to a
function in the same manner as individual variables; they are simply
included as subscript variable when the function call is made. |
|
Example 1 |
|
// arguments will be passed into the function by
value |
|
int find_min(int x, int y) { |
|
if ( x < y) return x; |
|
else return y; |
|
} |
|
|
|
|
|
|
int m = 87, n= 65; // declare two
integer variable |
|
cout << find_min(m,n) <<
endl; // 65 |
|
|
|
int grade[5] = {98, 87, 74, 65, 82}; //
declare an integer array |
|
|
|
for (i =0; i<5; i++) |
|
cout << grade[i] << "
"; // 98 87 74
65 82 |
|
cout << endl; |
|
// grade[1] and grade[3] will be passed to
function find_min by value |
|
cout << find_min(grade[1], grade[3])
<< endl; // 65 |
|
|
|
for (i =0; i<5; i++) |
|
cout << grade[i] << "
"; // 98 87 74
65 82 |
|
|
|
|
|
|
|
|
Example 2 |
|
// arguments will be passed into the function by
value |
|
void swap(int& x, int& y) { |
|
int temp; |
|
temp = x; |
|
x = y; |
|
y = temp; |
|
} |
|
|
|
|
int m = 87, n= 65; |
|
swap(m,n); |
|
cout
<< "m= " << m << " n= " << n
<< endl; // m= 65 n= 87 |
|
|
|
int
grade[5] = {98, 87, 74, 65, 82}; |
|
for
(i =0; i<5; i++) |
|
cout <<
grade[i] << " "; // 98 87 74 65
82 |
|
cout << endl; |
|
//grade[1] and grade[3] passed to function
swap by reference |
|
swap(grade[1], grade[3]); |
|
for
(i =0; i<5; i++) |
|
cout << grade[i] << "
"; // 98 65 74 87
82 |
|
cout << endl; |
|
|
|
|
|
|
Passing a complete array of values to a function
is in many respects an easier operation than passing individual elements. |
|
The function receives access to the actual
array, rather than a copy of the values in the array. |
|
In other words, C++ always pass a whole array to
a function by reference. |
|
|
|
|
|
|
Question: write a function that will find the
max value in an array |
|
// function prototype |
|
int find_max(int g[], int size); |
|
|
|
// function definition |
|
int find_max(int g[], int size) { |
|
int max = g[0]; |
|
for(int i = 1; i < size; i++ ) { |
|
if (max < g[i]) max = g[i]; |
|
} |
|
return max; |
|
} |
|
|
|
|
|
|
// function call |
|
int grade[5] = {98, 87, 74, 65, 82}; |
|
|
|
cout << "The max number in the
array is : " |
|
<< find_max(grade, 5) <<
endl; |
|
When you call the function find_max by passing
the complete array and the size of the array, you only need to put the name
of the array. You dont need to put []. |
|
|
|