Reference no: EM13162873
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 test program that prompts the user to enter ten numbers, invokes this method to return the index of the smallest element, and displays the index.
I've entered the following - But am receiving errors that note the void interferes with int. Please explain what the error is and how I can correct it. Thanks very much
import java.util.Scanner;
public class small{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] list = new double[10];
for(int i =0; i<list.length; i++){
System.out.printf("Enter the number for index %d : ",i);
list[i] = sc.nextDouble();
}
int smallI= indexOfSmallestElement(list);
double smallE = list[smallI];
System.out.println();
System.out.printf("The smallest element is %.2f and its index is %d.\n",smallE,smallI);
}
public static void int indexOfSmallestElement (double[] array) {
double currentValue = array[0];
int smallestIndex = 0;
for (int j=1; j < arrSize ; j++) {
if (array[j] < currentValue)
{
currentValue = array[j];
smallestIndex = j;
}
}
}