# How do improve the accuracy of this averaging program

• September 10th, 2013, 09:45 AM
Alge4
How do improve the accuracy of this averaging program
Hello,

My first post, been following a set of tutorials online. And it had been writing an averager, were the number of values to be summed was defined in the code. I wanted this to be "user" set. So I attempted to give it a go. I got quite far through it and ran this. It was at that moment i realized I had an inaccuracy because i had absorbed each number as an int type. What i would like to do is to swap it out so that the program can output a decimal. I understand that this will mean i will have to use floats and doubles. But no idea how because most of what I've done has been copied (/tweaked) from examples within the course.

If anyone could help me alter this code so that it will handle decimal places and output decimal results that would be a great help.

Alge4

Code :

```package averager; import java.util.ArrayList; import javax.swing.JOptionPane;   public class Averager { public static void main(String[] args) { //Declare variables ArrayList<Integer> list = new ArrayList<Integer>() ; int Number, i; int total =0; int average = 0; Number = Integer.parseInt(JOptionPane.showInputDialog("Please the total amount of numbers to be averaged.")); int[] grid = new int[Number]; i =0; while(i < Number ){ list.add(Integer.parseInt(JOptionPane.showInputDialog("Please enter number" + (i+1) + "of the numbers to be averaged."))); i++; } for(i=0;i<Number;i++){ grid[i]= list.get(i);   } for(i=0;i<Number;i++){ total = grid[i]+total; } average= total/Number; JOptionPane.showMessageDialog(null,"The average of your "+Number+" Number(s) is: "+average);   } }```
• September 10th, 2013, 10:45 AM
jps
Re: How do improve the accuracy of this averaging program
Quote:

It was at that moment i realized I had an inaccuracy because i had absorbed each number as an int type.
Start there. Then work on upgrading the math and the output
• September 10th, 2013, 09:23 PM
Junky
Re: How do improve the accuracy of this averaging program
You can still have all the values as ints. When it comes to calculating the average you need at least one floating point value.

9 / 2 = int
9.0 / 2 = double
9 / 2.0 = double
9.0 / 2.0 = double

Easiest solution for you would be to have the denominator as a double.