good mornig, given the following exercise Given the number n, not greater than 100, create a matrix of size n×n and fill it using the following rule. Numbers 0 should be stored on the primary (main) diagonal. The two diagonals, adjacent to the primary one, should contain numbers 1. The next two diagonals should contain numbers 2; etc.
Note: the primary diagonal runs from the top left corner to the bottom right corner.
Sample Output 1:
0 1 2 3 4
1 0 1 2 3
2 1 0 1 2
3 2 1 0 1
4 3 2 1 0
is possible do with loops without abs method?
`Scanner s = new Scanner(System.in);
System.out.print("inserisci numero");
int a = s.nextInt();
int arr[][] = new int [a][a];
System.out.println(Arrays.deepToString(arr));
for (int i = 0; i < arr.length; i ) {
for (int j = 0; j < arr[i].length; j ) {
int x = 0;
//here code is wrong
while (j< arr[i].length) {
if (i == j) {
arr[i][j]=x;
}if (j == i x) {
arr[i][j] = x;
}
j ;
x ;
}
/*} this is the principle but i can't put it on a loops
* if (i == j) {
arr[i][j]=0;
}if (j == i 1) {
arr[i][j] = 1;
} if (j == i 2) {
arr[i][j] = 2;
} if (j == i 3) {
arr[i][j] = 3;
} if (i == j 1) {
arr[i][j] = 1;
} if (i == j 2) {
arr[i][j] = 2;
} if (i == j 3) {
arr[i][j] = 3;*/
}
}
System.out.println(Arrays.deepToString(arr).replace("], ", "]\n").replace("[[", "[").replace("]]", "]"));
}`
CodePudding user response:
You are complicating the task unnecessarily. Try to work with the indices. For example, if we look at the indices of the main diagonal (i,j)
the indices are (0,0), (1,1), (2,2)
and so on. In other words, a main diagonal element always has indexes where i = j
. The diagonals Where ones are stored have the indices (1,0), (2,1), (3,2)...
in the lower area and (0,1), (1,2), (2,3)
and so on in the upper. so where the difference between i and j is 1. The same is true for the 2s. indexes with a difference of 2
such as (2,0), (3,1), (4,2)
and so on. Use this pattern to fill your matrix
public static void main(String[] args) {
int n = 5;
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i ) {
for (int j = 0; j < n; j ){
matrix[i][j] = Math.abs(i-j);
}
}
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
If, for whatever reason, you cann't use the Math.abs()
method just check which index is greater:
public static void main(String[] args) {
int n = 5;
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i ) {
for (int j = 0; j < n; j ){
if(i > j){
matrix[i][j] = i-j;
}
else {
matrix[i][j] = j-i;
}
}
}
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
CodePudding user response:
If you want to do this exercise without Math.abs, you can do in this way:
int n = 5;
int[][] matrix= new int[n][n];
for(int i = 0; i < n; i ){
for(int j = 0; j < n; j ){
if(j < i){
matrix[i][j] = matrix[i-1][j] 1;
}else if(j == i){
matrix[i][j] = 0;
}else{
matrix[i][j] = j - i;
}
}
}
for(int i = 0; i < n; i ){
for(int j = 0; j < n; j ){
System.out.print(matrix[i][j] "\t");
}
System.out.println();
}
you can place 0 when i and j are the same index. at the first round of second for, you have already place 0 on m[0][0], so you can use this value to place 1 under m[0][0]. if j > i, you can place the value obtained doing j - i.