fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Matrix {
  8. protected int row, column;
  9. protected int[,] ARRAY;
  10.  
  11. public int ROW {
  12. get { return row; }
  13. set { row = value; }
  14. }
  15.  
  16. public int COLUMN {
  17. get { return column; }
  18. set { column = value; }
  19. }
  20.  
  21. public Matrix() {
  22.  
  23. }
  24.  
  25. public Matrix(int row, int column) {
  26. this.row = ROW;
  27. this.column = COLUMN;
  28.  
  29. ARRAY = new int[this.COLUMN, this.ROW];
  30. }
  31.  
  32. public void EnterMatrix() {
  33. Console.WriteLine("enter the numbers of matrix columns: ");
  34. COLUMN = int.Parse(Console.ReadLine());
  35. Console.WriteLine("enter the numbers of matrix rows: ");
  36. ROW = int.Parse(Console.ReadLine());
  37.  
  38. ARRAY = new int[COLUMN, ROW];
  39.  
  40. for (int col = 0; col < COLUMN; col++) {
  41. for (int row = 0; row < ROW; row++) {
  42. Console.WriteLine("enter the elements of matrix cell[" + (col + 1) + ":" + (row + 1) + "]: ");
  43. ARRAY[col, row] = int.Parse(Console.ReadLine());
  44. }
  45. }
  46. }
  47.  
  48. public void Display() {
  49. for (int col = 0; col < COLUMN; col++) {
  50. Console.WriteLine("\n");
  51. for (int row = 0; row < ROW; row++) {
  52. Console.Write("{0}\t", ARRAY[col, row]);
  53. }
  54. }
  55. Console.WriteLine();
  56. }
  57.  
  58. public void ARRAYxARRAY(int[,] firstMatrix, int[,] secondMatrix, int[,] mult,
  59. int rowFirst, int columnFirst,
  60. int rowSecond, int columnSecond) {
  61. for (int i = 0; i < rowFirst; i++) {
  62. for (int j = 0; j < columnSecond; j++) {
  63. mult[i, j] = 0;
  64. }
  65. }
  66.  
  67. for (int i = 0; i < rowFirst; i++) {
  68. for (int j = 0; j < columnSecond; j++) {
  69. for (int k = 0; k < columnFirst; k++) {
  70. mult[i, j] += firstMatrix[i, k];
  71. }
  72. }
  73. }
  74. }
  75.  
  76. public void ARRAYxARRAY(Matrix Matrix, int a) {
  77. for (int col = 0; col < COLUMN; col++) {
  78. for (int row = 0; row < ROW; row++) {
  79. ARRAY[col, row] = a * Matrix.ARRAY[col, row];
  80. }
  81. }
  82. }
  83.  
  84. public void ARRAYxARRAY(int a, Matrix Matrix) {
  85. for (int col = 0; col < COLUMN; col++) {
  86. for (int row = 0; row < ROW; row++) {
  87. ARRAY[col, row] = a * Matrix.ARRAY[col, row];
  88. }
  89. }
  90. }
  91.  
  92. public void ARRAYxARRAY(Matrix fMatrix, Matrix sMatrix) {
  93. for (int col = 0; col < COLUMN; col++) {
  94. for (int row = 0; row < ROW; row++) {
  95. ARRAY[col, row] += fMatrix.ARRAY[col, row] * sMatrix.ARRAY[col, row];
  96. }
  97. }
  98. }
  99.  
  100. public void ARRAYxARRAY(Matrix Matrix, Vector Vector) {
  101. for (int col = 0; col < COLUMN; col++) {
  102. for (int row = 0; row < ROW; row++) {
  103. ARRAY[col, row] += Matrix.ARRAY[col, row] * Vector.ARRAY[col, row];
  104. }
  105. }
  106. }
  107.  
  108. ~Matrix() {
  109. Console.WriteLine("Matrix has been denied.");
  110. }
  111. }
  112.  
  113. class Vector : Matrix {
  114. public Vector() {
  115.  
  116. }
  117.  
  118. public Vector(int row, int column) {
  119. this.row = ROW;
  120. this.column = COLUMN;
  121.  
  122. ARRAY = new int[this.COLUMN, this.ROW];
  123. }
  124.  
  125. public void EnterVector() {
  126. Console.WriteLine("Choose type of vector: " +
  127. "\nvector-row= [1]" +
  128. "\nvector-col= [2]");
  129. int variant = int.Parse(Console.ReadLine());
  130.  
  131. if (variant == 1) {
  132. COLUMN = 1;
  133. Console.WriteLine("enter the number of vector elements: ");
  134. ROW = int.Parse(Console.ReadLine());
  135. }
  136. else if (variant == 2) {
  137. ROW = 1;
  138. Console.WriteLine("enter the numbers of vector elements: ");
  139. COLUMN = int.Parse(Console.ReadLine());
  140. }
  141.  
  142. ARRAY = new int[COLUMN, ROW];
  143.  
  144. for (int col = 0; col < COLUMN; col++) {
  145. for (int row = 0; row < ROW; row++) {
  146. Console.WriteLine("enter the elements of matrix cell[" + (col + 1) + ":" + (row + 1) + "]: ");
  147. ARRAY[col, row] = int.Parse(Console.ReadLine());
  148. }
  149. }
  150. }
  151.  
  152. public void ARRAYxARRAY(Vector fVector, Vector sVector) {
  153. for (int col = 0; col < COLUMN; col++) {
  154. for (int row = 0; row < ROW; row++) {
  155. ARRAY[col, row] += fVector.ARRAY[col, row] * sVector.ARRAY[col, row];
  156. }
  157. }
  158. }
  159.  
  160. ~Vector() {
  161. Console.WriteLine("Vector has been denied.");
  162. }
  163. }
  164.  
  165. class Test {
  166. static void Main() {
  167. Matrix MATRIX = new Matrix();
  168. MATRIX.EnterMatrix();
  169. Console.WriteLine("The matrix is: ");
  170. MATRIX.Display();
  171.  
  172. Console.WriteLine("\n");
  173.  
  174. Vector VECTOR = new Vector();
  175. VECTOR.EnterVector();
  176. Console.WriteLine("The matrix is: ");
  177. VECTOR.Display();
  178.  
  179. while (true) {
  180. Console.Write("Select anoperation:\n"
  181. + "\t1. matrix * matrix;\n"
  182. + "\t2. matrix * vector;\n"
  183. + "\t3. number * matrix;\n"
  184. + "\t4. vector * matrix;\n"
  185. + "\t5. vector * vector;\n"
  186. + "\t6. number * vectir;\n");
  187. int choice = int.Parse(Console.ReadLine());
  188.  
  189. switch (choice) {
  190. case 1: Matrix MATRIXbyMATRIX = new Matrix();
  191. MATRIXbyMATRIX.ARRAYxARRAY(MATRIX, MATRIX);
  192. MATRIXbyMATRIX.Display();
  193. break;
  194. case 2:
  195. Matrix MATRIXbyVECTOR = new Matrix();
  196. MATRIXbyVECTOR.ARRAYxARRAY(MATRIX, VECTOR);
  197. MATRIXbyVECTOR.Display();
  198. break;
  199. case 3:
  200. Matrix NUMBERbyMATRIX = new Matrix();
  201. Console.Write("enter the number to multiply by: ");
  202. int numb1 = int.Parse(Console.ReadLine());
  203. NUMBERbyMATRIX.ARRAYxARRAY(numb1, MATRIX);
  204. NUMBERbyMATRIX.Display();
  205. break;
  206. case 4:
  207. Matrix VECTORbyMATRIX = new Matrix();
  208. VECTORbyMATRIX.ARRAYxARRAY(VECTOR, MATRIX);
  209. VECTORbyMATRIX.Display();
  210. break;
  211. case 5:
  212. Matrix VECTORbyVECTOR = new Matrix();
  213. VECTORbyVECTOR.ARRAYxARRAY(VECTOR, VECTOR);
  214. VECTORbyVECTOR.Display();
  215. break;
  216. case 6:
  217. Matrix NUMBERbyVECTOR = new Matrix();
  218. Console.Write("enter the number to multiply by: ");
  219. int numb2 = int.Parse(Console.ReadLine());
  220. NUMBERbyVECTOR.ARRAYxARRAY(numb2, VECTOR);
  221. NUMBERbyVECTOR.Display();
  222. break;
  223. default:
  224. return;
  225. }
  226. }
  227. }
  228. }
