Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. //funkcja wypelnia macierz liczbami wpisanymi z klawiatury
  8. void wypelnij(float a[100][100], int n) {
  9.     int i, j;
  10.     cout << endl;
  11.     for (i = 0; i < n; i++)
  12.         for (j = 0; j < n; j++) {
  13.             cout << "Podaj element o indeksie " << i + 1 << "x" << j + 1 << " : ";
  14.             cin >> a[i][j];
  15.         }
  16.     cout << endl;
  17. }
  18.  
  19. //funkcja wyswietla podane macierze
  20. void wyswietl(float a[100][100], int n, int show) {
  21.     int i, j;
  22.     if (show == 1)
  23.         for (i = 0; i < n; i++) {
  24.             for (j = 0; j < n; j++)
  25.                 cout << " " << a[i][j] << " ";
  26.             cout << endl;
  27.         }
  28.     else if (show == 2) {
  29.         cout << "Macierz odwrotna : " << endl;
  30.         for (i = 0; i < n; i++) {
  31.             for (j = 0; j < n; j++)
  32.                 cout << " " << a[i][j] << " ";
  33.             cout << endl;
  34.         }
  35.     }
  36. }
  37.  
  38.  
  39. //funkcja tworzy macierz minorow
  40. void minor(float b[100][100], float a[100][100], int i, int n)
  41. {
  42.     int j, l, h = 0, k = 0;
  43.     for (l = 1; l < n; l++)
  44.         for (j = 0; j < n; j++)
  45.         {
  46.             if (j == i)
  47.                 continue;
  48.             b[h][k] = a[l][j];
  49.             k++;
  50.             if (k == (n - 1))
  51.             {
  52.                 h++;
  53.                 k = 0;
  54.             }
  55.         }
  56. }
  57.  
  58. //funkcja liczy wyznacznik macierzy
  59. float det(float a[100][100], int n)
  60. {
  61.     int i;
  62.     float b[100][100], sum = 0;
  63.     if (n == 1)
  64.         return a[0][0];
  65.     else if (n == 2)
  66.         return (a[0][0] * a[1][1] - a[0][1] * a[1][0]);
  67.     else
  68.         for (i = 0; i < n; i++)
  69.         {
  70.             minor(b, a, i, n); 
  71.             sum = (float)(sum + a[0][i] * pow(-1, i)*det(b, (n - 1))); 
  72.         }
  73.     return sum;
  74. }
  75.  
  76. //funkcja zajmuje sie transponowaniem macierzy
  77. void transponuj(float c[100][100], float d[100][100], int n, float det)
  78. {
  79.     int i, j;
  80.     float b[100][100];
  81.     for (i = 0; i < n; i++)
  82.         for (j = 0; j < n; j++)
  83.             b[i][j] = c[j][i];
  84.     for (i = 0; i < n; i++)
  85.         for (j = 0; j < n; j++)
  86.             d[i][j] = b[i][j] / det;   
  87. }
  88.  
  89. //funkcja tworzy macierz dopelnien algebraicznych
  90. void macierz_dopelnien(float a[100][100], float d[100][100], int n, float determinte)
  91. {
  92.     float b[100][100], c[100][100];
  93.     int l, h, m, k, i, j;
  94.     for (h = 0; h < n; h++)
  95.         for (l = 0; l < n; l++)
  96.         {
  97.             m = 0;
  98.             k = 0;
  99.             for (i = 0; i < n; i++)
  100.                 for (j = 0; j < n; j++)
  101.                     if (i != h && j != l)
  102.                     {
  103.                         b[m][k] = a[i][j];
  104.                         if (k < (n - 2))
  105.                             k++;
  106.                         else
  107.                         {
  108.                             k = 0;
  109.                             m++;
  110.                         }
  111.                     }
  112.             c[h][l] = (float)pow(-1, (h + l))*det(b, (n - 1)); 
  113.         }
  114.     transponuj(c, d, n, determinte);   
  115. }
  116.  
  117.  
  118. void macierz_odwrotna(float a[100][100], float d[100][100], int n, float det)
  119. {
  120.     if (det == 0)
  121.         cout << "Odwrocenie podanej macierzy nie jest mozliwe!" << endl;
  122.     else if (n == 1)
  123.         d[0][0] = 1/a[0][0];
  124.     else
  125.         macierz_dopelnien(a, d, n, det);
  126. }
  127.  
  128. int main()
  129. {
  130.     int i, j, n;
  131.     float a[100][100], d[100][100], deter;
  132.     cout << "Podaj stopien macierzy : ";
  133.     cin >> n;
  134.     wypelnij(a, n);
  135.     int print_matrix = 1;
  136.     wyswietl(a, n, print_matrix);  
  137.     deter = (float)det(a, n);  
  138.     cout << "Wyznacznik macierzy : " << deter<<endl;
  139.     macierz_odwrotna(a, d, n, deter);  
  140.     int print_inverse = 2;
  141.     wyswietl(d, n, print_inverse); 
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement