skip to Main Content

Initialize a two-dimensional array with the values shown in the table below

1   0   0   0   1   0   0   0   1   0
0   4   0   0   0   4   0   0   0   4
0   0   9   0   0   0   9   0   0   0
0   0   0   16  0   0   0   16  0   0
1   0   0   0   25  0   0   0   25  0
0   4   0   0   0   36  0   0   0   36
0   0   9   0   0   0   49  0   0   0
0   0   0   16  0   0   0   64  0   0
1   0   0   0   25  0   0   0   81  0
0   4   0   0   0   36  0   0   0  100

I tried doing a diagonal pattern but can’t seem to execute this pattern.
This is my diagonal code.

In the above diagram, the diagonal pattern seems to start after every 3 intervals replaced with zeros

function matrixPattern() {
  let arr = [];
  let len = 10;

  for (let i = 0; i < len; i++) {
    arr[i] = [];

    for (let j = 0; j < len; j++) {
      arr[i][j] = i !== j ? 0 : (i + 1) * (j + 1)
    }
  }
  for (let i in arr) {
    let line = '';

    for (let j in arr[i]) {
      line += arr[i][j] + 't';
    }

    console.log(line);
  }
}
matrixPattern()

2

Answers


  1. You can use modulo logic to identify which diagonals get the non-zero values. Note that the distance between two diagonals is not 3 but 4: the first diagonal starts at (0, 0), and the one at its right at (4, 0)!

    The non-zero values are squares of the minimum distance to the left or the top. So they are not the multiplication of two different values, but two the same values. The inner assignment could be:

    arr[i][j] = (i - j) % 4 ? 0 : (Math.min(i, j) + 1) ** 2
    

    So:

    function matrixPattern() {
      let arr = [];
      let len = 10;
    
      for (let i = 0; i < len; i++) {
        arr[i] = [];
    
        for (let j = 0; j < len; j++) {
          arr[i][j] = (i - j) % 4 ? 0 : (Math.min(i, j) + 1) ** 2;
        }
      }
      return arr; 
    }
    
    const mat = matrixPattern();
    
    for (const row of mat) console.log(...row);
    Login or Signup to reply.
  2. Here is a solution that does not use modulo. It proceeds as follows.

    1. Create an n x n array arr will all elements equal to zero.
    2. Step down the diagonal by incrementing an index i from 0 to n-1.
    3. Set diag = (i+1)**2 and arr[i][i] = diag
    4. Set j = i+4
    5. While j < n, set arr[i][j] and arr[j][i] to diag then j = j+4
    6. If i = n-1 we are finished with the construction of arr; else set i = i+1 and repeat with Step 3.

    In step 5, when setting arr[i][j] = diag, i can be thought of as a row index and j as a column index. When setting arr[j][i] = diag, j can be thought of as a row index and i as a column index.

    Here is Ruby code to construct the array arr.

    n = 10
    # create an nxn array with all elements set to zero
    arr = Array.new(n) { Array.new(n,0) }
    # loop over i = 0 to n-1  
    n.times do |i|
      diag = (i+1)**2
      arr[i][i] = diag
      # starting with j = i + 4, increment j by 4 while j < n
      j = i + 4
      while j < n
        arr[i][j] = diag
        arr[j][i] = diag 
        j = j + 4
      end
    end
    

    arr can be displayed as follows, confirming it is the desired array.

    1   0   0   0   1   0   0   0   1   0
    0   4   0   0   0   4   0   0   0   4 
    0   0   9   0   0   0   9   0   0   0
    0   0   0  16   0   0   0  16   0   0
    1   0   0   0  25   0   0   0  25   0
    0   4   0   0   0  36   0   0   0  36
    0   0   9   0   0   0  49   0   0   0
    0   0   0  16   0   0   0  64   0   0
    1   0   0   0  25   0   0   0  81   0
    0   4   0   0   0  36   0   0   0 100
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search