Advertisement
szubert

Untitled

May 11th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. double** allocateMemory(double **matrix, int n);
  5. double** readFromFile(char* fp, int *n);
  6. double calculateDeterminant(double **matrix, int n);
  7. void freeMemory(double **tempMatrix, int n);
  8.  
  9. int main(int argc, char **argv) {
  10.  
  11.     if(argc < 2) {
  12.         printf("Too few arguments");
  13.         exit(1);
  14.     } else if(argc > 2) {
  15.         printf("Too few arguments");
  16.         exit(1);
  17.     }
  18.  
  19.     int n;
  20.     //int i, j;
  21.     double **matrix = readFromFile(argv[1], &n);
  22.  
  23.     /*for (i = 0; i < n ; ++i)
  24.     {
  25.         for (j = 0; j < n ; ++j)
  26.             printf("%f ", matrix[i * n + j]);
  27.  
  28.         printf("\n");
  29.     }*/
  30.  
  31.     printf("%f \n", calculateDeterminant(matrix, n));
  32.  
  33.     freeMemory(matrix, n);
  34. }
  35.  
  36. double** allocateMemory(double **matrix, int n) {
  37.     matrix = (double**)malloc(n * sizeof(double));
  38.  
  39.     for(int i = 0; i < n; ++i) {
  40.         matrix[i] = (double*)malloc(n * sizeof(double));
  41.  
  42.         if(!matrix[i])
  43.             exit(4);
  44.     }
  45.  
  46.     return matrix;
  47. }
  48.  
  49. double** readFromFile(char* fp, int *n) {
  50.  
  51.     FILE *file = fopen(fp, "r");
  52.  
  53.     if (file == NULL)
  54.     {
  55.         printf("Can't open the file\n");
  56.         exit(1);
  57.     }
  58.  
  59.     fscanf(file, "%d\n", n);
  60.  
  61.     double **matrix;
  62.     matrix = allocateMemory(matrix, *n);
  63.  
  64.     for (int i = 0; i < *n ; ++i) {
  65.         for (int j = 0; j < *n ; ++j)
  66.         {
  67.             if (fscanf(file, "%lf", &matrix[i][j]) < 0) {
  68.                 printf("This isn't square matrix or dimension mismatch :(\n");
  69.                 exit(1);
  70.             }
  71.         }
  72.     }
  73.  
  74.     fclose(file);
  75.  
  76.     return matrix;
  77.  
  78. }
  79.  
  80. double calculateDeterminant(double **matrix, int n) {
  81.     double determinant = 0;
  82.  
  83.     if (n < 1) {
  84.         printf("Error 1: this isn't matrix\n");
  85.         exit(1);
  86.  
  87.     } else if (n == 1) {
  88.         return matrix[0][0];
  89.  
  90.     } else if (n == 2) {
  91.         return (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]);
  92.  
  93.     } else if (n > 2) {
  94.         double **temporaryMatrix;
  95.         allocateMemory(temporaryMatrix, n - 1);
  96.  
  97.         for (int i = 0; i < n; i++) {
  98.             for (int j = 1; j < n; j++) {
  99.                 for (int k = 0; k < n; k++) {
  100.                     if (k == i) {
  101.  
  102.                     } else if ((i == 0) || (k > i)) {
  103.                         temporaryMatrix[j - 1][(k - 1)] = matrix[j][k];
  104.                     } else {
  105.                         temporaryMatrix[j - 1][k] = matrix[j][k];
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             if((i + 1) % 2 == 0){
  111.                 determinant = determinant + (-1 * matrix[0][i] * calculateDeterminant(temporaryMatrix, n -1));
  112.             } else {
  113.                 determinant = determinant + matrix[0][i] * calculateDeterminant(temporaryMatrix, n -1);
  114.             }
  115.         }
  116.         freeMemory(temporaryMatrix, (n-1));
  117.     }
  118.  
  119.     return determinant;
  120. }
  121.  
  122. void freeMemory(double **tempMatrix, int n) {
  123.     for (int i = 0; i < n; ++i) {
  124.         free(tempMatrix[i]);
  125.     }
  126.     free(tempMatrix);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement