J
J
Janna16032020-06-18 16:32:25
Java
Janna1603, 2020-06-18 16:32:25

How to solve a problem with a two-dimensional array?

Create a square two-dimensional integer array (the number of rows and columns is the same), and using the loop(s) fill its diagonal elements with ones;
Hey! Help solve the problem, please. I just wrote this, but I don’t understand how

int[][] arr4 = new int[4][4];
        for (int i = 0; i < arr4.length; i++) {
            for (int j = 0; j < arr4.length; j++) {
              
            }
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Ngorso, 2020-06-18
@Ngorso

In short, it is necessary to fill in units those cells for which i == j. Something in the spirit

if (i == j) {
 arr4[i][j] = 1;
}

L
Lyoshik, 2020-06-18
@Kot1que

int n = 4;
int[][] array = new int[n][n];

for (int i = 0; i < n; i++) {
    array[i][i] = 1;
    array[i][n - i - 1] = 1;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question