Average an Array

A linear array can be used to store numerical data.

    int[]  ages = {17 , 25, 13 , 18 , 19 ,  12 , 28 , 65 , 17 , 18};

Assume that the array ages[] contains int values.
Write a loop to calculate the average of all the ages.

Solution

int[]  ages = {17 , 25, 13 , 18 , 19 ,  12 , 28 , 65 , 17 , 18};

double sum = 0;   // use double so division is correct at the end

for(int  c = 0c < ages.length ; c = c + 1)
{
        sum = sum + ages[c];
}

output("Average = " + (sum / ages.length) );