Program to calculate the determinant of a matrix of any order using a recursive function.

#include "stdio.h"
#include "conio.h"
#include "math.h"

// A square matrix struct
typedef struct
{
int order;
int **array;

}
SQRMATRIX;

// Function declarations
int CreateMatrix( SQRMATRIX *p, int order );
void DisplayMatrix( SQRMATRIX *p );
void InputMatrix( SQRMATRIX *p );
int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col );
int CalcDeterminant( SQRMATRIX *p );
void DestroyMatrix( SQRMATRIX *p );

// main( )
int main( )
{
SQRMATRIX p;
int order;

printf("Enter order of matrix: ");
scanf("%d", &order );

if( !CreateMatrix( &p, order ))
{
printf("Matrix couldn't be created.");
return 0;
}

printf("Matrix created.");
InputMatrix( &p );

printf("The matrix is:");
DisplayMatrix( &p );

printf("The determinant of the matrix is: %d", CalcDeterminant( &p));

getch( );
return 0;
}

// Create matrix of specified order
int CreateMatrix( SQRMATRIX *p, int order )
{
int i;

if( order < 1 )
return 0;
p->order = order;

p->array = (int**) malloc( order * sizeof( int* ));
// Allocate space

for each row

if( !p->array )
return 0;

for( i=0; i < order; i++ )
{ p->array[i] = (int*)
malloc( order* sizeof( int ));
// Allocate

space
for each column
if( !p->array )
return 0;
}
return 1;
}

// Print matrix in proper format
void DisplayMatrix( SQRMATRIX *p )
{
int i,j;

if( p->order < i =" 0;">order; i++ )
{
for( j = 0; j <>order; j++ )
printf("%5d ", p->array[i][j] );

printf("");
}
}

// Input matrix from user
void InputMatrix( SQRMATRIX *p )
{
int i,j;

for( i = 0; i <>order; i++ )
for( j = 0; j <>order; j++ )
{
printf("Enter element at ( %d, %d ): ", i+1, j+1 );
scanf("%d", &p->array[i][j] );
}
}

/* Calculate the 'minor' of the given matrix at given position.
The minor is the matrix formed by deleting the specified row
and column from the matrix.
*/

int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col )
{
int i,j,a,b;

if( p->order <= 1 )
return 0;
if( row >= p->order || col >= p->order )

return 0;

if( !CreateMatrix( minor, p->order-1 ))
return 0;

a = b = 0;

for( i = 0; i <>order; i++ )
{
if( i != row )
{
b = 0;
for( j = 0; j <>order; j++ )
{
if( j != col )
{
minor->array[a][b] = p->array[i][j];
b++; // Increase column-count of minor
}
}
a++; // Increase row-count of minor
}
}

return 1;
}

/* Calculate the determinant recursively.
The recursive definition is :
det( m ) = Summation( i = 0 to order ) [ (-1)^i * m[0][i] * det(
minor( m[0][i] ))]
*/
int CalcDeterminant( SQRMATRIX *p )
{
int i, result = 0, temp;
SQRMATRIX minor;

if( p->order <>order == 1 )
return p->array[0][0];

for( i = 0; i <>order; i++ )
{
if( !CalcMinor( p, &minor, 0, i ))
{
printf("CalcDeterminant( ) : Memory allocation failed.");
return 0;
}

result += ( pow( -1, i ) * p->array[0][i] * CalcDeterminant( &minor
));

DestroyMatrix( &minor );
}

return result;
}
// Release allocated memory
void DestroyMatrix( SQRMATRIX *p )
{
int i;

if( p->order < i =" 0;">order; i++ )
free( p->array[i] ); // free each columns

free( p->array ); // free each row
p->order = 0;
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!