a) Write a Java method, lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. Also, write a program to test your method.
package index;
public class Index {
public static int lastLargestIndex(int arr [], int s){
int max = arr[0];
int index = 0;
for(int i=0; i<s; i++){
if(arr[i] > max){
max = arr[i];
index = i;
}
}
return index;
}
public static void main(String[] args) {
int [] arr1 = {4,7,8,4,8,10};
int len = arr1.length;
System.out.printf("The index of the last occurrence of the largest element: %d",lastLargestIndex(arr1, len));
}
}
Comments
Leave a comment