How to implement the following program:

  1. Keyboard input some n
  2. Choice k (number n): k from 1 to n (any, also keyboard input)
  3. Create n number of diagonal matrices (n × n), where k will be on the main diagonal.

Example:

  1. n = 3
  2. k1 = 2, k2 = 4, k3 = 7
  3. Matrices:

    (1 matrix)

    200 020 002 

    (2 matrix)

     400 040 004 

    (3 matrix)

     700 070 007 
  • Give the subject a heading that captures the essence. There are 100,500 questions about "programming in the Java language." - Enikeyschik
  • What specific step did you have problems with? - Enikeyschik
  • Create n is the number of n × n diagonal matrices. These matrices are necessary for further calculations in the program. I will refer to these matrices - Jhmxtp
  • What specific step is the problem? With the creation of the matrix, with the creation of the created matrix with data, with the creation of exactly n matrices, with the creation of an n × n matrix? - Enikeyschik

1 answer 1

Create n matrices of size n by n as follows:

 int[][][] matrices = new int[n][n][n]; 

You need to create an array that stores N arrays of NxN size. If you are familiar with C ++, this is the same as creating a pointer to pointers, each of which points to an array. I will give a small example of how to access the elements of matrices.

 for (int i = 0; i < n; i++) { // Перебор матриц for (int j = 0; j < n; j++) { // Перебор строк матрицы i for (int k = 0; k < n; k++) { // Перебор столбцов матрицы i // Ваш код здесь // Пр.: matrices[i][j][k] = 0 } } }