go to previous page   go to home page   go to next page hear noise

Answer:

for ( double val : array )
total += val ;

Sum of No Elements

Here is the revised program:

class SumArrayRevised
{

  public static void main ( String[] args ) 
  {
    double[] array =  {} ;

    // declare and initialize the total
    double total =    0.0 ;

    // add each element of the array to the total
    for ( double val : array )
      total += val ;
      
    System.out.println("The total is: " + total );
  }
}      

QUESTION 20:

But the array has no elements!!   Will the program compile?   Will the program run?   What is the sum of an array with no elements?