Success #stdin #stdout 0.03s 15108KB
stdin
3
3
1
0
0
0
1
0
0
0
1
2
3
1
2
3
2
7
stdout
enter the numbers of matrix columns: 
enter the numbers of matrix rows: 
enter the elements of matrix cell[1:1]: 
enter the elements of matrix cell[1:2]: 
enter the elements of matrix cell[1:3]: 
enter the elements of matrix cell[2:1]: 
enter the elements of matrix cell[2:2]: 
enter the elements of matrix cell[2:3]: 
enter the elements of matrix cell[3:1]: 
enter the elements of matrix cell[3:2]: 
enter the elements of matrix cell[3:3]: 
The matrix is: 


1	0	0	

0	1	0	

0	0	1	


Choose type of vector: 
vector-row= [1]
vector-col= [2]
enter the numbers of vector elements: 
enter the elements of matrix cell[1:1]: 
enter the elements of matrix cell[2:1]: 
enter the elements of matrix cell[3:1]: 
The matrix is: 


1	

2	

3	
Select anoperation:
	1. matrix * matrix;
	2. matrix * vector;
	3. number * matrix;
	4. vector * matrix;
	5. vector * vector;
	6. number * vectir;

Select anoperation:
	1. matrix * matrix;
	2. matrix * vector;
	3. number * matrix;
	4. vector * matrix;
	5. vector * vector;
	6. number * vectir;
Vector has been denied.
Matrix has been denied.
Matrix has been denied.
Matrix has been denied.