|
Arrays as Arguments in a Function Call
% cat array_max.cpp
// Using arrays as arguments in a function call
// Determine the maximum element in an array
#include<iostream>
using namespace std;
int maxi( const int results[], int NUM_ITEMS ); // declaration
int main()
{
const int NUM_ITEMS = 5;
int max, score[NUM_ITEMS] = {22, 77, 44, 11, 33 };
max = maxi( score, NUM_ITEMS ); // use the array
name in the call
cout << "The maximum number is " << max
<<
endl;
return 0;
}
int maxi( const int results[], int NUM_ITEMS )
{
int i, max = results[0];
for( i = 1; i < NUM_ITEMS; i++)
{
if( max < results[i] )
max = results[i];
}
return max;
}
% g++ array_max.cpp
% a.out
The maximum number is 77
|