I want to pass two arrays into methods that will calculate the averages of the elements in the arrays.
Here is my code.
import java.util.Scanner;
public class Averaginganarray {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Enter in the ten numbers for the array");
int[] x= new int[10];
for(int i =0; i <10; i++) {
x[i] = input.nextInt();
}
average(x);
}
public static int average(int[] array) {
int sum = 0 ;
for(int i =0; i<=array.length;i++) {
sum+=array[i];
}
int average = sum/10;
System.out.println("The average of the elements in the array is" + " " + average);
return average;
}
public static double average(double[] array) {
double sum = 0 ;
for(int k=0; k<=array.length;k++) {
sum+=array[k];
}
double average = sum/10;
System.out.println("The average of the elements in the array is" + " " + average);
return average;
}
}