SHARE
TWEET

MAtrix

Mary_99 May 14th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <math.h>
  5.  
  6.  
  7. void openFile(FILE **fp, char **filename);
  8. int readSize(FILE **fp);
  9. void allocateMemory(int size, double ***matrix);
  10. void readValues(int size, double **matrix, FILE *fp);
  11. void printMatrix(int size, double **matrix);
  12. double determineDeterminant(double **matrix, int size);
  13. double determineDeterminant2x2(double **matrix);
  14. void nextDet(int size, double **detmatrix, double **matrix, int column);
  15. void freeMemory(int size, double ***matrix);
  16.  
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20.     FILE *fp;
  21.     char* filename = argv[1];
  22.     int size;
  23.     double **matrix;
  24.     openFile(&fp, &filename);
  25.     size = readSize(&fp);
  26.     allocateMemory(size, &matrix);
  27.     readValues(size, matrix, fp);
  28.     fclose(fp);
  29.     printMatrix(size, matrix);
  30.     double det = determineDeterminant(matrix, size);
  31.     printf("\n\n Determinant = %f\n\n\n", det);
  32.     freeMemory(size, &matrix);
  33.     return 0;
  34. }
  35.  
  36. void openFile(FILE **fp, char **filename)
  37. {
  38.     assert(fp);
  39.     assert(filename);
  40.     *fp = fopen(*filename, "r");
  41.     if (*fp == NULL)
  42.     {
  43.         printf(" File can not be open! %s", *filename);
  44.         exit(0);
  45.     }
  46. }
  47.  
  48.  
  49. int readSize(FILE **fp)
  50. {
  51.     assert(fp);
  52.     int size;
  53.     int correct = fscanf(*fp, "%d", &size);
  54.     if(!correct || size <= 0){
  55.         printf("Incorret size \n");
  56.         exit(0);
  57.     }
  58.     return size;
  59. }
  60.  
  61.  
  62. void allocateMemory(int size, double ***matrix)
  63. {
  64.     assert(matrix);
  65.     *matrix = (double**)malloc(sizeof(double*) * size);
  66.     if(!(*matrix))
  67.     {
  68.         abort();
  69.     }
  70.     int i;
  71.     for(i=0; i<size; i++)
  72.     {
  73.         (*matrix)[i] = (double*)malloc(sizeof(double)*size);
  74.         if(!(*matrix)[i])
  75.         {
  76.             abort();
  77.         }
  78.     }
  79. }
  80.  
  81. void readValues(int size, double **matrix, FILE *fp)
  82. {
  83.     assert(matrix);
  84.     assert(fp);
  85.     for(int i = 0; i < size; i++)
  86.     {
  87.         for(int j = 0; j < size; j++)
  88.         {
  89.             if(!fscanf(fp, "%lf", &matrix[i][j]))
  90.             {
  91.                 printf("Wrong Matrix!!\n");
  92.                 freeMemory(size, &matrix);
  93.                 exit(0);
  94.             }
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. void printMatrix(int size, double **matrix)
  101. {
  102.     assert(matrix);
  103.     printf("%d\n", size);
  104.     for(int i = 0; i < size; i++)
  105.     {
  106.         for(int j = 0; j < size; j++)
  107.         {
  108.             printf(" %f  ", matrix[i][j]);
  109.         }
  110.         printf("\n");
  111.     }
  112. }
  113.  
  114.  
  115. double determineDeterminant(double **matrix, int size)
  116. {
  117.     assert(matrix);
  118.     if(size == 1)
  119.     {
  120.         return matrix[0][0];
  121.     }
  122.     if(size == 2)
  123.     {
  124.         return determineDeterminant2x2(matrix);
  125.     }
  126.     int sign = 1;
  127.     double determinant = 0;
  128.     double **detmatrix;
  129.     for(int column = 0; column < size; column++)
  130.     {
  131.         allocateMemory(size - 1, &detmatrix);
  132.         nextDet(size, detmatrix, matrix, column);
  133.         determinant = sign * matrix[0][column] * determineDeterminant(detmatrix, size - 1) + determinant;
  134.         sign *= -1;
  135.         freeMemory(size - 1, &detmatrix);
  136.     }
  137.     return determinant;
  138. }
  139.  
  140.  
  141. double determineDeterminant2x2(double **matrix)
  142. {
  143.     assert(matrix);
  144.     double det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  145.     return det;
  146. }
  147.  
  148.  
  149. void nextDet(int size, double **detmatrix, double **matrix, int column)
  150. {
  151.     assert(detmatrix);
  152.     assert(matrix);
  153.     int biggermatrixcolumn = 0, smallermatrixcolumn = 0;
  154.     while(biggermatrixcolumn < size){
  155.         if(biggermatrixcolumn != column)
  156.         {
  157.             for(int i=1; i<size; i++){
  158.                 detmatrix[i-1][smallermatrixcolumn] = matrix[i][biggermatrixcolumn];
  159.             }
  160.             smallermatrixcolumn++;
  161.         }
  162.         biggermatrixcolumn++;
  163.     }
  164. }
  165.  
  166.  
  167. void freeMemory(int size, double ***matrix)
  168. {
  169.     assert(matrix);
  170.     for(int i = 0; i<size; i++){
  171.         free((*matrix)[i]);
  172.     }
  173.     free(*matrix);
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top