<<< Printing array elements | Index | Enhanced for loop >>> |
Code that computes the average of the array of prices
double sum = 0.0; for (int i = 0; i < prices.length; i++) { sum += prices[i]; } double average = sum/prices.length;
Another way to compute the average in a for loop
double sum = 0.0; for (int i = 0; i < prices.length; sum += prices[i++]); average = sum / prices.length;
<<< Printing array elements | Index | Enhanced for loop >>> |