Reputation: 51
I have to make a square array that shows me at the output: 1.
1 1 1 1 1
1 2 2 2 2
1 2 3 3 3
1 2 3 4 4
1 2 3 4 5
I was thinking about this solution, but the output is wrong:
public class Main
{
public static void main(String[] args) {
int[][] nowatablica = new int[7][7];
System.out.println();
for (int i = 0; i < nowatablica.length;i++ ) {
for (int j = 0; j < nowatablica.length; j++) {
nowatablica[i][j] = i+j;
System.out.print(nowatablica [i][j] + " ");
}
System.out.println("");
}
}
}
Can you tell me where I can find the probelem? Thanks.
Upvotes: 0
Views: 37
Reputation: 1155
The problem lies in nowatablica[i][j] = i+j;
. You are not calculating the value right. If you look at the array, you notice that the value 1 exists only in row 0 and column 0, the value 2 exists only in cells [1, x] and [x, 1], with x>=1 etc.
The correct expression is thus nowatablica[i][j] = Math.min(i, j)+1;
Upvotes: 1