Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method that returns the index of the smallest element in an array of int

ID: 3551564 • Letter: W

Question

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. ",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;

}

}

}




Explanation / Answer

The mistake u ade was u wrote two return types in function indexOfSmallestElement (double[] array)

as public static void int indexOfSmallestElement (double[] array) ;

it should be public static int indexOfSmallestElement (double[] array) ;



The code goes here...



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. ", smallE,

smallI);


}


public static int indexOfSmallestElement(double[] array) {

double currentValue = array[0];

int smallestIndex = 0;

for (int j = 1; j < array.length; j++) {

if (array[j] < currentValue) {

currentValue = array[j];

smallestIndex = j;

}

}

return smallestIndex;

}

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote