Advertisement
Slavik9510

Untitled

Dec 6th, 2022
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. class HelloWorld
  5. {
  6.     static void Main()
  7.     {
  8.         int n;
  9.         Console.Write("Введiть розмiрнiсть матрицi: ");
  10.         n = int.Parse(Console.ReadLine());
  11.         int[,] matrix = new int[n, n];
  12.         Console.WriteLine("1) Випадково згенерувати матрицю\n2) Ввести матрицю вручну");
  13.         int choise = int.Parse(Console.ReadLine());
  14.         int i, j;
  15.         switch (choise)
  16.         {
  17.             case 1:
  18.                 {
  19.                     Random r = new Random();
  20.                     for (i = 0; i < matrix.GetLength(0); i++)
  21.                     {
  22.                         for (j = 0; j < matrix.GetLength(1); j++)
  23.                         {
  24.                             matrix[i, j] = r.Next(0, 99);
  25.                         }
  26.                     }
  27.                     break;
  28.                 }
  29.             case 2:
  30.                 {
  31.                     for (i = 0; i < matrix.GetLength(0); i++)
  32.                     {
  33.                         for (j = 0; j < matrix.GetLength(1); j++)
  34.                         {
  35.                             Console.Write($"Введiть Matrix{i + 1}{j + 1}: ");
  36.                             matrix[i, j] = int.Parse(Console.ReadLine());
  37.                         }
  38.                     }
  39.                     break;
  40.                 }
  41.             default:
  42.                 {
  43.                     Console.WriteLine("Error");
  44.                     Environment.Exit(1);
  45.                     break;
  46.                 }
  47.         }
  48.         Console.WriteLine("\nВведена матриця:");
  49.         for (i = 0; i < matrix.GetLength(0); i++)
  50.         {
  51.             for (j = 0; j < matrix.GetLength(0); j++)
  52.             {
  53.                 Console.Write($"{matrix[i, j]:D2} ");
  54.             }
  55.             Console.WriteLine();
  56.         }
  57.         //finding determinant
  58.         double determinant = CalculateDeterminant(matrix);
  59.         Console.WriteLine($"Визначник матрицi: {determinant}");
  60.     }
  61.     static int CalculateDeterminant(int[,] matrix)
  62.     {
  63.         int det = 0;
  64.         int[,] submatrix = new int[matrix.GetLength(0) - 1, matrix.GetLength(0) - 1];
  65.         if (matrix.GetLength(0) == 2)
  66.         {
  67.             return ((matrix[0, 0] * matrix[1, 1]) - (matrix[1, 0] * matrix[0, 1]));
  68.         }
  69.         else
  70.         {
  71.             for (int x = 0; x < matrix.GetLength(0); x++)
  72.             {
  73.                 int subi = 0;
  74.                 for (int i = 1; i < matrix.GetLength(0); i++)
  75.                 {
  76.                     int subj = 0;
  77.                     for (int j = 0; j < matrix.GetLength(0); j++)
  78.                     {
  79.                         if (j == x)
  80.                         {
  81.                             continue;
  82.                         }
  83.                         submatrix[subi, subj] = matrix[i, j];
  84.                         subj++;
  85.                     }
  86.                     subi++;
  87.                 }
  88.                 det = det + (int)(Math.Pow(-1, x) * matrix[0, x] * CalculateDeterminant(submatrix));
  89.             }
  90.         }
  91.         return det;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement