# C Program to do matrix operations

Matrices are very useful in many branches of mathematics. They are sometimes used in physics also. Hence, I wrote a C program which can perform any of the basic operations on matrices like addition, multiplication etc.

## Introduction

The common operations on matrices can be listed as:
• Finding trace of a matrix.

• Finding determinant of a matrix.

• Finding sum of two matrices.

• Finding difference of two matrices.

• Finding product of a number and a matrix.

• Finding product of two matrices.

• Finding power of a matrix.

• Finding transpose of a matrix.

• Finding inverse of a matrix.

• Finding permanent of a matrix.

The permanent of a matrix can be obtained by replacing all minus symbols in the determinant formula with plus symbols. Using the program below, all the above listed operations can be done.

## Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define S 25
typedef long double val;
void scanning_matrix(val A[][S],val a,val b)
{
val I,J;
for(I=0;I{
for(J=0;Jscanf("%Lf",&A[I][J]);
}
}
void printing_matrix(val A[][S],val a,val b)
{
val I,J;
for(I=0;I{
for(J=0;Jprintf("(%Lf)\t",A[I][J]);
printf("\n\n ");
}
}
void multiply_matrix(val A[][S],val B[][S],val C[][S],val a,val b,val c)
{
val I,J,K;
for(I=0;I{
for(J=0;J{
for(K=0,C[I][J]=0;KC[I][J]+=A[I][K]*B[K][J];
}
}
}
val detandper_matrix(val A[][S],val a,val b)
{
val B[S][S],c,I,J,M,N,P,Q,X;
if(b!=1&&b!=2)
printf("\n SYNTAX ERROR:TRY AGAIN \n ");
else
{
X=(b==1)?(-1):(+1);
if(a==1)
c=A[0][0];
else
{
for(P=0,c=0,Q=1;P{
for(I=0,M=0,N=0;I{
for(J=0;J{
B[I][J]=0;
if(I!=0&&J!=P)
{
B[M][N]=A[I][J];
if(N<(a-2))
N++;
else
{
N=0;
M++;
}
}
}
}
c+=Q*A[0][P]*detandper_matrix(B,a-1,b);
Q=X*Q;
}
}
}
return(c);
}
void cofactor_matrix(val A[][S],val B[][S],val a)
{
val C[S][S],I,J,M,N,P,Q;
for(P=0;P{
for(Q=0;Q{
for(I=0,M=0,N=0;I{
for(J=0;J{
if(I!=P&&J!=Q)
{
C[M][N]=A[I][J];
if(N<(a-2))
N++;
else
{
N=0;
M++;
}
}
}
}
B[P][Q]=pow(-1,P+Q)*detandper_matrix(C,a-1,1);
}
}
}
void main()
{
val I,J,K,NUMBER,choice,a,b,c,d,A[S][S],B[S][S],C[S][S];
for(choice=0;choice<=11;choice++)
{
printf("\n 1.Trace(A) ");
printf(" 2.Determinant(A) ");
printf(" 3.(A)+(B) ");
printf(" 4.(A)-(B) ");
printf(" 5.(a)*[A] ");
printf(" 6.(A)*(B) ");
printf("\n\n 7.[A]^(a) ");
printf(" 8.Transpose of (A) ");
printf(" 9.Inverse of (A) ");
printf(" 10.Permanent(A) ");
printf(" 11.Off ");
scanf("%Lf",&choice);
if(choice==1)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&a==b)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
for(I=0,NUMBER=0;I{
for(J=0;JNUMBER+=(I==J)?(A[I][J]):(0);
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Trace of Matrix (A) is {%Lf} \n\n ",NUMBER);
}
else
printf("\n Trace of Matrix (A) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==2)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&a==b)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
NUMBER=detandper_matrix(A,a,1);
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Determinant of Matrix (A) is {%Lf} \n\n ",NUMBER);
}
else
printf("\n Determinant of Matrix (A) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==3)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
printf("\n Enter {order} of matrix (B) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&c,&d);
if(a==floor(a)&&b==floor(b)&&a>0&&b>0&&a==c&&b==d)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
printf("\n Enter {elements} of matrix (B) [row wise]:");
scanning_matrix(B,c,d);
for(I=0;I{
for(J=0;JC[I][J]=A[I][J]+B[I][J];
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Matrix (B) is: \n\n ");
printing_matrix(B,c,d);
printf("Matrix (A)+(B) is: \n\n ");
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",C[I][J]);
printf("\n\n ");
}
}
else
printf("\n Matrix (A)+(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==4)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
printf("\n Enter {order} of matrix (B) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&c,&d);
if(a==floor(a)&&b==floor(b)&&a>0&&b>0&&a==c&&b==d)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
printf("\n Enter {elements} of matrix (B) [row wise]:");
scanning_matrix(B,c,d);
for(I=0;I{
for(J=0;JC[I][J]=A[I][J]-B[I][J];
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Matrix (B) is: \n\n ");
printing_matrix(B,c,d);
printf("Matrix (A)-(B) is: \n\n ");
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",C[I][J]);
printf("\n\n ");
}
}
else
printf("\n Matrix (A)-(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==5)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&b==floor(b)&&b>0)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
printf("\n Enter {a} value:");
scanf("%Lf",&NUMBER);
for(I=0;I{
for(J=0;JB[I][J]=NUMBER*A[I][J];
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Matrix (%Lf)*[A] is: \n\n ",NUMBER);
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",B[I][J]);
printf("\n\n ");
}
}
else
printf("\n Matrix [A] IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==6)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
printf("\n Enter {order} of matrix (B) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&c,&d);
if(a==floor(a)&&c==floor(c)&&d==floor(d)&&a>0&&c>0&&d>0&&c==b)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
printf("\n Enter {elements} of matrix (B) [row wise]:");
scanning_matrix(B,c,d);
multiply_matrix(A,B,C,a,c,d);
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Matrix (B) is: \n\n ");
printing_matrix(B,c,d);
printf("Matrix (A)*(B) is: \n\n ");
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",C[I][J]);
printf("\n\n ");
}
}
else
printf("\n Matrix (A)*(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==7)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&a==b)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
printf("\n Enter {a} value:");
scanf("%Lf",&c);
if(c==floor(c)&&c>0)
{
for(I=0;I{
for(J=0;JC[I][J]=(I!=J)?(0):(1);
}
for(K=0;K{
multiply_matrix(A,C,B,a,b,b);
for(I=0;I{
for(J=0;JC[I][J]=B[I][J];
}
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Matrix [A]^(%.0Lf) is: \n\n ",c);
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",B[I][J]);
printf("\n\n ");
}
}
else
printf("\n Matrix [A]^(%Lf) IS UNDEFINED:TRY AGAIN \n\n ",c);
}
else
printf("\n Matrix [A] IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==8)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&b==floor(b)&&b>0)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
for(I=0;I{
for(J=0;JB[I][J]=A[J][I];
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Transpose of Matrix (A) is: \n\n ");
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",B[I][J]);
printf("\n\n ");
}
}
else
printf("\n Transpose of Matrix (A) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==9)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&a==b)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
NUMBER=detandper_matrix(A,a,1);
if(NUMBER!=0)
{
cofactor_matrix(A,C,a);
for(I=0;I{
for(J=0;JB[I][J]=C[J][I]/NUMBER;
}
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Inverse of matrix (A) is: \n\n ");
for(I=0;I{
for(J=0;Jprintf("{%Lf}\t",B[I][J]);
printf("\n\n ");
}
}
else
{
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Inverse of Matrix (A) IS UNDEFINED:TRY AGAIN \n\n ");
}
}
else
printf("\n Inverse of Matrix (A) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==10)
{
printf("\n Enter {order} of matrix (A) [(rows)x(columns) format]:");
scanf("%Lfx%Lf",&a,&b);
if(a==floor(a)&&a>0&&a==b)
{
printf("\n Enter {elements} of matrix (A) [row wise]:");
scanning_matrix(A,a,b);
NUMBER=detandper_matrix(A,a,2);
printf("\n Matrix (A) is: \n\n ");
printing_matrix(A,a,b);
printf("Permanent of Matrix (A) is {%Lf} \n\n ",NUMBER);
}
else
printf("\n Permanent of Matrix (A) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice==11)
{
printf("\n ");
exit(0);
}
else
{
printf("\n {%Lf} IS AN INVALID CHOICE:TRY AGAIN \n\n ",choice1);
choice=0;
}
}
getch();
}

## Working of the Program

1. The header files stdio.h, conio.h and math.h are used for standard input output operations, conditional input output operations and mathematical operations respectively.

2. A variable S is defined to store the maximum size of a matrix.

3. The function scanning_matrix is used to scan a two dimensional matrix.

4. The function printing_matrix is used to print a two dimensional matrix in an understandable manner.

5. The function multiply_matrix is used to perform the multiplication operation on 2 two dimensional matrices.

6. The function detandper_matrix is used to calculate the determinant or permanent of a two dimensional matrix and is used in the calculation of inverse of such matrix.

7. The function cofactor_matrix is used to find the cofactor matrix of a two dimensional matrix and is used in the calculation of inverse of such matrix.

8. A variable 'choice' is used to select the required matrix operation.

9. A typedef statement is used so that 'val' can be used in place of 'long double' in the program.

10. An else-if ladder has the codes for different operations which can be accessed from corresponding 'choice' values.

This program deals only with the two dimensional matrices. The code for any particular operation can be obtained by selecting the particular 'choice' value in the program. Any operation can be added by just adding a new else-if block in the program.

No responses found. Be the first to comment...

• Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
• No HTML formatting and links to other web sites are allowed.
• This is a strictly moderated site. Absolutely no spam allowed.
• Name:
Email: