Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication41
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.  
  10.             double[,] matrix =
  11.             {
  12.                 {1, 2, 3},
  13.                 {4, 10, 6},
  14.                 {7, 8, 9}
  15.             };
  16.  
  17.             var det = DetRec(matrix);
  18.             Console.WriteLine(det);
  19.         }
  20.  
  21.         private static double DetRec(double[,] matrix)
  22.         {
  23.             if (matrix.Length == 4)
  24.             {
  25.                 return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0];
  26.             }
  27.             double sign = 1, result = 0;
  28.             for (int i = 0; i < matrix.GetLength(1); i++)
  29.             {
  30.                 double[,] minor = GetMinor(matrix, i);
  31.                 result += sign * matrix[0, i] * DetRec(minor);
  32.                 sign = -sign;
  33.             }
  34.             return result;
  35.         }
  36.  
  37.         private static double[,] GetMinor(double[,] matrix, int n)
  38.         {
  39.             double[,] result = new double[matrix.GetLength(0) - 1, matrix.GetLength(0) - 1];
  40.             for (int i = 1; i < matrix.GetLength(0); i++)
  41.             {
  42.                 for (int j = 0; j < n; j++)
  43.                     result[i - 1, j] = matrix[i, j];
  44.                 for (int j = n + 1; j < matrix.GetLength(0); j++)
  45.                     result[i - 1, j - 1] = matrix[i, j];
  46.             }
  47.             return result;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement