Code Fragment: ArrayMax



// Finds the maximum element in array A of n integers
int arrayMax(int A[], int n) {
  int currentMax = A[0];		// executed once
  for (int i=1; i < n; i++)		// executed once; n times; n-1 times, resp.
    if (currentMax < A[i])		// executed n-1 times
	currentMax = A[i];		// executed at most n-1 times
  return currentMax;			// executed once
}

// Testing method called when the program is executed
int main() {
  const int n = 9;
  int num[n] = { 10, 15, 3, 5, 56, 107, 22, 16, 85 };
  cout << "Array:";
  for (int i=0; i < n; i++) 
    cout << " " << num[i];		// prints one element of the array
  cout << ".\n";
  cout << "The maximum element is " << arrayMax(num,n) << ".\n";
  return EXIT_SUCCESS;
}