a) Draw a flowchart, 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.
Source code
public class Main
{
static int getlastLargestIndex(int [] arr, int n){
int max = arr[0];
int lastLargestIndex=0;
for(int i=0;i<n;i++){
if(arr[i]>=max){
max=arr[i];
lastLargestIndex=i;
}
}
return lastLargestIndex;
}
public static void main(String[] args) {
int [] arr={10,11,12,24,21,24,12,24,23,21};
int n=10;
System.out.println("The elements of the array are: ");
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
System.out.println("\nThe index of the last occurrence of the largest element in the array: "+getlastLargestIndex(arr,n));
}
}
Output
Comments
Leave a comment