How To Calculate the Average of an Array in Java
This is one of the articles from our Java Tutorial for Beginners.
Task:
1. Create 2 arrays made of 5 integer numbers randomly chosen from the range 0 to 5.
2. Display the arrays in the console in two separate lines.
3. Calculate the average of the numbers of each array and then display a message that states which array has a larger average. However, if the two arrays have the same average, the message needs to say that they are the same.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Arrays; public class Test { public static void main(String[] args) { int [] mas1 = new int[5]; int [] mas2 = new int[5]; for (int i = 0; i < 5; i++) { mas1[i] = (int)(Math.random()*6); mas2[i] = (int)(Math.random()*6); } System.out.println(Arrays.toString(mas1)); System.out.println(Arrays.toString(mas2)); double average1 = 0; double average2 = 0; for (int i = 0; i < 5; i++) { average1 += mas1[i]; average2 += mas2[i]; } average1/=5; average2/=5; if(average1 > average1){ System.out.println("The average of the first array ("+average1+") is greater than the average of "+ "the second array ("+average2+")"); } else if(average1 < average2){ System.out.println("The average of the first array ("+average1+") is less than the average of "+ "the second array ("+average2+")"); } else { System.out.println("The averages of the arrays are the same ("+average1+")"); } } } |
Commentary:
We create two arrays of 5 randomly chosen integer numbers in the range 0 to 5.
1 2 |
int [] mas1 = new int[5]; int [] mas2 = new int[5]; |
We create a loop that will generate elements of the arrays. We generate numbers in the range 0 to 5. If you've forgotten how to generate numbers in Java, read this article: "Random Number Generation in Java"
1 2 3 4 |
for (int i = 0; i < 5; i++) { mas1[i] = (int)(Math.random()*6); mas2[i] = (int)(Math.random()*6); } |
We display each array in a line with the help of the Arrays class.
1 2 |
System.out.println(Arrays.toString(mas1)); System.out.println(Arrays.toString(mas2)); |
We create variables to store the averages of the arithmetic arrays.
1 2 |
double average1 = 0; double average2 = 0; |
We find the sum of the elements of each array and then divide each sum by the number of elements in each array. We do this to find the average of each array.
1 2 3 4 |
for (int i = 0; i < 5; i++) { average1 += mas1[i]; average2 += mas2[i]; } |
We compare the averages and then display the correct phrase in the console.
1 2 3 4 5 6 7 8 9 |
if(average1 > average1){ System.out.println("The average of the first array ("+average1+") is greater than the average of "+ "the second array ("+average2+")"); } else if(average1 < average2){ System.out.println("The average of the first array ("+average1+") is less than the average of "+ "the second array ("+average2+")"); } else { System.out.println("The averages of the arrays are the same ("+average1+")"); } |
You can find more articles in our Java Tutorial for Beginners.