Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2024

 

Tasks | Task groups | Matrix

PrevNext


Two-dimensional arrays (matrices)

The condition "An M × N matrix of integers (or real numbers) is given" means that the actual size of a two-dimensional array and all its elements are given (M is the number of rows and N is the number of columns, the matrix has M·N elements). The amount of rows (columns) of any matrix is assumed to be in the range 2 to 10 if the task does not specify it explicitly. The order number of the first row (column) of matrix is assumed to be equal to 1, the element AI,J is assumed to be in the I-th row and J-th column (I = 1, …, M, J = 1, …, N). An input/output of matrix elements must be performed in the order of rows, that is, in ascending order of their indices with the second index (the column index) changing faster than the first one (the row index).

A matrix having the size M × M is called a square matrix of order M; the actual size of square matrix is defined by one integer M.

If a task connected with matrix creation or matrix changing does not specify output data then the resulting matrix elements are supposed to be output (by rows).

Since version 4.19, the taskbook includes two versions of this group. The new version, named ZMatrix, differs from the original one in that it associates with rows, columns, and elements of matrices not order numbers (starting with 1), but indices (starting with 0); the prefix Z in the group name can be interpreted as "zero-indexed". The ZMatrix group is convenient for languages in which elements of arrays and other collections are always indexed from 0. In the ZMatrix group, compared to the Matrix group, the tasks with the following numbers have been changed: 7–12, 17–18, 21–22, 25–26, 31–35, 40–41, 47–48, 61–62, 68–69. In some tasks, only the wording was corrected (the initial and resulting data sets remained unchanged): 1–2, 13–16, 53–54, 59–60, 80–87, 96–100.

Two-dimensional arrays (matrices): creation

The size of a resulting matrix is assumed to be not greater than 10 × 10 in all tasks connected with the matrix creation.

Matrix1. Given two positive integers M and N, create and output an M × N matrix of integers such that all its elements of the I-th row are equal to 10·I (I = 1, …, M).

Matrix2. Given two positive integers M and N, create and output an M × N matrix of integers such that all its elements of the J-th column are equal to 5·J (J = 1, …, N).

Matrix3. Two positive integers M, N and a sequence of M real numbers are given. Create and output an M × N matrix of real numbers such that each of its columns contains all numbers from the given sequence (in the same order).

Matrix4. Two positive integers M, N and a sequence of M real numbers are given. Create and output an M × N matrix of real numbers such that each of its rows contains all numbers from the given sequence (in the same order).

Matrix5. Two positive integers M and N, a real number D, and a sequence of M real numbers are given. Create and output an M × N matrix of real numbers such that its first column contains all numbers from the given sequence (in the same order), and elements of each next column are equal to the sum of the corresponding element of the previous column and the number D (so each row of the matrix will be an arithmetic sequence with the common difference D).

Matrix6. Two positive integers M and N, a real number D, and a sequence of M real numbers are given. Create and output an M × N matrix of real numbers such that its first row contains all numbers from the given sequence (in the same order), and elements of each next row are equal to the sum of the corresponding element of the previous row and the number R (so each column of the matrix will be a geometric sequence with the common ratio R).

Two-dimensional arrays (matrices): output

Matrix7°. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ M). Output elements of the matrix row with the order number K.

Matrix8. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ M). Output elements of the matrix column with the order number K.

Matrix9. An M × N matrix of real numbers is given. Output elements of its rows with even order numbers (2, 4, …). An output of matrix elements must be performed in the order of rows. Do not use conditional statements.

Matrix10. An M × N matrix of real numbers is given. Output elements of its columns with odd order numbers (1, 3, …). An output of matrix elements must be performed in the order of columns. Do not use conditional statements.

Matrix11. An M × N matrix of real numbers is given. Output elements of the matrix in the following order: the first row from left to right, the second row from right to left, the third row from left to right, the fourth row from right to left, and so on.

Matrix12. An M × N matrix of real numbers is given. Output elements of the matrix in the following order: the first column from up to down, the second column from down to up, the third column from up to down, the fourth column from down to up, and so on.

Matrix13. A real-valued square matrix A of order M is given. Starting with the element A1,1, output its elements as follows: all elements of the first row, all elements of the M-th column except the element A1,M (which is already output), all remaining elements of the second row, all remaining elements of the (M−1)-th column, and so on; the element AM,1 must be output in the end. All rows must be output from left to right, all columns must be output from up to down.

