Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file determinant.c
- * @author Michal Szydlowski, TCS2, 163880
- * @date 13.05.2011
- * @brief Program calculates the value of the determinant for a user-defined matrix */
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #define FINISH 2
- /**function for proper reactions in case of occurence of an error in normal execution flow */
- /*void *my_malloc(size_t size)
- {
- static int finish = 0;
- if (finish++ == FINISH)
- return NULL;
- return malloc(size);
- }
- #define malloc my_malloc*/
- /** Function performs operations necessary to calculate the determinant, using the recursive algorithm
- @param **matrix - content of the matrix - variable, which adress is stored by a pointer, of adress stored by a pointer to the allocated memory
- @param size - size of the analyzed matrix
- @return det - calculated determinant
- */
- double determinant(double **matrix, float size)
- {
- int y,x,main_col_counter,x_sub;
- double det = 0;
- double **submatrix = NULL; /* initializing the pointer as a NULL value, so that malloc has the uninitialized memory to point */
- if (size==1)
- {
- det=matrix[0][0]; /* if the matrix consists of one element, this element is the determinant */
- }
- else if(size==2)
- {
- det = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1]; /* algorithm for 2x2 matrix */
- }
- else
- {
- det = 0;
- /* loop calculating the subsequent elements of the sum */
- for (main_col_counter=1;main_col_counter<=size;main_col_counter++)
- {
- submatrix = malloc((size-1)*sizeof(double)); /* allocating the memory dynamically */
- if(submatrix==NULL)
- {
- for(y=0;y<size;y++)
- {
- free(submatrix[y]);
- }
- free(submatrix);
- printf("Memory could not be allocated\n"); /*error handling */
- return 0;
- }
- for (y=0;y<size-1;y++)
- {
- submatrix[y] = malloc((size-1)*sizeof(double)); /* setting the proper memory for each matrix row*/
- }
- /* loop copying the appropriate contents of the main matrix to the submatrices*/
- for (y=1;y<size;y++)
- {
- /* loop copying the content to current submatrix */
- x_sub = 0;
- for (x=0;x<size;x++)
- {
- if (x == main_col_counter-1) /* the column with index equal to sum index is excluded (as well as the first row) */
- continue;
- submatrix[y-1][x_sub] = matrix[y][x];
- x_sub++;
- }
- }
- /* adding the new sum element to the determinant */
- det = det + pow(-1.0,main_col_counter+1.0) * matrix[0][main_col_counter-1] * determinant(submatrix,size-1);
- /* freeing the memory */
- for (y=0;y<size-1;y++)
- {
- free(submatrix[y]);
- }
- free(submatrix);
- }
- }
- return det;
- }
- /*###################################################################################################################*/
- int main()
- {
- int y,x;
- float size;
- double **matrix;
- printf ("Enter matrix size: ");
- if(scanf("%f", &size)!=1) /* checking if the input is a number */
- {
- printf("Invalid characters entered \n");
- return 0;
- }
- else if(size<0 || (size - floor(size))!=0) /* checking if the number entered is positive and integer */
- {
- printf("Size must be a positive integer\n");
- return 0;
- }
- matrix = malloc(size*sizeof(double)); /* allocating the memory dynamically */
- if(matrix==NULL)
- {
- for(y=0;y<size;y++)
- {
- free(matrix[y]);
- }
- free(matrix);
- printf("Memory could not be allocated\n"); /*error handling */
- return 0;
- }
- for(y=0;y<size;y++)
- {
- matrix[y]=malloc(size * sizeof(double)); /* setting the proper memory for each matrix row*/
- }
- for(y=0;y<size;y++)
- {
- for(x=0;x<size;x++)
- {
- printf("Please enter the element of the %i row, %i column: ",y+1,x+1);
- if (scanf("%lf",&matrix[y][x])!=1) /* if the entered character is not a number, program does not accept it end terminates */
- {
- printf("Invalid value \n");
- return 0;
- }
- }
- }
- /* presenting the result */
- printf("The determinant of the matrix is equal %lf \n",determinant(matrix,size));
- for(y=0;y<size;y++)
- {
- free(matrix[y]);
- }
- free(matrix);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement