Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdio_ext.h>
- double determinant(double **array_one, int matrixSize); /*Recursive determinant function*/
- double **allocMemory(int matrixSize); /*Function that allocates memory*/
- void readkeys(double **array_one, int matrixSize); /*Read numbers into matrix from the keyboard*/
- int readMatrixSize(); /*Reads matrix size*/
- int main()
- {
- int matrixSize;
- int vertical, horizontal; /*Vertical and horizontal location in dynamic allocated array*/
- double **array_one=NULL;/**/
- matrixSize=readMatrixSize();
- array_one=allocMemory(matrixSize); /*Returning pointer from function*/
- readkeys(array_one, matrixSize);
- for(vertical=0; vertical<matrixSize; vertical++) /*Showing filled array, for test purposes*/
- {
- printf("\n");
- for(horizontal=0; horizontal<matrixSize; horizontal++)
- printf("%-5f ", array_one[vertical][horizontal]);
- }
- printf("\n\nDeterminant=%f\n", determinant(array_one, matrixSize)); /*Calculation of determinant*/
- for(vertical=0; vertical<matrixSize; vertical++)
- {
- free(array_one[vertical]); /*Freeing allocated memory of second dimension*/
- }
- free(array_one); /*Freeing first dimension*/
- return 0;
- }
- void readkeys(double **array_one, int matrixSize)
- {
- int trash=0;
- int check=0; /*If one you've inputed good valude */
- int vertical;
- int horizontal;
- for(vertical=0; vertical<matrixSize; vertical++)
- for(horizontal=0; horizontal<matrixSize; horizontal++)
- {
- check=0;
- printf("Number: ");
- while(check==0 || trash!='\n')
- {
- check=scanf("%lf", &array_one[vertical][horizontal]);
- trash=getchar();
- if(check==0 ||trash!='\n' )
- {
- printf("Wrong number!!!!!\n");
- }
- __fpurge(stdin);
- }
- }
- }
- int readMatrixSize()
- {
- int trash=0;
- int matrixSize;
- int check=0; /*If one you've inputed good valude */
- while(check==0 || matrixSize<1 ||trash!='\n') /*Checking correctness of input value*/
- {
- check=scanf("%d", &matrixSize);
- trash=getchar();
- if(check==0 || matrixSize<1 ||trash!='\n' )
- {
- printf("Wrong number!!!!!\n");
- }
- __fpurge(stdin);
- }
- printf("Size of matrix: %d\n", matrixSize);
- return matrixSize;
- }
- double **allocMemory(int matrixSize)
- {
- int vertical;
- double **new_one=NULL;
- new_one = malloc(matrixSize*sizeof(double *));
- if (new_one == 0)
- {
- printf("ERROR: Out of memory\n");
- free(new_one);
- exit(1);
- }
- for(vertical=0; vertical<matrixSize; vertical++)
- {
- new_one[vertical] = malloc(matrixSize*sizeof(double));
- if (new_one[vertical] == 0)
- {
- printf("ERROR: Out of memory another dimension\n");
- while(vertical>0)
- {
- vertical--;
- free(new_one[vertical]);
- }
- free(new_one);
- exit(1);
- }
- }
- return new_one;
- }
- double determinant(double **array_one, int matrixSize)
- {
- double determinantV=0;
- int multiplier;
- int vertical, horizontal;
- int newVertical, newHorizontal;
- double **minor_one=NULL;
- if (matrixSize==1)
- return array_one[0][0];
- if (matrixSize==2)
- return array_one[0][0]*array_one[1][1]-array_one[0][1]*array_one[1][0]; /*Formula to calculate matrix of size 2*/
- matrixSize=matrixSize-1; /*Changing size of matrix*/
- minor_one=allocMemory(matrixSize);
- for(multiplier=0; multiplier<=matrixSize; multiplier++)
- {
- newHorizontal=0; /*Passing values from major array to minor one*/
- newVertical=0;
- for(horizontal=1; horizontal<=matrixSize; horizontal++)
- {
- for(vertical=0; vertical<=matrixSize; vertical++)
- {
- if(vertical!=multiplier)
- {
- minor_one[newVertical][newHorizontal]=array_one[vertical][horizontal];
- newVertical++;
- }
- }
- newVertical=0;
- newHorizontal++;
- }
- if(multiplier%2==0) /*According to formula the component must be multiplied by 1 or -1*/
- determinantV+=array_one[multiplier][0]*determinant(minor_one, matrixSize);
- if(multiplier%2==1)
- determinantV+=(-1)*array_one[multiplier][0]*determinant(minor_one, matrixSize);
- }
- for(vertical=0; vertical<matrixSize; vertical++) /*Freeing memory of minor array*/
- {
- free(minor_one[vertical]);
- }
- free(minor_one);
- return determinantV;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement