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

The problem says: Write a method that finds the smallest element in an array of

ID: 3625734 • Letter: T

Question

The problem says:



Write a method that finds the smallest element in an array of integers using the following header:



public static double min(double[] array)



Write a test program that prompts the user to enter ten numbers, invokes this method to return the minimum value, and displays the minimum value. Here is a sample run of the program:



Enter ten numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2



The minimum number is: 1.5.







Now this is what I have:





import



java.util.Scanner;

public class Exercise6_9 {
public static void main(String[] args) {
double[] numbers = new double[10];

//Enter ten double numbers: Scanner(System.in)
System.out.println("Enter ten double numbers: ");
Scanner input =
new Scanner(System.in);

//Call method min and then display the result
public double min(double[] array) {
int size = array.length;
int smallest = array[0];
System.out.println(
"The minimum number is " + m);

}

}

public static double min(double[] list) {
double m = list[0]; //m is the smallest element

// for (i = 1 to list.length - 1)
for(int loop=0; loop<size; loop++)
// if (m is larger than list[i])
if(array[loop]<smallest)
// list[i] is the new smallest element
smallest = array[loop];
// return the smallest element, m
return m;
}
}





I don't know whats wrong with it!

Explanation / Answer

Next time, compile more often. You had a program structure that would not compile, also you were referencing variables not declared.

import java.util.Scanner;

public class Exercise6_9 {
public static void main(String[] args) {
double[] numbers = new double[10];

//Enter ten double numbers: Scanner(System.in)
System.out.print("Enter ten double numbers: ");
Scanner input = new Scanner(System.in);

double[] list = new double[10];

for(int i = 0; i < 10; i++)
{
list[i] = input.nextDouble();
}

System.out.println("Min: "+min(list));
}
public static double min(double[] list) {
double m = list[0]; //m is the smallest element

// for (i = 1 to list.length - 1)
for(int loop=1; loop<list.length; loop++)
// if (m is larger than list[i])
if(list[loop]<m)
// list[i] is the new smallest element
m = list[loop];
// return the smallest element, m
return m;
}
}