Question #278135

Create method:

Method name: maxIndex

Method parameters: An integer array named arr

Method return: An integer value

Method description: This method will find the index of the array arr with the maximum value.

For example, if arr ={1.0, 2.0, 5.5, 1.5}, then it returns 2 since the maximum is 5.5 and it is present in index 2. In case, there are two same values, it returns the index of the last same value.


Java


Expert's answer



import java.util.Scanner;


public class App {
	/***
	 * Method name: maxIndex 
	 * Method description: This method will find the index of
	 * the array arr with the maximum value.
	 * 
	 * @param arr Method parameters: An integer array named arr
	 * @return Method return: An integer value
	 */
	private static int maxIndex(int arr[]) {
		int max = arr[0];
		int index = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > max) {
				max = arr[i];
				index = i;
			}
		}
		return index;
	}


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter the size of array: ");
		int size = keyBoard.nextInt();
		int arr[] = new int[size];
		for (int i = 0; i < arr.length; i++) {
			System.out.print("Enter element " + (i + 1) + ": ");
			arr[i] = keyBoard.nextInt();
		}
		System.out.println("\nThe index of the array arr with the maximum value maxIndex: " + maxIndex(arr));


		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS