class MinAlgorithm { public static void main ( String[] args ) { int[] array = { -20, 19, 1, 5, -1, 27, 19, 5 } ; int min; // initialize the current minimum min = array[0]; // scan the array for ( int val : array ) { if ( val < min ) min = val ; } System.out.println("The minimum of this array is: " + min ); } }
Say that you want to compute the sum of a list of numbers. This algorithm sounds much like those of the previous two programs.
The sum is initialized to zero. Then the loop adds each element of the array to the sum.
You could initialize the sum to the first number and then have the loop add the remaining numbers. But there is no advantage in doing this.
Here is the program.
The array
contains values of type double
.
Assume that the array contains at least one element.
Complete the program by filling in the blanks.