Matrix14. A real-valued square matrix A of order M is given. Starting with the element A1,1, output its elements as follows: all elements of the first column, all elements of the M-th row except the element AM,1 (which is already output), all remaining elements of the second column, all remaining elements of the (M−1)-th row, and so on; the element A1,M must be output in the end. All rows must be output from left to right, all columns must be output from up to down.

Matrix15. A real-valued square matrix A of order M is given (M is an odd number). Starting with the element A1,1 and moving clockwise, output all matrix elements in the spiral order: the first row from left to right, the last column from up to down, the last row from right to left, the first column from down to up, all remaining elements of the second row (from left to right), and so on; the central element of the matrix must be output in the end.

Matrix16. A real-valued square matrix A of order M is given (M is an odd number). Starting with the element A1,1 and moving counter-clockwise, output all matrix elements in the spiral order: the first column from up to down, the last row from left to right, the last column from down to up, the first row from right to left, all remaining elements of the second column (from up to down), and so on; the central element of the matrix must be output in the end.

Two-dimensional arrays (matrices): analysis

Matrix17. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ M). Find the sum and the product of elements of the matrix row with the order number K.

Matrix18. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ N). Find the sum and the product of elements of the matrix column with the order number K.

Matrix19. An M × N matrix of real numbers is given. Find the sum of elements for each matrix row.

Matrix20. An M × N matrix of real numbers is given. Find the product of elements for each matrix column.

Matrix21. An M × N matrix of real numbers is given. Find the average of elements for each matrix row with odd order number (1, 3, …). Do not use conditional statements.

Matrix22. An M × N matrix of real numbers is given. Find the sum of elements for each matrix column with even order number (2, 4, …). Do not use conditional statements.

Matrix23. An M × N matrix of real numbers is given. Find the minimal element for each matrix row.

Matrix24°. An M × N matrix of real numbers is given. Find the maximal element for each matrix column.

Matrix25. An M × N matrix of real numbers is given. Find the order number of the matrix row with the maximal sum of elements. Output this order number and the maximal sum value.

Matrix26. An M × N matrix of real numbers is given. Find the order number of the matrix column with the minimal product of elements. Output this order number and the minimal product value.

Matrix27. An M × N matrix of real numbers is given. Find the maximal value among the minimal elements of matrix rows.

Matrix28. An M × N matrix of real numbers is given. Find the minimal value among the maximal elements of matrix columns.

Matrix29. An M × N matrix of real numbers is given. For each matrix row find the amount of elements that are smaller than the average of all elements of this row.

Matrix30. An M × N matrix of real numbers is given. For each matrix column find the amount of elements that are greater than the average of all elements of this column.

Matrix31. An M × N matrix of real numbers is given. Find the order numbers of row and column for an element whose value is the closest to the average of all matrix elements.

Matrix32. An M × N matrix of integers is given. Find the order number of the first matrix row that contains the equal amount of positive and negative elements (zero elements are not considered). If the matrix does not contain the required rows then output 0.

Matrix33. An M × N matrix of integers is given. Find the order number of the last matrix column that contains the equal amount of positive and negative elements (zero elements are not considered). If the matrix does not contain the required columns then output 0.

Matrix34. An M × N matrix of integers is given. Find the order number of the last matrix row that contains even numbers only. If the matrix does not contain the required rows then output 0.

Matrix35. An M × N matrix of integers is given. Find the order number of the first matrix column that contains odd numbers only. If the matrix does not contain the required columns then output 0.

Matrix36°. An M × N matrix of integers is given, values of its elements are in the range 0 to 100. A matrix row is called the similar with the other row if these rows contain the same set of numbers. For example, rows (1, 3, 3, 2) and (2, 2, 1, 3) contain the same set {1, 2, 3} and therefore they are the similar rows. Find the amount of matrix rows that are the similar with the first row.

Matrix37. An M × N matrix of integers is given, values of its elements are in the range 0 to 100. A matrix column is called the similar with the other column if these columns contain the same set of numbers. For example, columns (1, 3, 3, 2) and (2, 2, 1, 3) contain the same set {1, 2, 3} and therefore they are the similar columns. Find the amount of matrix columns that are the similar with the last column.

Matrix38. An M × N matrix of integers is given. Find the amount of its rows that contain no elements with equal values.

Matrix39. An M × N matrix of integers is given. Find the amount of its columns that contain no elements with equal values.

Matrix40. An M × N matrix of integers is given. Find the order number of the last row that contains the maximal amount of elements with equal values.

Matrix41. An M × N matrix of integers is given. Find the order number of the first column that contains the maximal amount of elements with equal values.

