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 2
- 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 **memory_alloc(double **tab, int size);
- double la_place(double **tab, int size);
- int main(void)
- {
- char size_of_matrix[10];
- char typed_characters[10];
- int size;
- double value;
- int r, c, i;
- double determinant;
- double **tab=NULL;
- puts("Hello. Would you kindly choose size of the square matrix?");
- while(1)
- {
- fgets(size_of_matrix,10,stdin);
- size = atoi(size_of_matrix);
- if(size!=0)
- {
- break;
- }
- else {
- puts("\nBe careful. Try once again!");
- continue;
- }
- }
- printf("Give the values of the elements of the matrix\n");
- tab = memory_alloc(tab,size); /*allocating memory for the array */
- r=0;
- while(r<size)
- {
- c=0;
- while(c<size)
- {
- fgets(typed_characters,10,stdin);
- value = atof(typed_characters);
- if(value!=0)
- {
- tab[r][c] = value;
- c++;
- }
- else {
- puts("\nBe careful. Try once again!");
- continue;
- }
- }
- r++;
- }
- /*printing the elements of the matrix */
- printf("The matrix looks like this:\n\n");
- for(r=0;r<size;r++)
- {
- for(c=0;c<size;c++)
- {
- printf("%f\t", tab[r][c]);
- if(c==(size-1))
- {
- printf("\n");
- }
- }
- }
- determinant=la_place(tab,size);
- if(tab == NULL)
- {
- return 0;
- }
- printf("\n\nThe value of the determinant is:");
- printf("\n\n%.3f\n\n",determinant);
- for (i = 0; i < size; i++)
- {
- free (tab[i]);
- }
- free (tab);
- return 0;
- }
- /*######################## function which allocates the memory ########################*/
- double **memory_alloc(double **tab, int size)
- {
- int counter;
- tab = NULL;
- tab = malloc(size*sizeof(double));
- if (tab == NULL)
- {
- puts("Not enough memory\n");
- free(tab);
- tab=0;
- exit(0);
- }
- for (counter=0;counter<size;counter++)
- {
- tab[counter]=malloc(size*sizeof(double));
- if(tab[counter] == NULL)
- {
- puts("Not enough memory\n");
- while(counter>-1)
- {
- free(tab[counter]);
- counter--;
- }
- free(tab);
- tab=0;
- exit(0);
- }
- }
- return tab;
- }
- double la_place(double **tab, int size)
- {
- double determinant=0; /*initial value of the determinant*/
- double **smaller_tab=NULL; /*second matrix for minors*/
- int row, column, h, i; /*just counters*/
- for(h=0;h<size;h++)
- {
- /*If the matrix has size equal to 1 it's determinant is equal to the value of the single element*/
- if(size == 1)
- {
- determinant += tab[0][0];
- return determinant;
- }
- smaller_tab = memory_alloc(smaller_tab,size-1); /*allocating memory for the minor*/
- if(smaller_tab == NULL)
- {
- for (i = 0; i < size; i++)
- {
- free (smaller_tab[i]);
- }
- free (smaller_tab);
- smaller_tab=0;
- puts("Not enough memory\n");
- exit(0);
- }
- /*creating the minor*/
- for(row=1; row<size; row++)
- {
- for(column=0; column<h; column++)
- {
- smaller_tab[row-1][column]=tab[row][column];
- }
- for(column=(h+1); column<size; column++)
- {
- smaller_tab[row-1][column-1]=tab[row][column];
- }
- }
- determinant +=-1*pow((double)-1,(double)(1+h))*tab[0][h]*la_place(smaller_tab,size-1); /*calculating determinant*/
- /*free_arr(smaller_tab,size-1); freeing used minor*/
- for (i = 0; i < size-1; i++)
- {
- free (smaller_tab[i]);
- }
- free (smaller_tab);
- }
- return determinant;/*returning the value of the determinant*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement