Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define ERROR_TEXT "ALLOCATING MEMORY PROBLEMS "
- double **allocate_matrix(int matrix_size);
- void fill_array(double **matrix, int matrix_size);
- void show_array(double **matrix, int matrix_size1);
- double calculate_determinant(double **matrix, double *result, int matrix_size);
- void free_matrix(double **matrix, int matrix_size);
- void free_matrix(double **matrix, int matrix_size){ //free allocate memory
- int current_row;
- for(current_row=0;current_row<matrix_size;current_row++) free(matrix[current_row]);
- free(matrix);
- }
- double **allocate_matrix(int matrix_size){
- int given_row, current_row;
- double **matrix=malloc((matrix_size)*sizeof(double*)); //allocates space for number of rows
- if(matrix==NULL) printf(ERROR_TEXT);
- else{
- for(given_row=0;given_row<matrix_size;given_row++){
- matrix[given_row]=malloc(sizeof(double)*(matrix_size)); //fullfils rows with space for data
- if(matrix[given_row]==NULL){
- printf(ERROR_TEXT);
- for(current_row=0;current_row<given_row;current_row++) //empties space allocated prieviously
- {free(matrix[current_row]);}
- free(matrix); //empites space allocated for number of rows
- }
- }
- }
- return matrix;
- }
- void fill_array(double **matrix, int matrix_size){
- int current_row, current_column;
- printf("\n\n");
- for(current_row=0;current_row<matrix_size;current_row++){
- for(current_column=0;current_column<matrix_size;current_column++){
- scanf("%lf",&matrix[current_row][current_column]);
- }
- }
- }
- void show_array(double **matrix, int matrix_size){
- int current_row, current_column;
- for(current_row=0;current_row<matrix_size;current_row++){
- for(current_column=0;current_column<matrix_size;current_column++){
- printf("%lf \t",matrix[current_row][current_column]);
- }
- printf("\n");
- }
- }
- double calculate_determinant(double **matrix,double *result,int matrix_size){
- double determinant=0;
- double **minor=0;
- int det_column, current_row, current_column;
- if(matrix_size==1){
- *result=matrix[0][0];
- }
- else if(matrix_size == 2){
- *result=matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
- }
- else{
- minor=allocate_matrix(matrix_size-1);
- if(minor==NULL) return-1;
- for(det_column=0;det_column<matrix_size;det_column++){
- for(current_row=1;current_row<matrix_size;current_row++){
- int minor_column=0;
- for(current_column=0;current_column<matrix_size;current_column++){
- if(current_column!=det_column){
- minor[current_row-1][minor_column]=matrix[current_row][current_column];
- minor_column++;
- }
- }
- }
- if(calculate_determinant(minor, &determinant, matrix_size-1)==-1){ //&determinat to adres *result
- free_matrix(minor,matrix_size-1);
- return-1;
- }
- else{
- *result+=matrix[0][det_column]*pow((-1),(2+det_column))*determinant;
- determinant=0;
- }
- }
- free_matrix(minor, matrix_size-1);
- }
- return 1;
- }
- int main(){
- int matrix_size;
- double det=0;
- double **matrix=NULL;
- printf("Please insert size of the matrix");
- scanf("%d", &matrix_size);
- matrix=allocate_matrix(matrix_size);
- if(matrix!=NULL){
- fill_array(matrix,matrix_size);
- show_array(matrix,matrix_size);
- if(calculate_determinant(matrix, &det, matrix_size)){
- printf("\nDeterminant is equal = %lf\n\n", det);
- }
- free_matrix(matrix, matrix_size);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement