Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MatrixDeterminant
  8. {
  9. class Program
  10. {
  11. public static int[,] get_matrix(int[,] mas, int size, int row)
  12. {
  13. int[,] arr = new int[size, size];
  14. int iarr = 0;
  15. for (int i = 0; i<(size-1); i++)
  16. {
  17. int jarr = 1;
  18. if (iarr==row)
  19. iarr++;
  20. for (int j = 0; j<(size-1); j++)
  21. {
  22. arr[i, j] = mas[iarr, jarr];
  23. jarr++;
  24. }
  25. iarr++;
  26. }
  27. return (arr);
  28. }
  29.  
  30. public static double determinant(int[,] mas, int size)
  31. {
  32. double det = 0;
  33. if (size == 1)
  34. return mas[0, 0];
  35. for (int i = 0; i < size; i++)
  36. {
  37. det += Math.Pow(-1, (i + 2)) * mas[i, 0] * determinant(get_matrix(mas, size, i), size - 1);
  38. }
  39. return det;
  40. }
  41.  
  42. static void Main(string[] args)
  43. {
  44. int size;
  45. Console.WriteLine("Enter the dimension of the matrix: ");
  46. Random rnd = new Random();
  47. try
  48. {
  49. size = Convert.ToInt32(Console.ReadLine());
  50.  
  51. int[,] arr = new int[size, size];
  52. for (int i = 0; i < size; i++)
  53. {
  54. for (int j = 0; j < size; j++)
  55. {
  56. arr[i, j] = rnd.Next(-5, 6);
  57. }
  58. }
  59.  
  60. for (int i = 0; i < size; i++)
  61. {
  62. for (int j = 0; j < size; j++)
  63. {
  64. Console.Write("{0,6}", arr[i, j]);
  65. }
  66. Console.WriteLine();
  67. Console.WriteLine();
  68. }
  69.  
  70. Console.WriteLine("Matrix determinant is: " + determinant(arr, size));
  71.  
  72. }
  73. catch (FormatException)
  74. {
  75. Console.WriteLine("Invalid format");
  76. }
  77.  
  78. Console.ReadKey();
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement