Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions

Question

Qfd about smartwatch table

Answered: 1 week ago

Question

Which of these influenced your life most dramatically?

Answered: 1 week ago

Question

What roles have these individuals played in your life?

Answered: 1 week ago