Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- double det(double **, int);
- double ** matrix_alloc( int );
- void matrix_free( double** , int);
- int main()
- {
- FILE* plik;
- if ((plik = fopen("mat5.txt", "r")) == NULL)
- printf("blad z otwarciem pliku");
- int N = -1;
- fscanf(plik, "%d\n", &N);
- printf("%d\n", N);
- double** tab = matrix_alloc(N);
- for (int i = 0; i < N; i++){
- for (int j = 0; j < N; j++){
- fscanf(plik, "%lf ", &tab[i][j]);
- }
- fscanf(plik, "\n");
- }
- fclose(plik);
- for (int i = 0; i < N; i++){
- for (int j = 0; j < N; j++){
- printf("%.2lf \t", tab[i][j]);
- }
- printf("\n");
- }
- printf("Wyznacznik wynosi: %f", det((double**)tab, N));
- matrix_free(tab, N);
- getchar();
- return 0;
- }
- double ** matrix_alloc( int N ){
- double **tab = (double**)malloc(sizeof(double*) * N);
- for (int i = 0; i < N; i++)
- tab[i] = (double*)malloc(sizeof(double) * N);
- return tab;
- }
- void matrix_free (double ** tab, int N){
- for (int i = 0; i < N; i++)
- free(tab[i]);
- free(tab);
- }
- double det(double ** matrix, int n)
- {
- double** minor;
- double sum = 0.0;
- double sign = 1.0;
- int i;
- if (n == 1) return matrix[n-1][n-1];
- if (n == 2) return matrix[n - 1 - 1][n - 1 - 1] * matrix[n - 1][n - 1] - matrix[n - 1][n - 1 - 1] * matrix[n - 1 - 1][n - 1];
- minor = matrix_alloc(n-1);
- for (i = 0; i < n; i++)
- {
- for (register int k = 1, mi = 0; k < n; k++, mi++)
- {
- for (register int j = 0, mj = 0; j < n; j++, mj++)
- {
- if (j == i) mj--;
- else minor[mi][mj] = matrix[k][j];
- }
- }
- }
- sum += sign * matrix[0][i] * det(minor,n-1);
- sign = -sign;
- matrix_free(minor,n-1);
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement