In this example I’ll show you how to calculating determinant 2×2 of a matrix calculator.
C# Code:
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 |
class Program { public static double det(double[,] A, int N) { double c, r = 1; for (int i = 0; i < N; i++) { for (int k = i + 1; k < N; k++) { c = A[k,i] / A[i,i]; for (int j = i; j < N; j++) A[k,j] = A[k,j] - c * A[i,j]; } } for (int i = 0; i < N; i++) r *= A[i,i]; return r; } static void Main(string[] args) { int d = 2; double[,] M = new double[d, d]; M[0,0] = 8; M[0,1] = 15; M[1,0] = 3; M[1,1] = 7; //Print Matrix for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { Console.Write(M[i, j]+" "); } Console.WriteLine(); } Console.WriteLine("Det(M) = {0}", det(M, d)); Console.ReadLine(); } } |
Output: