Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions

Question

^&* 7.2b What does it mean to beat the market?

Answered: 1 week ago