# Guide to calculate Sum and Average using Java in 3D Array

25
Advertisement

Guide to Calculate Sum and Average using Java in 3D Array

Arrays are the building blocks of many programming tasks, and when we venture into the realm of three-dimensional arrays, the complexity and possibilities expand. In this blog post, we’ll explore how to find the sum and average of elements in a 3D array using Java, providing a step-by-step guide with practical programming examples and their outputs.

### Understanding 3D Arrays:

Before we delve into the code, let’s briefly understand what a 3D array is. Much like a 2D array is an array of arrays, a 3D array is an array of 2D arrays. In simpler terms, it’s like stacking multiple tables together.

### Calculating Sum in a 3D Array:

Summing the elements in a 3D array involves traversing through each dimension of the array using nested loops. The process is akin to exploring each table (2D array) in the stack and adding up the values.

Java
``````public class ThreeDimensionalArraySum {
public static void main(String[] args) {
// Initialize a 3D array
int[][][] threeDArray = {
{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}},
{{13, 14, 15}, {16, 17, 18}}
};

// Calculate the sum of elements
int sum = 0;
for (int i = 0; i < threeDArray.length; i++) {
for (int j = 0; j < threeDArray[i].length; j++) {
for (int k = 0; k < threeDArray[i][j].length; k++) {
sum += threeDArray[i][j][k];
}
}
}

// Print the result
System.out.println("Sum of elements in the 3D array: " + sum);
}
}``````

### Calculating Sum and Average in a 3D Array:

Calculating the average involves finding the sum as we did earlier and then dividing it by the total number of elements in the array.

Java
``````public class ThreeDimensionalArrayExample {
public static void main(String[] args) {
// Initialize a 3D array
int[][][] threeDArray = {
{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}},
{{13, 14, 15}, {16, 17, 18}}
};

// Calculate the sum of elements
int sum = 0;
for (int i = 0; i < threeDArray.length; i++) {
for (int j = 0; j < threeDArray[i].length; j++) {
for (int k = 0; k < threeDArray[i][j].length; k++) {
sum += threeDArray[i][j][k];
}
}
}

// Calculate the total number of elements
int totalElements = threeDArray.length * threeDArray[0].length * threeDArray[0][0].length;

// Calculate the average
double average = (double) sum / totalElements;

// Print the 3D array
System.out.println("Printing 3D Array:");

for (int i = 0; i < threeDArray.length; i++) {
for (int j = 0; j < threeDArray[i].length; j++) {
for (int k = 0; k < threeDArray[i][j].length; k++) {
System.out.print(threeDArray[i][j][k] + " ");
}
System.out.println(); // Move to the next line after each row
}
System.out.println(); // Separate each 2D array with an empty line
}

// Print the sum and average
System.out.println("Sum of elements in the 3D array: " + sum);
System.out.println("Average of elements in the 3D array: " + average);
}
}
``````

### Output:

Java
``````Printing 3D Array:
1 2 3
4 5 6

7 8 9
10 11 12

13 14 15
16 17 18

Sum of elements in the 3D array: 171
Average of elements in the 3D array: 9.5
``````

### Key Takeaways:

• 3D Array Structure: Visualize a 3D array as a stack of 2D arrays.
• Nested Loops: Traverse each dimension of the array using nested loops.
• Sum Calculation: Accumulate values during traversal to find the sum.
• Average Calculation: Divide the sum by the total number of elements to find the average.

You can create the same use case with using Java Array Guide to 3D Arrays with User Input.

In conclusion, 3D arrays in Java open doors to a world of possibilities, from graphics processing to scientific simulations. Understanding how to navigate and perform basic operations like sum and average is essential for any programmer. This blog post has provided a hands-on guide to get you started on your journey into the fascinating realm of 3D arrays. Happy coding!