In this tutorial, I will show you how you can multiply two matrices using the C# programming language.
The full source code in C# can be found on my account on dotnetfiddle online tool.
This is the source code I used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | using System; using System.Collections.Generic; public class Program { public static void Main() { Console.WriteLine("Hello World"); int i,j,k; int[,] A = {{1, 2}, {3, 5}, {1,1976}}; int[,] B = {{5, 10, 5}, {100, 1000, 1}}; var ARows = A.GetLength(0); var ACols = A.GetLength(1); var BRows = B.GetLength(0); var BCols = B.GetLength(1); Console.WriteLine("Numebr of rows in matrix A: " + ARows + " and number of columns in matrix A: " + ACols); Console.WriteLine("Numebr of rows in matrix B: " + BRows + " and number of columns in matrix B: " + BCols); int[,] C = new int[ARows, BRows]; List<int> matrixDimA = new List<int>(); List<int> matrixDimB = new List<int>(); Console.WriteLine("Number of matrix A elements are :" + ARows *ACols); //matrixDimA.Reverse(); Console.WriteLine("Elements of matrix A elements are :"); for (i = 0; i < ARows; i++) { Console.Write("["); for (j = 0; j < ACols; j++) { if (j != ACols - 1) { Console.Write( A[i, j] + ","); } else { Console.Write( A[i, j]); } } Console.WriteLine("]"); } Console.WriteLine("Number of matrix A elements are :" + BRows *BCols); Console.WriteLine("Elements of matrix A elements are :"); for (i = 0; i < BRows; i++) { Console.Write("["); for (j = 0; j < BCols; j++) { if (j != BCols - 1) { Console.Write( B[i, j] + ","); } else { Console.Write( B[i, j]); } } Console.WriteLine("]"); } if((ARows == BCols) & (ACols == BRows)) { Console.WriteLine("Matrix multiplication is possible because: "); Console.WriteLine("Numebr of rows in matrix A: " + ARows + " = with number of columns in matrix B: " + BCols); Console.WriteLine("Numebr of columns in matrix A: " + ACols + " = with number of rows in matrix B: " + BRows); for (i = 0; i < ARows; i++) { Console.Write("["); for (j = 0; j < BRows; j++) { if (j != ACols - 1) { C[i, j] = A[i, j] * B[j, i]; Console.Write( C[i, j] + ","); } else { C[i, j] = A[i, j] * B[j, i]; Console.Write( C[i, j]); } } Console.WriteLine("]"); } } else { Console.WriteLine("Matrix multiplication rows is not possible."); } var CRows = C.GetLength(0); var CCols = C.GetLength(1); Console.WriteLine("Number of matrix C elements are :" + CRows *CCols); Console.WriteLine("Elements of matrix C elements are :"); for (i = 0; i < CRows; i++) { Console.Write("["); for (j = 0; j < CCols; j++) { if (j != CCols - 1) { Console.Write( C[i, j] + ","); } else { Console.Write( C[i, j]); } } Console.WriteLine("]"); } } } |
This is the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Hello World Numebr of rows in matrix A: 3 and number of columns in matrix A: 2 Numebr of rows in matrix B: 2 and number of columns in matrix B: 3 Number of matrix A elements are :6 Elements of matrix A elements are : [1,2] [3,5] [1,1976] Number of matrix A elements are :6 Elements of matrix A elements are : [5,10,5] [100,1000,1] Matrix multiplication is possible because: Numebr of rows in matrix A: 3 = with number of columns in matrix B: 3 Numebr of columns in matrix A: 2 = with number of rows in matrix B: 2 [5,200] [30,5000] [5,1976] Number of matrix C elements are :6 Elements of matrix C elements are : [5,200] [30,5000] [5,1976] |