This problem ensures you know how to implement a loop and search in an array.
int theMax = a[0];
public class FindMax {
/** @param a an array of integers of length > 0
Returns the maximum integer in a */
public static int findMax(int[] a){
for (int i =0; i < a.length; i++){
if (a[i] > theMax) {
theMax = a[i];}
}
return theMax;
}
//Do some unit testing
public static void main (String [] argv){
int[] a = new int[5];
a[0] = 1; a[1] = 3; a[2] = 2; a[3] = 7; a[4] = 5;
System.out.println(findMax(a));
}
}
1 comment:
I've done this programming problem on my blog. Here .
Post a Comment