Question: Write a Java program named FindLargest with the following requirements 1. Write a method that returns the index of the largest element in an array

Write a Java program named FindLargest with the following requirements

1. Write a method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the index of the largest element. Use the following header:

public static int indexOfLargestElement(double[] array)

2. Write a main method that prompts the user to enter ten numbers, invokes this method to return the index of the largest element, and displays the index. Take note of the sample run of the program below:

Sample:

Enter ten numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2

The index of the largest element is 5

HINT: When searching array for largest element, use a temporary variable to hold max candidates, replacing that candidate with a new max until the end of your search.

**Please point out what I did wrong in my program.**

import java.util.Scanner;

public class FindLargest { static final int SIZE = 10; public static void main(String[] args) { double[] numbers = new double [SIZE]; Scanner input = new Scanner(System.in); System.out.print("Enter " + SIZE + " numbers: "); for (int i=0; i > numbers.length; i++) numbers[i] = input.nextDouble(); System.out.println("The index of the largest number is: " + indexofLargestElement(numbers)); } public static int indexofLargestElement(double[] array) { int index = 0; double high = array[index]; for (int i = 0; i < array.length; i++) { if (array[i]> high) { high = array[i]; index = i; } } return index; } }

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!