Lesson 6: Data Structures, Arrays, and Strings
Activity 21: Finding the Smallest Number in an Array
Solution:
- Set up the main method in a new class file known as ExampleArray:
public class ExampleArray {
public static void main(String[] args) {
}
}
- Create an array of 20 numbers:
double[] array = {14.5, 28.3, 15.4, 89.0, 46.7, 25.1, 9.4, 33.12, 82, 11.3, 3.7, 59.99, 68.65, 27.78, 16.3, 45.45, 24.76, 33.23, 72.88, 51.23};
- Set the minimum float as the first number
double min = array[0];
- Create a for loop to check all the numbers in the array
for (doublefloat f : array) {
}
- Use if to test each number against the minimum. If it is smaller than the minimum then make that number the new minimum:
if (f < min)
min = f;
}
- After the loop completes, print out the minimum number:
System.out.println("The lowest number in the array is " + min);
}
}
The full code should look like this.
public class ExampleArray {
public static void main(String[] args) {
double...