create 2d array from 1 entry in another 2d array code example
Example 1: adding elements in a specified column or row in a two dimensional array java
package multidimensionalarrays;
public class MultidimensionalArrays {
public static void main(String[] args) {
double sumOfRow = 0;
double[][] matrix = new double[3][4];
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter a 3 by 4 matrix row by row: ");
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
matrix[row][col] = input.nextDouble();
}
}
double[] sumOfCol =new double[matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sumOfRow += matrix[i][j];
sumOfCol[j] += matrix[i][j];
}
System.out.println("Sum of the elements at row " + row + " is: " + sumOfRow);
}
System.out.println("Sum of the elements at column " + col + " is: " + sumOfCol);
}
}
Example 2: c fill 2d array
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
disp[i][j] = disp[i][j];
}
}