Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdio_ext.h>
  5.  
  6. double determinant(double **array_one, int matrixSize); /*Recursive determinant function*/
  7. double **allocMemory(int matrixSize); /*Function that allocates memory*/
  8. void readkeys(double **array_one, int matrixSize); /*Read numbers into matrix from the keyboard*/
  9. int readMatrixSize(); /*Reads matrix size*/
  10. int main()
  11. {
  12. int matrixSize;
  13. int vertical, horizontal; /*Vertical and horizontal location in dynamic allocated array*/
  14. double **array_one=NULL;/**/
  15. matrixSize=readMatrixSize();
  16. array_one=allocMemory(matrixSize); /*Returning pointer from function*/
  17. readkeys(array_one, matrixSize);
  18. for(vertical=0; vertical<matrixSize; vertical++) /*Showing filled array, for test purposes*/
  19. {
  20. printf("\n");
  21. for(horizontal=0; horizontal<matrixSize; horizontal++)
  22. printf("%-5f ", array_one[vertical][horizontal]);
  23. }
  24. printf("\n\nDeterminant=%f\n", determinant(array_one, matrixSize)); /*Calculation of determinant*/
  25. for(vertical=0; vertical<matrixSize; vertical++)
  26. {
  27. free(array_one[vertical]); /*Freeing allocated memory of second dimension*/
  28. }
  29. free(array_one); /*Freeing first dimension*/
  30.  
  31. return 0;
  32. }
  33.  
  34. void readkeys(double **array_one, int matrixSize)
  35. {
  36. int trash=0;
  37. int check=0; /*If one you've inputed good valude */
  38. int vertical;
  39. int horizontal;
  40. for(vertical=0; vertical<matrixSize; vertical++)
  41. for(horizontal=0; horizontal<matrixSize; horizontal++)
  42. {
  43. check=0;
  44. printf("Number: ");
  45. while(check==0 || trash!='\n')
  46. {
  47. check=scanf("%lf", &array_one[vertical][horizontal]);
  48. trash=getchar();
  49. if(check==0 ||trash!='\n' )
  50. {
  51. printf("Wrong number!!!!!\n");
  52. }
  53. __fpurge(stdin);
  54. }
  55. }
  56.  
  57. }
  58.  
  59. int readMatrixSize()
  60. {
  61. int trash=0;
  62. int matrixSize;
  63. int check=0; /*If one you've inputed good valude */
  64. while(check==0 || matrixSize<1 ||trash!='\n') /*Checking correctness of input value*/
  65. {
  66. check=scanf("%d", &matrixSize);
  67. trash=getchar();
  68. if(check==0 || matrixSize<1 ||trash!='\n' )
  69. {
  70. printf("Wrong number!!!!!\n");
  71. }
  72. __fpurge(stdin);
  73. }
  74. printf("Size of matrix: %d\n", matrixSize);
  75. return matrixSize;
  76.  
  77. }
  78.  
  79. double **allocMemory(int matrixSize)
  80. {
  81. int vertical;
  82. double **new_one=NULL;
  83. new_one = malloc(matrixSize*sizeof(double *));
  84. if (new_one == 0)
  85. {
  86. printf("ERROR: Out of memory\n");
  87. free(new_one);
  88. exit(1);
  89. }
  90. for(vertical=0; vertical<matrixSize; vertical++)
  91. {
  92. new_one[vertical] = malloc(matrixSize*sizeof(double));
  93. if (new_one[vertical] == 0)
  94. {
  95. printf("ERROR: Out of memory another dimension\n");
  96. while(vertical>0)
  97. {
  98. vertical--;
  99. free(new_one[vertical]);
  100. }
  101. free(new_one);
  102. exit(1);
  103. }
  104. }
  105. return new_one;
  106. }
  107.  
  108. double determinant(double **array_one, int matrixSize)
  109. {
  110. double determinantV=0;
  111. int multiplier;
  112. int vertical, horizontal;
  113. int newVertical, newHorizontal;
  114. double **minor_one=NULL;
  115. if (matrixSize==1)
  116. return array_one[0][0];
  117. if (matrixSize==2)
  118. return array_one[0][0]*array_one[1][1]-array_one[0][1]*array_one[1][0]; /*Formula to calculate matrix of size 2*/
  119. matrixSize=matrixSize-1; /*Changing size of matrix*/
  120. minor_one=allocMemory(matrixSize);
  121. for(multiplier=0; multiplier<=matrixSize; multiplier++)
  122. {
  123. newHorizontal=0; /*Passing values from major array to minor one*/
  124. newVertical=0;
  125. for(horizontal=1; horizontal<=matrixSize; horizontal++)
  126. {
  127. for(vertical=0; vertical<=matrixSize; vertical++)
  128. {
  129. if(vertical!=multiplier)
  130. {
  131. minor_one[newVertical][newHorizontal]=array_one[vertical][horizontal];
  132. newVertical++;
  133. }
  134. }
  135. newVertical=0;
  136. newHorizontal++;
  137. }
  138.  
  139. if(multiplier%2==0) /*According to formula the component must be multiplied by 1 or -1*/
  140. determinantV+=array_one[multiplier][0]*determinant(minor_one, matrixSize);
  141. if(multiplier%2==1)
  142. determinantV+=(-1)*array_one[multiplier][0]*determinant(minor_one, matrixSize);
  143. }
  144. for(vertical=0; vertical<matrixSize; vertical++) /*Freeing memory of minor array*/
  145. {
  146. free(minor_one[vertical]);
  147. }
  148. free(minor_one);
  149. return determinantV;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement