Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <math.h>
- using namespace std;
- //funkcja wypelnia macierz liczbami wpisanymi z klawiatury
- void wypelnij(float a[100][100], int n) {
- int i, j;
- cout << endl;
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++) {
- cout << "Podaj element o indeksie " << i + 1 << "x" << j + 1 << " : ";
- cin >> a[i][j];
- }
- cout << endl;
- }
- //funkcja wyswietla podane macierze
- void wyswietl(float a[100][100], int n, int show) {
- int i, j;
- if (show == 1)
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++)
- cout << " " << a[i][j] << " ";
- cout << endl;
- }
- else if (show == 2) {
- cout << "Macierz odwrotna : " << endl;
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++)
- cout << " " << a[i][j] << " ";
- cout << endl;
- }
- }
- }
- //funkcja tworzy macierz minorow
- void minor(float b[100][100], float a[100][100], int i, int n)
- {
- int j, l, h = 0, k = 0;
- for (l = 1; l < n; l++)
- for (j = 0; j < n; j++)
- {
- if (j == i)
- continue;
- b[h][k] = a[l][j];
- k++;
- if (k == (n - 1))
- {
- h++;
- k = 0;
- }
- }
- }
- //funkcja liczy wyznacznik macierzy
- float det(float a[100][100], int n)
- {
- int i;
- float b[100][100], sum = 0;
- if (n == 1)
- return a[0][0];
- else if (n == 2)
- return (a[0][0] * a[1][1] - a[0][1] * a[1][0]);
- else
- for (i = 0; i < n; i++)
- {
- minor(b, a, i, n);
- sum = (float)(sum + a[0][i] * pow(-1, i)*det(b, (n - 1)));
- }
- return sum;
- }
- //funkcja zajmuje sie transponowaniem macierzy
- void transponuj(float c[100][100], float d[100][100], int n, float det)
- {
- int i, j;
- float b[100][100];
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++)
- b[i][j] = c[j][i];
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++)
- d[i][j] = b[i][j] / det;
- }
- //funkcja tworzy macierz dopelnien algebraicznych
- void macierz_dopelnien(float a[100][100], float d[100][100], int n, float determinte)
- {
- float b[100][100], c[100][100];
- int l, h, m, k, i, j;
- for (h = 0; h < n; h++)
- for (l = 0; l < n; l++)
- {
- m = 0;
- k = 0;
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++)
- if (i != h && j != l)
- {
- b[m][k] = a[i][j];
- if (k < (n - 2))
- k++;
- else
- {
- k = 0;
- m++;
- }
- }
- c[h][l] = (float)pow(-1, (h + l))*det(b, (n - 1));
- }
- transponuj(c, d, n, determinte);
- }
- void macierz_odwrotna(float a[100][100], float d[100][100], int n, float det)
- {
- if (det == 0)
- cout << "Odwrocenie podanej macierzy nie jest mozliwe!" << endl;
- else if (n == 1)
- d[0][0] = 1/a[0][0];
- else
- macierz_dopelnien(a, d, n, det);
- }
- int main()
- {
- int i, j, n;
- float a[100][100], d[100][100], deter;
- cout << "Podaj stopien macierzy : ";
- cin >> n;
- wypelnij(a, n);
- int print_matrix = 1;
- wyswietl(a, n, print_matrix);
- deter = (float)det(a, n);
- cout << "Wyznacznik macierzy : " << deter<<endl;
- macierz_odwrotna(a, d, n, deter);
- int print_inverse = 2;
- wyswietl(d, n, print_inverse);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement