# Exercise 147

• February 23rd, 2014, 02:16 PM
Exercise 147
So here is the deal:

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

Code Java:

```  double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 }; ArrayList<Double> d = new ArrayList<Double>(); ArrayList<Double> e = new ArrayList<Double>();   for ( double x : q ) { d.add( x ); e.add ( new Double ( x ) ); }   double dTotal = 0.0, eTotal = 0.0;   for (double c : d) { double a += c; }   for (double f : e) { double b += f; }   return (dTotal / d.length); return (eTotal / e.length);```

--- Update ---

I switched it a little bit. It still gives me the same error:

Code Java:

```for (double c : d)   {   dTotal += c;   }       for (double f : e)   {   double eTotal += f;   }       System.out.print(dTotal / d.size());   System.out.print(eTotal / e.size());```

--- Update ---

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.
• February 23rd, 2014, 02:50 PM
jashburn
Re: Exercise 147
Hi. The problem in your code:

Code :

`double eTotal += f;`

You should remove the 'double' for 'eTotal' as it has already been declared in

Code :

`double dTotal = 0.0, eTotal = 0.0;`

Here's the working code:

Code :

```double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 }; ArrayList<Double> d = new ArrayList<Double>(); ArrayList<Double> e = new ArrayList<Double>();   for (double x : q) { d.add(x); e.add(new Double(x)); }   double dTotal = 0.0, eTotal = 0.0;   for (double c : d) { dTotal += c; }   for (double f : e) { // double eTotal += f; eTotal += f; }   System.out.println(dTotal / d.size()); System.out.println(eTotal / e.size());```

It prints the mean as 1.3800000000000003.
• February 23rd, 2014, 04:20 PM