Question

In: Computer Science

Write a Java method that returns the index of the largest element in an array of...

Write a Java 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 smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.

Solutions

Expert Solution

The required JAVA program is given below,

import java.util.Arrays;
import java.util.Scanner;

class Ideone {
   // Finding the index of largest element
   public static int indexOfLargestElement(double[] array){
       double maxValue = array[0];
       int maxIndex = 0;
       // finding maximum value
   for (int i = 0; i < array.length; i++) {
           if (maxValue < array[i]) {
               maxValue = array[i];
           }
       }
       // finding first index of max value
       for (int i = 0; i < array.length; i++) {
           if (maxValue == array[i]) {
               maxIndex=i;
           }
       }
       return maxIndex;
   }
  
   public static void main(String args[]) {
   Scanner s = new Scanner(System.in);
       // input length of array , it can be hardcoded as 10
   System.out.println("Enter the length of the input array:");
   int length = s.nextInt();
   double [] array = new double[length];
       // entering the elements
   System.out.println("Enter the elements:");
   for(int i=0; i<length; i++ ) {
   array[i] = s.nextDouble();
   }
       // printing output
       System.out.println("Index of largest element is :");
   System.out.println(indexOfLargestElement(array));
}
}

SCREENSHOT


Related Solutions

Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a method called mode that returns the most frequently occurring element of an array of...
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT