03-11-2010 02:10 PM
03-11-2010 02:27 PM
05-31-2013 12:24 AM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define N 10
//Function to find power raised to -1
int powe (int i) {
int k=1;
if (i==0)
return 1;
else {
while(i>0){
k *= -1;
i--;
}
return k;
}
}
//Function to display matrix contents
void display (float mat[N][N], int n) {
int i, j;
for (i=0; i<n; i++) {
printf("\n\n");
for (j=0; j<n; j++) {
printf("%10.3f", mat[i][j]);
}
}
}
//Function to copy matrix
void copymat(float smat[N][N], float dmat[N][N], int n) {
int i, j;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
dmat[i][j] = smat[i][j];
}
}
}
//Function to find the transpose of a matrix
void transpose(float mat[N][N], int n) {
int i, j, temp;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
if (i < j) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
}
}
//function to find the determinent of a matrix
int determ (float mat[N][N], int n) {
float lmat[N][N];
int i, j, k, r, c;
float det=0.0;
if (n == 1)
return(mat[0][0]);
if (n == 2)
return((mat[0][0]*mat[1][1])-(mat[0][1]*mat[1][0]));
else {
for(i=0; i<n; i++) {
r=0;
for(j=1; j<n; j++) {
c=0;
for(k=0; k<n; k++) {
if (i==k)
continue;
else {
lmat[r][c] = mat[j][k];
c++;
}
}
r++;
}
det+=powe(i)*(mat[0][i]*determ(lmat, n-1));
}
return(det);
}
}
// function to obtain the cofactor matrix
void cofactor(float mat[N][N], int n) {
float cmat[N][N], lmat[N][N];
int row, col, i, j, k, r, c;
for (row=0; row<n; row++) {
for(col=0; col<n; col++) {
r=0;
for(j=0; j<n; j++) {
if (j == row)
continue;
c=0;
for(k=0; k<n; k++) {
if (k == col)
continue;
else {
lmat[r][c] = mat[j][k];
c++;
}
}
r++;
}
cmat[row][col] = powe(row+col+2)*determ(lmat, n-1);
}
}
copymat(cmat, mat, n);
}
//function to find the inverse
void inverse(float mat[N][N], int n, float det) {
int i, j;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
mat[i][j] /= det;
}
}
}
//Main function
void main() {
float mat[N][N];
int i, j, n;
float det;
//scan the size of matix from keyboard
printf("\n Enter the Size of Matrix: ");
scanf("%d", &n);
if (n > N) { // If matrix size exceeds the size of allocated matrix
printf("\n Matrix Overflow");
getch();
exit(0);
}
//scan the elements of matrix from keyboard
printf("\n Enter a Matrix");
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
printf("\n Input Element(%d, %d): ", i+1, j+1);
scanf("%f", &mat[i][j]);
}
}
printf("\n Matix ");
display(mat, n); // display matrix
printf("\n Press any key ..."); getch();
det = determ(mat, n); // obtain determinent
cofactor(mat, n); // obtain cofactor matrix
printf("\n Cofactor Matrix");
display(mat, n);
printf("\n Press any key ..."); getch();
transpose(mat, n); // transpose cofactor matrix = matix adjoint
printf("\n Adjoint Matrix");
display(mat, n);
printf("\n Press any key ..."); getch();
printf("\n Determinant of Matrix is %8.2f", det);
if (det == 0)
printf("\n Matix is Singular, Hence cannot have Inverse");
else {
printf("\n Matix is non Singular. \n Inverse of Matrix is: ");
inverse(mat, n, det); //divide each element of matrix adjoint
display(mat, n); // by the determinent = matrix inverse
}
getch();
}