Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define FINISH 99
- void *my_malloc(size_t size)
- {
- static int finish = 0;
- if (finish++ == FINISH)
- return NULL;
- return malloc(size);
- }
- #define malloc my_malloc
- void *my_calloc(size_t count, size_t size)
- {
- static int finish = 0;
- if (finish++ == FINISH)
- return NULL;
- return calloc(count, size);
- }
- #define calloc my_calloc
- double **alloc(double **tab, int size)
- {
- int row1=0,row2=0;
- tab = NULL;
- tab = malloc( size*sizeof(double *)); /*allocating the memory for the first dimension*/
- if (tab == NULL)
- {
- printf("Not enough memory\n");
- free(tab);
- tab=0;
- return 0;
- }
- for (row1=0;row1<size;row1++)
- {
- tab[row1]=malloc(size * sizeof(double)); /*allocating the memory fot the second dimension*/
- if(tab[row1] == NULL)
- {
- printf("Not enough memory\n");
- for (row2=0;row2<=row1;row2++)
- free(tab[row2]);
- free(tab);
- tab=0;
- return 0;
- }
- }
- return tab;
- }
- double det(double **matrix, int size)
- {
- double **minor=NULL;
- double result=0;
- int row,col,chosencol;
- if(size==1)
- {
- result+=matrix[0][0];
- return result;
- }
- else
- {
- for(chosencol=0;chosencol<size;chosencol++)
- {
- minor =alloc(minor,size-1);
- if(minor==NULL)
- {
- free(minor);
- return 0;
- }
- for(row=1;row<size;row++)
- {
- for(col=0;col<chosencol;col++)
- minor[row-1][col]=matrix[row][col];
- for(col=chosencol+1;col<size;col++)
- minor[row-1][col-1]=matrix[row][col];
- }
- result+=pow((int)-1,(int)(2+chosencol))* matrix[0][chosencol]* det(minor,size-1);
- for(row=0;row<size-1;row++)
- free(minor[row]);
- free(minor);
- }
- }
- return result;
- }
- int main()
- {
- int row,col;
- int size=0;
- char temp[10];
- double **matrix;
- while(size==0)
- {
- printf("\nPlease enter the size of square matrix,\nfor which determinant is to be calculated: ");
- fgets (temp,10, stdin);
- size=(int)strtod(temp,NULL);
- }
- matrix=alloc(matrix,size);
- printf("\nPlease give the values of %d-dimnesioned matrix:\n",size);
- for(row=0; row<size; row++)
- for(col=0; col<size; col++)
- {
- {
- printf("the %d. %d. element: ",row+1,col+1);
- fgets (temp,10, stdin);
- matrix[row][col]=(double)strtod(temp,NULL);
- }
- }
- printf("\nYour matrix:\n");
- for(row=0;row<size;row++)
- {
- for(col=0;col<size;col++)
- printf("%g\t",*(*(matrix+row)+col));
- printf("\n");
- }
- printf("\nDeterminant: %g\n", det(matrix,size));
- for(row=0;row<size;row++)
- free(matrix[row]);
- free(matrix);
- getchar();
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement