Answer to Question #246763 in Java | JSP | JSF for lukie

Question #246763

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.



1
Expert's answer
2021-10-05T18:16:20-0400


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));
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS