In: Computer Science
Create a program using Java.
Create two different 3D arrays with random numbers. (lets name them array1 and array 2)
Add the two 3Darrays together and then get the average.
Save the average on a separate 3D array.(lets name it array3)
Then add array1 and array3 then get the average
Save the average on a separate 3Darray(lets name it array 4)
Then add array2 and array3 then get the average
Save the average on a separate 3Darray (lets name it array5)
Use recursion
CODE
import java.util.Arrays;
import java.util.Random;
public class Main
{
public static double[][][] getRandomArray(int a, int b, int c) {
double[][][] arr = new double[a][b][c];
Random random = new Random();
for (int i=0; i<a; i ++) {
for (int j=0; j<b; j++) {
for (int k=0; k<c; k++) {
arr[i][j][k] = random.nextInt(100);
}
}
}
return arr;
}
public static double[][][] average(double array1[][][], double array2[][][], int a, int b, int c) {
double[][][] arr = new double[a][b][c];
for (int i=0; i<a; i ++) {
for (int j=0; j<b; j++) {
for (int k=0; k<c; k++) {
arr[i][j][k] = (array1[i][j][k] + array2[i][j][k])/2;
}
}
}
return arr;
}
public static void printArray(double arr[][][]) {
for (int i=0; i<arr.length; i ++) {
for (int j=0; j<arr[i].length; j++) {
for (int k=0; k<arr[i][j].length; k++) {
System.out.print(arr[i][j][k] + " ");
}
System.out.println();
}
}
}
public static void main (String[] args)
{
double array1[][][] = getRandomArray(3, 3, 3);
double array2[][][] = getRandomArray(3, 3, 3);
double array3[][][] = average(array1, array2, 3, 3, 3);
double array4[][][] = average(array1, array3, 3, 3, 3);
double array5[][][] = average(array2, array3, 3, 3, 3);
printArray(array1);
System.out.println();
printArray(array2);
System.out.println();
printArray(array3);
System.out.println();
printArray(array4);
System.out.println();
printArray(array5);
}
}