Advertisement
Mary_99

determinant

May 7th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define ERROR_TEXT "ALLOCATING MEMORY PROBLEMS "
  6.  
  7. double **allocate_matrix(int matrix_size);
  8. void fill_array(double **matrix, int matrix_size);
  9. void show_array(double **matrix, int matrix_size1);
  10. double calculate_determinant(double **matrix, double *result, int matrix_size);
  11. void free_matrix(double **matrix, int matrix_size);
  12.  
  13. void free_matrix(double **matrix, int matrix_size){     //free allocate memory
  14.   int current_row;
  15.   for(current_row=0;current_row<matrix_size;current_row++) free(matrix[current_row]);
  16.   free(matrix);
  17. }
  18.  
  19. double **allocate_matrix(int matrix_size){
  20.   int given_row, current_row;  
  21.   double **matrix=malloc((matrix_size)*sizeof(double*));    //allocates space for number of rows
  22.    
  23.   if(matrix==NULL) printf(ERROR_TEXT);
  24.   else{
  25.     for(given_row=0;given_row<matrix_size;given_row++){
  26.       matrix[given_row]=malloc(sizeof(double)*(matrix_size));   //fullfils rows with space for data
  27.       if(matrix[given_row]==NULL){
  28.     printf(ERROR_TEXT);
  29.     for(current_row=0;current_row<given_row;current_row++)  //empties space allocated prieviously
  30.       {free(matrix[current_row]);}
  31.       free(matrix);                     //empites space allocated for number of rows
  32.       }
  33.       }
  34.     }
  35.  return matrix;
  36. }
  37.  
  38. void fill_array(double **matrix, int matrix_size){
  39.   int current_row, current_column;
  40.   printf("\n\n");
  41.   for(current_row=0;current_row<matrix_size;current_row++){
  42.     for(current_column=0;current_column<matrix_size;current_column++){
  43.       scanf("%lf",&matrix[current_row][current_column]);
  44.     }
  45.   }
  46. }
  47.  
  48. void show_array(double **matrix, int matrix_size){
  49.   int current_row, current_column;
  50.   for(current_row=0;current_row<matrix_size;current_row++){
  51.     for(current_column=0;current_column<matrix_size;current_column++){
  52.       printf("%lf \t",matrix[current_row][current_column]);
  53.     }
  54.     printf("\n");
  55.   }
  56. }
  57.  
  58. double calculate_determinant(double **matrix,double *result,int matrix_size){
  59.   double determinant=0;
  60.   double **minor=0;
  61.   int det_column, current_row, current_column;
  62.  
  63.     if(matrix_size==1){
  64.       *result=matrix[0][0];
  65.     }
  66.     else if(matrix_size == 2){
  67.       *result=matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];  
  68.     }
  69.     else{
  70.       minor=allocate_matrix(matrix_size-1);
  71.       if(minor==NULL) return-1;
  72.      
  73.       for(det_column=0;det_column<matrix_size;det_column++){
  74.     for(current_row=1;current_row<matrix_size;current_row++){
  75.      
  76.       int minor_column=0;
  77.       for(current_column=0;current_column<matrix_size;current_column++){
  78.         if(current_column!=det_column){
  79.           minor[current_row-1][minor_column]=matrix[current_row][current_column];
  80.           minor_column++;
  81.         }
  82.       }
  83.      
  84.     }
  85.     if(calculate_determinant(minor, &determinant, matrix_size-1)==-1){ //&determinat to adres *result
  86.       free_matrix(minor,matrix_size-1);
  87.       return-1;  
  88.     }
  89.     else{
  90.       *result+=matrix[0][det_column]*pow((-1),(2+det_column))*determinant;
  91.       determinant=0;
  92.     }
  93.       }
  94.       free_matrix(minor, matrix_size-1);
  95.     }
  96.    
  97.   return 1;
  98. }
  99.  
  100. int main(){
  101.   int matrix_size;
  102.   double det=0;
  103.   double **matrix=NULL;
  104.  
  105.   printf("Please insert size of the matrix");
  106.   scanf("%d", &matrix_size);
  107.  
  108.     matrix=allocate_matrix(matrix_size);
  109.       if(matrix!=NULL){
  110.     fill_array(matrix,matrix_size);
  111.     show_array(matrix,matrix_size);
  112.  
  113.     if(calculate_determinant(matrix, &det, matrix_size)){
  114.       printf("\nDeterminant is equal = %lf\n\n", det);
  115.     }
  116.    
  117.     free_matrix(matrix, matrix_size);
  118.       }
  119.      
  120.   return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement