198k views
5 votes
Write a Java program that creates a two-dimensional array of 10 rows of integers and 10 columns of integers. Each element has the integer 1 stored inside. After, for each column, display the sum of all the elements using for loops.

1 Answer

4 votes

Final answer:

To create the program, you can use nested loops to initialize the array with 1s and calculate the sum of each column.

Step-by-step explanation:

To create a Java program that creates a two-dimensional array of 10 rows and 10 columns, we can use nested loops. First, we initialize the array with all elements set to 1. Then, we iterate over each column, using a separate loop for each column. Inside the inner loop, we calculate the sum of the elements in that column.

int[][] array = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
array[i][j] = 1;
}
}

for (int j = 0; j < 10; j++) {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += array[i][j];
}
System.out.println("Sum of column " + j + ": " + sum);
}

User Magius
by
8.8k points