Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define FINISH 99
  6.  
  7. void *my_malloc(size_t size)
  8. {
  9. static int finish = 0;
  10. if (finish++ == FINISH)
  11.  return NULL;
  12. return malloc(size);
  13. }
  14. #define malloc my_malloc
  15.  
  16.  
  17. void *my_calloc(size_t count, size_t size)
  18. {
  19. static int finish = 0;
  20. if (finish++ == FINISH)
  21.  return NULL;
  22. return calloc(count, size);
  23. }
  24. #define calloc my_calloc
  25.  
  26.  
  27. double **alloc(double **tab, int size)
  28. {
  29.     int row1=0,row2=0;
  30.     tab = NULL;
  31.     tab = malloc( size*sizeof(double *));   /*allocating the memory for the first dimension*/
  32.     if (tab == NULL)
  33.     {
  34.         printf("Not enough memory\n");
  35.         free(tab);
  36.         tab=0;
  37.         return 0;
  38.     }
  39.     for (row1=0;row1<size;row1++)
  40.     {
  41.       tab[row1]=malloc(size * sizeof(double));  /*allocating the memory fot the second dimension*/
  42.       if(tab[row1] == NULL)
  43.       {
  44.         printf("Not enough memory\n");
  45.         for (row2=0;row2<=row1;row2++)
  46.             free(tab[row2]);
  47.         free(tab);
  48.         tab=0;
  49.         return 0;
  50.       }
  51.     }
  52.     return tab;
  53. }
  54. double det(double **matrix, int size)
  55. {
  56.     double **minor=NULL;
  57.     double result=0;
  58.    
  59.     int row,col,chosencol;
  60.    
  61.     if(size==1)
  62.     {
  63.         result+=matrix[0][0];
  64.         return result;
  65.     }
  66.  
  67.     else
  68.     {
  69.         for(chosencol=0;chosencol<size;chosencol++)
  70.         {
  71.             minor =alloc(minor,size-1);
  72.             if(minor==NULL)
  73.             {
  74.                 free(minor);
  75.                 return 0;
  76.             }
  77.  
  78.        
  79.             for(row=1;row<size;row++)
  80.             {
  81.                 for(col=0;col<chosencol;col++)
  82.                     minor[row-1][col]=matrix[row][col];
  83.                 for(col=chosencol+1;col<size;col++)
  84.                     minor[row-1][col-1]=matrix[row][col];
  85.             }
  86.             result+=pow((int)-1,(int)(2+chosencol))*  matrix[0][chosencol]* det(minor,size-1);
  87.            
  88.             for(row=0;row<size-1;row++)
  89.                 free(minor[row]);
  90.             free(minor);
  91.         }
  92.     }
  93.     return result;
  94. }
  95.  
  96. int main()
  97. {
  98.     int row,col;
  99.     int size=0;
  100.     char temp[10];
  101.     double **matrix;
  102.    
  103.    
  104.     while(size==0)
  105.     {
  106.         printf("\nPlease enter the size of square matrix,\nfor which determinant is to be calculated: ");
  107.         fgets (temp,10, stdin);
  108.         size=(int)strtod(temp,NULL);
  109.     }
  110.    
  111.     matrix=alloc(matrix,size);
  112.    
  113.     printf("\nPlease give the values of %d-dimnesioned matrix:\n",size);
  114.     for(row=0; row<size; row++)
  115.         for(col=0; col<size; col++)
  116.         {
  117.            
  118.             {
  119.                 printf("the %d. %d. element: ",row+1,col+1);
  120.                 fgets (temp,10, stdin);
  121.                 matrix[row][col]=(double)strtod(temp,NULL);
  122.             }
  123.         }
  124.    
  125.     printf("\nYour matrix:\n");
  126.     for(row=0;row<size;row++)                
  127.     {
  128.         for(col=0;col<size;col++)
  129.             printf("%g\t",*(*(matrix+row)+col));
  130.         printf("\n");
  131.     }
  132.    
  133.     printf("\nDeterminant: %g\n", det(matrix,size));
  134.     for(row=0;row<size;row++)
  135.         free(matrix[row]);
  136.     free(matrix);
  137.     getchar();
  138.     return(0);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement