// findMax2.cc  10/12/05 P. Conrad
// Find maximum value in an array with a function

#include <iostream>
using namespace std;

void printArray(int *a, int size);
// a is a pointer to the first element in the array
// equivalent: void printArray(int a[], int size);


int maxOfArray(int *a, int size);
// return maximum value in the array
//   maximum vs. maximal... actual we are returning a
//   maximal value....   maximum implies uniqueness...


int main(void)
{

  int b[5] = {3, 17, 16, 5, -1};
  int a[5];

  // dump the contents of both arrays

  cout << "=====The a array=======\n";
  printArray(a,5);
  cout << "=====The b array=======\n";
  printArray(b,5);

  // find the maximum of array b and print it

  int largest;
  largest = maxOfArray(  b, 5   );
  cout << "The largest value is: "  << largest << endl;
  
  return 0;
    
   
}




void printArray(int *a, int size)
{
  // a is a pointer to the first element in the array
  // equivalent: void printArray(int a[], int size);
  
  for (int i=0; i<size; i++)
    cout << "a[" << i << "] = " << a[i] << endl; 

}


int maxOfArray(int *a, int size)
{
  // return maximum value in the array
  //   maximum vs. maximal... actual we are returning a
  //   maximal value....   maximum implies uniqueness...

  int max = a[0];

  // I know at this point that max is the maximum value
  // of the elements in the array from 0 to 0

  for (int i=1; i< size; i++)   // or use   for (int i=1; i<=4; i++)
    {
      max = ( a[i] >= max ? a[i] : max );


      // I know at this point that max is the maximum value
      // of the elements in the array from 0 up through and including i
    }

  return max;

}

