Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. /*#define FINISH 2
  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 **memory_alloc(double **tab, int size);
  28. double la_place(double **tab, int size);
  29.  
  30.  
  31. int main(void)
  32. {
  33. char size_of_matrix[10];
  34. char typed_characters[10];
  35. int size;
  36. double value;
  37. int r, c, i;
  38. double determinant;
  39. double **tab=NULL;
  40.  
  41.  
  42. puts("Hello. Would you kindly choose size of the square matrix?");
  43.  
  44.  
  45. while(1)
  46. {
  47. fgets(size_of_matrix,10,stdin);
  48. size = atoi(size_of_matrix);
  49. if(size!=0)
  50. {
  51. break;
  52. }
  53. else {
  54. puts("\nBe careful. Try once again!");
  55. continue;
  56. }
  57. }
  58.  
  59.  
  60. printf("Give the values of the elements of the matrix\n");
  61.  
  62.  
  63. tab = memory_alloc(tab,size); /*allocating memory for the array */
  64.  
  65. r=0;
  66. while(r<size)
  67. {
  68. c=0;
  69. while(c<size)
  70. {
  71. fgets(typed_characters,10,stdin);
  72. value = atof(typed_characters);
  73. if(value!=0)
  74. {
  75. tab[r][c] = value;
  76. c++;
  77. }
  78. else {
  79. puts("\nBe careful. Try once again!");
  80. continue;
  81. }
  82. }
  83. r++;
  84. }
  85.  
  86.  
  87. /*printing the elements of the matrix */
  88. printf("The matrix looks like this:\n\n");
  89. for(r=0;r<size;r++)
  90. {
  91. for(c=0;c<size;c++)
  92. {
  93. printf("%f\t", tab[r][c]);
  94. if(c==(size-1))
  95. {
  96. printf("\n");
  97. }
  98. }
  99. }
  100.  
  101. determinant=la_place(tab,size);
  102. if(tab == NULL)
  103. {
  104. return 0;
  105. }
  106.  
  107. printf("\n\nThe value of the determinant is:");
  108. printf("\n\n%.3f\n\n",determinant);
  109.  
  110. for (i = 0; i < size; i++)
  111. {
  112. free (tab[i]);
  113. }
  114. free (tab);
  115.  
  116.  
  117. return 0;
  118. }
  119.  
  120.  
  121. /*######################## function which allocates the memory ########################*/
  122. double **memory_alloc(double **tab, int size)
  123. {
  124. int counter;
  125.  
  126. tab = NULL;
  127. tab = malloc(size*sizeof(double));
  128.  
  129. if (tab == NULL)
  130. {
  131. puts("Not enough memory\n");
  132. free(tab);
  133. tab=0;
  134. exit(0);
  135. }
  136.  
  137. for (counter=0;counter<size;counter++)
  138. {
  139. tab[counter]=malloc(size*sizeof(double));
  140. if(tab[counter] == NULL)
  141. {
  142. puts("Not enough memory\n");
  143. while(counter>-1)
  144. {
  145. free(tab[counter]);
  146. counter--;
  147. }
  148. free(tab);
  149. tab=0;
  150. exit(0);
  151. }
  152. }
  153. return tab;
  154. }
  155.  
  156.  
  157. double la_place(double **tab, int size)
  158. {
  159. double determinant=0; /*initial value of the determinant*/
  160. double **smaller_tab=NULL; /*second matrix for minors*/
  161. int row, column, h, i; /*just counters*/
  162.  
  163. for(h=0;h<size;h++)
  164. {
  165. /*If the matrix has size equal to 1 it's determinant is equal to the value of the single element*/
  166. if(size == 1)
  167. {
  168. determinant += tab[0][0];
  169. return determinant;
  170. }
  171.  
  172.  
  173. smaller_tab = memory_alloc(smaller_tab,size-1); /*allocating memory for the minor*/
  174. if(smaller_tab == NULL)
  175. {
  176. for (i = 0; i < size; i++)
  177. {
  178. free (smaller_tab[i]);
  179. }
  180. free (smaller_tab);
  181. smaller_tab=0;
  182. puts("Not enough memory\n");
  183. exit(0);
  184. }
  185.  
  186. /*creating the minor*/
  187. for(row=1; row<size; row++)
  188. {
  189. for(column=0; column<h; column++)
  190. {
  191. smaller_tab[row-1][column]=tab[row][column];
  192. }
  193.  
  194. for(column=(h+1); column<size; column++)
  195. {
  196. smaller_tab[row-1][column-1]=tab[row][column];
  197. }
  198.  
  199. }
  200.  
  201. determinant +=-1*pow((double)-1,(double)(1+h))*tab[0][h]*la_place(smaller_tab,size-1); /*calculating determinant*/
  202.  
  203.  
  204. /*free_arr(smaller_tab,size-1); freeing used minor*/
  205. for (i = 0; i < size-1; i++)
  206. {
  207. free (smaller_tab[i]);
  208. }
  209. free (smaller_tab);
  210. }
  211.  
  212.  
  213. return determinant;/*returning the value of the determinant*/
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement