Common Array Algorithms

This chapter discusses how a for loop can be used to iterate over the elements of a one-dimensional array. In addition, it also discusses enhanced for loops. The chapter demonstrates the use of arrays to solve common problems such as finding sum, average, maximum, and minimum values of numbers stored in array. Pay attention to some of the common programming errors one can make while using arrays.

16. The complete program is given below.


Answer:

Yes.

Complete Minimum-finding Program

As with the maximum-finding program, the trial minimum is initialized to the first (index 0) element of the array. Then it is updated as needed as each element of the array is inspected in the loop.

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 index=0; index < array.length; index++ )
    { 
      if ( array[ index ] < min )

        min = array[ index ] ;

    }
      
    System.out.println("The minimum of this array is: " + min );
  }
}


Question 16:

Could an enhanced for loop be used in this program?