Matrix42. An M × N matrix of real numbers is given. Find the amount of its rows whose values of elements are sorted in ascending order.

Matrix43. An M × N matrix of real numbers is given. Find the amount of its columns whose values of elements are sorted in descending order.

Matrix44. An M × N matrix of real numbers is given. Find minimal element among elements of all matrix rows whose values of elements are sorted in ascending or descending order. If the matrix does not contain the required rows then output 0 (as a real number).

Matrix45. An M × N matrix of real numbers is given. Find maximal element among elements of all matrix columns whose values of elements are sorted in ascending or descending order. If the matrix does not contain the required columns then output 0 (as a real number).

Matrix46. An M × N matrix of integers is given. Find the matrix element that is the maximum in its row and the minimum in its column. If the matrix does not contain such elements then output 0.

Two-dimensional arrays (matrices): changing

Matrix47. An M × N matrix of real numbers and two integers K1, K2 are given (1 ≤ K1 < K2 ≤ M). Exchange matrix rows with the order numbers K1 and K2.

Matrix48. An M × N matrix of real numbers and two integers K1, K2 are given (1 ≤ K1 < K2 ≤ N). Exchange matrix columns with the order numbers K1 and K2.

Matrix49. An M × N matrix of real numbers is given. For each matrix row exchange values of its minimal and maximal element.

Matrix50. An M × N matrix of real numbers is given. For each matrix column exchange values of its minimal and maximal element.

Matrix51. An M × N matrix of real numbers is given. Exchange matrix rows containing the minimal and the maximal element of the matrix.

Matrix52. An M × N matrix of real numbers is given. Exchange matrix columns containing the minimal and the maximal element of the matrix.

Matrix53°. An M × N matrix of real numbers is given. Exchange the column with the order number 1 and the last column that contains positive numbers only. If the matrix does not contain the required columns then do not change it.

Matrix54. An M × N matrix of real numbers is given. Exchange the column with the order number N and the first column that contains negative numbers only. If the matrix does not contain the required columns then do not change it.

Matrix55. An M × N matrix of real numbers is given (M is an even number). Exchange the upper and lower half of the matrix.

Matrix56. An M × N matrix of real numbers is given (N is an even number). Exchange the left and right half of the matrix.

Matrix57. An M × N matrix of real numbers is given (M and N are even numbers). Exchange the upper left and lower right quarter of the matrix.

Matrix58. An M × N matrix of real numbers is given (M and N are even numbers). Exchange the upper right and lower left quarter of the matrix.

Matrix59. An M × N matrix of real numbers is given. Reflect its elements about the horizontal axis of symmetry of the matrix (that is, exchange matrix rows with the order numbers 1 and M, 2 and M − 1, and so on).

Matrix60. An M × N matrix of real numbers is given. Reflect its elements about the vertical axis of symmetry of the matrix (that is, exchange matrix columns with the order numbers 1 and N, 2 and N − 1, and so on).

Matrix61. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ M). Remove the matrix row with the order number K.

Matrix62. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ N). Remove the matrix column with the order number K.

Matrix63. An M × N matrix of real numbers is given. Remove the matrix row that contains the minimal matrix element.

Matrix64. An M × N matrix of real numbers is given. Remove the matrix column that contains the maximal matrix element.

Matrix65. An M × N matrix of real numbers is given. Remove its first column that contains positive numbers only. If the matrix does not contain the required columns then do not change it.

Matrix66. An M × N matrix of real numbers is given. Remove its last column that contains negative numbers only. If the matrix does not contain the required columns then do not change it.

Matrix67. An M × N matrix of real numbers is given. The matrix contains both positive and negative numbers. Remove all matrix columns that contain positive numbers only. If the matrix does not contain the required columns then do not change it.

Matrix68. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ M). Insert a new row of elements with zero value before the matrix row with the order number K.

Matrix69. An M × N matrix of real numbers and an integer K are given (1 ≤ K ≤ N). Insert a new column of elements with the value 1 after the matrix column with the order number K.

Matrix70. An M × N matrix of real numbers is given. Double the occurrence of the matrix row that contains the maximal matrix element.

Matrix71. An M × N matrix of real numbers is given. Double the occurrence of the matrix column that contains the minimal matrix element.

Matrix72. An M × N matrix of real numbers is given. Insert a new column of elements with the value 1 before the first matrix column that contains positive numbers only. If the matrix does not contain the required columns then do not change it.

Matrix73. An M × N matrix of real numbers is given. Insert a new column of elements with zero value after the last matrix column that contains negative numbers only. If the matrix does not contain the required columns then do not change it.

