/** Program for finding the maximum element in an array A of n integers. */
public class ArrayMaxProgram {
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.println("The maximum element is " + arrayMax(num,n) + ".");
}
}