# Thread: Calculating average of two arrays - Calculation coming out incorrect

1. Member Join Date
Sep 2014
Posts
10
Rep Power
0

## Calculating average of two arrays - Calculation coming out incorrect

I am working on an assignment, and I have come up with a program that runs, but it does not run correctly. This is the assignment:

Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array)
public static double average(double[] array)
Write a test program that prompts the user to enter ten double values, invokes this method and displays the average value.

When I run it my averages are coming out incorrect, and I am not sure why. I have tried moving and rearranging things and nothing is working. I appreciate any help/advice.
Java Code:
```
public static int average(int[] array) {
int sum = 0;
int average = 0;
for (int i = 0; i < array.length; i++) {
sum = sum + array[i];
}
average = sum / array.length;
return average;

}

public static double average(double[] array) {
double sum = 0;
double average = 0;
for (int i = 0; i < 5; i++) {
sum = sum + array[i];
}
average = sum / array.length;
return average;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Integer values: ");
int array1[] = new int;
for (int i = 0; i < array1.length; i++) {
array1[i] = input.nextInt();
}
int avg1 = average(array1);
System.out.println("Average of First Array: " + avg1);
System.out.println();

System.out.print("Enter Ten Double Values: ");
double array2[] = new double;
for (int i = 0; i < array1.length; i++) {
array2[i] = input.nextDouble();
}
double avg2 = average(array2);
System.out.println("Average of Second Array: " + avg2);

}
}```  Reply With Quote

2. ## Re: Calculating average of two arrays - Calculation coming out incorrect

make float instead of int ..ur code is correct only.  Reply With Quote

3. Senior Member Join Date
Oct 2010
Posts
393
Rep Power
10

## Re: Calculating average of two arrays - Calculation coming out incorrect

I would have to differ. Check line 15.

Regards.  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Calculating average of two arrays - Calculation coming out incorrect

I think you misread the assignment:

"Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array)
public static double average(double[] array)"

Regards,
Jim  Reply With Quote

5. Member Join Date
Sep 2014
Posts
10
Rep Power
0

## Re: Calculating average of two arrays - Calculation coming out incorrect

What is wrong with it? I am not noticing anything. Is it the sum = sum part you are talking about? I am new to this  Reply With Quote

6. ## Re: Calculating average of two arrays - Calculation coming out incorrect

Check the number of elements in that array ...

kind regards,

Jos  Reply With Quote

7. Member Join Date
Sep 2014
Posts
10
Rep Power
0

## Re: Calculating average of two arrays - Calculation coming out incorrect

Thank you I think I fixed it, plus I was not think the first one is only an int. So, if it is not even it will not show the exact number. Thank you again  Reply With Quote

java 