Matrix74°. An M × N matrix of real numbers is given. A matrix element is called the local minimum if it is smaller than all its neighbors. Replace all local minimums of the matrix by zero values. An additional matrix may be used for performing the required replacement.

Matrix75. An M × N matrix of real numbers is given. A matrix element is called the local maximum if it is greater than all its neighbors. Replace values of all local maximums of the matrix by opposite values. An additional matrix may be used for performing the required replacement.

Matrix76. An M × N matrix of real numbers is given. Rearrange the matrix rows so that values of their first elements were in ascending order.

Matrix77. An M × N matrix of real numbers is given. Rearrange the matrix columns so that values of their last elements were in descending order.

Matrix78. An M × N matrix of real numbers is given. Rearrange the matrix rows so that minimal values of their elements were in descending order.

Matrix79. An M × N matrix of real numbers is given. Rearrange the matrix columns so that maximal values of their elements were in ascending order.

Two-dimensional arrays (matrices): diagonals

Matrix80. A real-valued square matrix A of order M is given. Find the sum of elements of its main diagonal:

A1,1,    A2,2,    A3,3,    …,    AM,M.

Matrix81. A real-valued square matrix A of order M is given. Find the average of elements of its secondary diagonal:

A1,M,    A2,M−1,    A3,M−2,    …,    AM,1.

Matrix82°. A real-valued square matrix A of order M is given. Find the sum of elements of each matrix diagonal that is parallel to the main diagonal. Begin with the single-element diagonal A1,M.

Matrix83. A real-valued square matrix A of order M is given. Find the sum of elements of each matrix diagonal that is parallel to the secondary diagonal. Begin with the single-element diagonal A1,1.

Matrix84. A real-valued square matrix A of order M is given. Find the average of elements of each matrix diagonal that is parallel to the main diagonal. Begin with the single-element diagonal A1,M.

Matrix85. A real-valued square matrix A of order M is given. Find the average of elements of each matrix diagonal that is parallel to the secondary diagonal. Begin with the single-element diagonal A1,1.

Matrix86. A real-valued square matrix A of order M is given. Find the minimal value of elements of each matrix diagonal that is parallel to the main diagonal. Begin with the single-element diagonal A1,M.

Matrix87. A real-valued square matrix A of order M is given. Find the maximal value of elements of each matrix diagonal that is parallel to the secondary diagonal. Begin with the single-element diagonal A1,1.

Matrix88°. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie below the main diagonal. Do not use conditional statements.

Matrix89. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie above the secondary diagonal. Do not use conditional statements.

Matrix90. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie on the secondary diagonal or below it. Do not use conditional statements.

Matrix91. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie on the main diagonal or above it. Do not use conditional statements.

Matrix92. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie above the main diagonal and above the secondary diagonal simultaneously. Do not use conditional statements.

Matrix93. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie above the main diagonal and below the secondary diagonal simultaneously. Do not use conditional statements.

Matrix94. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie below the main diagonal (or on it) and above the secondary diagonal (or on it) simultaneously. Do not use conditional statements.

Matrix95. A real-valued square matrix of order M is given. Assign zero value to the matrix elements that lie below the main diagonal (or on it) and below the secondary diagonal (or on it) simultaneously. Do not use conditional statements.

Matrix96. A real-valued square matrix A of order M is given. Reflect its elements about the main diagonal (that is, exchange values of matrix elements A1,2 and A2,1, A1,3 and A3,1, and so on). Do not use an additional matrix.

Matrix97. A real-valued square matrix A of order M is given. Reflect its elements about the secondary diagonal (that is, exchange values of matrix elements A1,1 and AM,M, A1,2 and AM−1,M, and so on). Do not use an additional matrix.

Matrix98. A real-valued square matrix A of order M is given. Rotate its elements by 180° (that is, exchange values of matrix elements A1,1 and AM,M, A1,2 and AM,M−1, and so on). Do not use an additional matrix.

Matrix99. A real-valued square matrix A of order M is given. Rotate its elements counter-clockwise by 90° (that is, assign an initial value of A1,1 to AM,1, an initial value of AM,1 to AM,M, and so on). Do not use an additional matrix.

Matrix100°. A real-valued square matrix A of order M is given. Rotate its elements clockwise by 90° (that is, assign an initial value of A1,1 to A1,M, an initial value of A1,M to AM,M, and so on). Do not use an additional matrix.


PrevNext

 

  Ðåéòèíã@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
01.01.2024