-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_10.java
More file actions
48 lines (42 loc) · 1.4 KB
/
Copy pathExercise07_10.java
File metadata and controls
48 lines (42 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
/**************************************************************************
* main method gets the list, then uses the indexOfSmallestElements to find the
* index of the smallest element. If less than one element is passed in the
* the array it returns -1.
***************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_10 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] list = new double[10];
System.out.print("Enter ten numbers: ");
for (int i = 0; i < list.length; i++) {
list[i] = input.nextDouble();
}
int index = indexOfSmallestElement(list);
if(index < 0)
System.out.println("Error");
else
System.out.println("The index of the minimal value is " + index);
System.out.println("");
}
public static int indexOfSmallestElement(double[] array){
double smallest = Integer.MAX_VALUE;
int index = 0;
int smallestIndex = 0;
for(double i : array){
if(i < smallest){
smallest = i;
smallestIndex = index;
}
index++;
}
if(smallest != Integer.MAX_VALUE)
return smallestIndex;
else
return -1;
}
}