Guest User

Untitled

a guest
Feb 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. #include<Windows.h>
  6. #include <iomanip>
  7. using namespace std;
  8. //алгебраическое дополнение к элементу a[row][col] генерация их
  9. double add(double a[N][M], int row, int col)
  10. {
  11. double b[2][2];
  12. int i, j, bi, bj;
  13.  
  14. for (i = 0, bi = 0; i<3; i++)
  15. {
  16. if (i != row)
  17. {
  18. for (j = 0, bj = 0; j<3; j++)
  19. if (j != col)
  20. {
  21. b[bi][bj] = a[i][j];
  22. bj++;
  23. }
  24. bi++;
  25. }
  26. }
  27.  
  28. if ((row + col) % 2)
  29. return b[0][1] * b[1][0] - b[0][0] * b[1][1];
  30. else
  31. return b[0][0] * b[1][1] - b[0][1] * b[1][0];
  32. }
  33. //определитель матрицы
  34. double det(double a[N][M])
  35. {
  36. int i;
  37. double sum;
  38. for (i = 0, sum = 0.; i<3; i++)
  39. sum += a[i][0] * add(a, i, 0);
  40. if (sum < 0)
  41. cout << "ERROR DETERMINANT NOL'";
  42. return sum;
  43. }
  44. //обратная матрица
  45. void inverse(double a[N][M], double d)
  46. {
  47. double a1[3][3], cur;
  48. int i, j;
  49.  
  50. for (i = 0; i < 3; i++)
  51. for (j = 0; j < 3; j++)
  52. a1[i][j] = add(a, i, j) / d;
  53.  
  54. for (i = 0; i < 3; i++)
  55. for (j = i + 1; j < 3; j++)
  56. {
  57. cur = a1[i][j];
  58. a1[i][j] = a1[j][i];
  59. a1[j][i] = cur;
  60. }
  61. if (d < 0)
  62. cout << "Error n";
  63. else {
  64. cout << "nobratnya matrix :n";
  65. for (i = 0; i < 3; i++)
  66. {
  67. for (j = 0; j < 3; j++)
  68. cout << setw(7) << a1[i][j];
  69. cout << "n";
  70. }
  71. }
  72. }
  73. //основная функция
  74. int main()
  75. {
  76. int N, M; //размерность матрицы
  77. double determinant; // объявление детерминанта(опеределителя)
  78. int i, j;
  79. int ** arr = new int *[N];
  80. cout << "n "; cin >> N;
  81.  
  82. cout << "m "; cin >> M;
  83. for (i = 0; i<N; i++)
  84. int** a = new int *[M];
  85. for (j = 0; j<M; j++)
  86. arr[i][j] = rand() % 10 - 5; //задаем рандом в диапазоне от +10 до -5
  87. cout << "matrix A:n";
  88. for (i = 0; i<3; i++)
  89. {
  90. for (j = 0; j<3; j++)
  91. cout << setw(7) << setprecision(2) << arr[i][j]; //setprecision - сокращение до двух знаков после запятой //setw(7) расстояние между знаками
  92. cout << "n";
  93. }
  94. determinant = det(arr);
  95. cout << "ndeterminant: " << determinant << "n"; //вывод детерминанта
  96. if (determinant) inverse(arr, determinant); //в другом случае
  97. else cout << "obrantoy matrici net";
  98. getchar();
  99. return 0;
  100. }
  101.  
  102. double add(double a[N][M], unsigned row, unsigned col, unsigned N, unsigned M)
  103. {
  104. double b[2][2];
  105. unsigned i, j, bi, bj;
  106.  
  107. for (i = 0, bi = 0; i<N; i++)
  108. {
  109. if (i != row)
  110. {
  111. for (j = 0, bj = 0; j<M; j++)
  112. if (j != col)
  113. {
  114. b[bi][bj] = a[i][j];
  115. bj++;
  116. }
  117. bi++;
  118. }
  119. }
  120.  
  121. if ((row + col) % 2)
  122. return b[0][1] * b[1][0] - b[0][0] * b[1][1];
  123. else
  124. return b[0][0] * b[1][1] - b[0][1] * b[1][0];
  125. }
  126. //определитель матрицы
  127. double det(double a[N][M], unsigned N, unsigned M)
  128. {
  129. int i;
  130. double sum;
  131. for (i = 0, sum = 0.; i<N; i++)
  132. sum += a[i][0] * add(a, i, 0, N, M);
  133. if (sum < 0)
  134. cout << "ERROR DETERMINANT NOL'";
  135. return sum;
  136. }
  137. //обратная матрица
  138. void inverse(double a[N][M], double d, unsigned N, unsigned M)
  139. {
  140. double a1[N][M], cur;
  141. unsigned i, j;
  142.  
  143. for (i = 0; i < N; i++)
  144. for (j = 0; j < M; j++)
  145. a1[i][j] = add(a, i, j, N, M) / d;
  146.  
  147. for (i = 0; i < N; i++)
  148. for (j = i + 1; j < M; j++)
  149. {
  150. cur = a1[i][j];
  151. a1[i][j] = a1[j][i];
  152. a1[j][i] = cur;
  153. }
  154. if (d < 0)
  155. cout << "Error n";
  156. else {
  157. cout << "nobratnya matrix :n";
  158. for (i = 0; i <N; i++)
  159. {
  160. for (j = 0; j < M; j++)
  161. cout << setw(7) << a1[i][j];
  162. cout << "n";
  163. }
  164. }
  165. }
  166. //основная функция
  167. int main()
  168. {
  169. unsigned N, M; //размерность матрицы
  170. double determinant; // объявление детерминанта(опеределителя)
  171. unsigned i, j;
  172. cout << "n "; cin >> N;
  173. cout << "m "; cin >> M;
  174. int ** arr = new int*[N];
  175. for (i = 0; i<N; i++)
  176. arr[i] = new int[M];
  177. for (j = 0; j<M; j++)
  178. arr[i][j] = rand() % 10 - 5; //задаем рандом в диапазоне от +10 до -5
  179. cout << "matrix A:n";
  180. for (i = 0; i<N; i++)
  181. {
  182. for (j = 0; j<M; j++)
  183. cout << setw(7) << setprecision(2) << arr[i][j]; //setprecision - сокращение до двух знаков после запятой //setw(7) расстояние между знаками
  184. cout << "n";
  185. }
  186. determinant = det(arr, N, M);
  187. cout << "ndeterminant: " << determinant << "n"; //вывод детерминанта
  188. if (determinant) inverse(arr, determinant, N, M); //в другом случае
  189. else cout << "obrantoy matrici net";
  190. getchar();
  191. return 0;
  192. }
Add Comment
Please, Sign In to add comment