Question: **please don't use handwriting The attached java program attempts to find the index of the largest number within an array of integers using the following

**please don't use handwriting

The attached java program attempts to find the index of the largest number within an array of integers using the following method:

private static int largestNumberIndex(int[] a, int from){

if(from == a.length - 1)

return a[a.length - 1];

return largestNumberIndex(a, from+1) > a[from] ? largestNumberIndex(a, from+1) : a[from];

}

However, this code return the largest number in the array but not the index, for instance using the example in the program with an array like:

int[] a = {10, 3, 70, 5000, 8, 1900, 12, 700, 1000, 230};

The program will return 5000.

Your Task:

Make a very small code modification so that the index of the largest number will be returned and not the value from the array, so for the same example it will return 3 which is the index of the number 5000 in the array.

Please use this code:

public class MaxIndex{ private static int largestNumberIndex(int[] a, int from){ if(from == a.length - 1) return a[a.length - 1]; return largestNumberIndex(a, from+1) > a[from] ? largestNumberIndex(a, from+1) : a[from]; } public static void main(String[] args){ int[] a = {10, 3, 70, 5000, 8, 1900, 12, 700, 1000, 230}; System.out.println(largestNumberIndex(a, 0)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!