/**
*Test program for an algorithm that finds the maximum element in an array.
*/
public class ArrayMaxProgram {
/** Finds the maximum element in array A of n integers. */
static 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. */
public static void main(String args[]) {
int[] num = { 10, 15, 3, 5, 56, 107, 22, 16, 85 };
int n = num.length;
System.out.print("Array:");
for (int i=0; i < n; i++)
System.out.print(" " + num[i]); // prints one element of the array
System.out.println(".");
System.out.println("The maximum element is " + arrayMax(num,n) + ".");
}
}