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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started