Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. /**     @file determinant.c
  2. *   @author Michal Szydlowski, TCS2, 163880
  3. *   @date 13.05.2011
  4. *   @brief Program calculates the value of the determinant for a user-defined matrix  */
  5.  
  6. #include <stdlib.h>  
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. #define FINISH 2
  11.  
  12. /**function for proper reactions in case of occurence of an error in normal execution flow */
  13. /*void *my_malloc(size_t size)
  14. {
  15.     static int finish = 0;
  16.     if (finish++ == FINISH)
  17.         return NULL;
  18.     return malloc(size);
  19. }
  20. #define malloc my_malloc*/
  21.  
  22.  
  23. /** Function performs operations necessary to calculate the determinant, using the recursive algorithm
  24. @param **matrix - content of the matrix - variable, which adress is stored by a pointer, of adress stored by a pointer to the allocated memory
  25. @param size - size of the analyzed matrix
  26. @return det - calculated determinant
  27. */
  28. double determinant(double **matrix, float size)
  29. {
  30.     int y,x,main_col_counter,x_sub;
  31.     double det = 0;
  32.     double **submatrix = NULL;              /* initializing the pointer as a NULL value, so that malloc has the uninitialized memory to point */
  33.    
  34.     if (size==1)
  35.     {
  36.             det=matrix[0][0];               /* if the matrix consists of one element, this element is the determinant */
  37.     }
  38.     else if(size==2)    
  39.     {
  40.         det = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];    /* algorithm for 2x2 matrix */
  41.     }  
  42.     else
  43.     {
  44.             det = 0;
  45.         /* loop calculating the subsequent elements of the sum */
  46.             for (main_col_counter=1;main_col_counter<=size;main_col_counter++)
  47.         {
  48.                 submatrix = malloc((size-1)*sizeof(double));            /* allocating the memory dynamically */
  49.             if(submatrix==NULL)
  50.             {
  51.                 for(y=0;y<size;y++)
  52.                 {
  53.                     free(submatrix[y]);
  54.                 }
  55.                 free(submatrix);       
  56.                 printf("Memory could not be allocated\n");      /*error handling */
  57.                 return 0;
  58.             }
  59.                 for (y=0;y<size-1;y++)
  60.             {
  61.                         submatrix[y] = malloc((size-1)*sizeof(double));     /* setting the proper memory for each matrix row*/
  62.             }
  63.             /* loop copying the appropriate contents of the main matrix to the submatrices*/
  64.                 for (y=1;y<size;y++)
  65.             {
  66.                 /* loop copying the content to current submatrix */
  67.                         x_sub = 0;
  68.                     for (x=0;x<size;x++)
  69.                 {
  70.                             if (x == main_col_counter-1)            /* the column with index equal to sum index is excluded (as well as the first row) */
  71.                             continue;
  72.                             submatrix[y-1][x_sub] = matrix[y][x];
  73.                             x_sub++;
  74.                     }
  75.                 }
  76.             /* adding the new sum element to the determinant */    
  77.                 det = det + pow(-1.0,main_col_counter+1.0) * matrix[0][main_col_counter-1] * determinant(submatrix,size-1);
  78.             /* freeing the memory */
  79.                 for (y=0;y<size-1;y++)
  80.             {
  81.                         free(submatrix[y]);
  82.             }        
  83.             free(submatrix);
  84.             }
  85.     }
  86.     return det;
  87. }
  88. /*###################################################################################################################*/
  89. int main()
  90. {
  91.     int y,x;
  92.     float size;
  93.     double **matrix;       
  94.     printf ("Enter matrix size: ");
  95.     if(scanf("%f", &size)!=1)           /* checking if the input is a number */
  96.     {
  97.         printf("Invalid characters entered \n");
  98.         return 0;
  99.     }
  100.     else if(size<0 || (size - floor(size))!=0)  /* checking if the number entered is positive and integer */
  101.     {
  102.         printf("Size must be a positive integer\n");
  103.         return 0;
  104.     }
  105.  
  106.     matrix = malloc(size*sizeof(double));           /* allocating the memory dynamically */
  107.     if(matrix==NULL)
  108.     {
  109.         for(y=0;y<size;y++)
  110.         {
  111.             free(matrix[y]);
  112.         }
  113.         free(matrix);      
  114.         printf("Memory could not be allocated\n");      /*error handling */
  115.         return 0;
  116.     }
  117.     for(y=0;y<size;y++)
  118.     {
  119.         matrix[y]=malloc(size * sizeof(double));    /* setting the proper memory for each matrix row*/
  120.     }      
  121.     for(y=0;y<size;y++)
  122.     {
  123.         for(x=0;x<size;x++)
  124.         {
  125.             printf("Please enter the element of the %i row, %i column: ",y+1,x+1);     
  126.             if (scanf("%lf",&matrix[y][x])!=1)          /* if the entered character is not a number, program does not accept it end terminates */
  127.             {
  128.                 printf("Invalid value \n");    
  129.                 return 0;
  130.             }          
  131.         }      
  132.     }  
  133.     /* presenting the result */
  134.     printf("The determinant of the matrix is equal %lf \n",determinant(matrix,size));
  135.     for(y=0;y<size;y++)
  136.     {
  137.         free(matrix[y]);
  138.     }
  139.     free(matrix);  
  140.     getchar();     
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement