// By Rami Jaloudi
// Java - Calculate Average With Array Input
import java.util.*;
public class CalculateAverageInputArray {
public static void main(String[] args) {
int Iterations;
System.out.print("Please enter the number of iterations to simulate: ");
Scanner scan = new Scanner(System.in);
Iterations = scan.nextInt();
double[] numbers = new double[Iterations];
System.out.print("Please enter the number of iterations to simulate "
+ Iterations + " times: " + "\n");
Scanner scan2 = new Scanner(System.in);
for(int i=0; i < Iterations ; i++)
numbers[i] = scan2.nextDouble();
//alternative methods: if you are looking to pre-define the array & with integers instead of double.
//define an array
//int[] numbers = new int[]{10,20,15,25,16,60,100};
/*
* Average value of array elements would be
* sum of all elements/total number of elements
*/
//calculate sum of all array elements
double sum=0;
for(int i=0; i < Iterations ; i++)
sum = sum + numbers[i];
//calculate average value
double average = sum / Iterations;
System.out.println("Average value of array elements is : " + average);
}
}
No comments:
Post a Comment