Sunday, June 8, 2008

Matrix Operations


/* Subhranath Chunder - Matrix Operations */

/* A program to implement the following operation on two matrices:
a. Addition
b. Multiplication
c. Transposition
d. Determinant */

#include<stdio.h>
#include<conio.h>
#include<math.h>

/* Function declarations */
void print(int[][100],int,int);
void add(int[][100],int,int,int[][100],int,int);
void multiply(int[][100],int,int,int[][100],int,int);
void transpose(int[][100],int,int);
int determinant(int[][100],int);

/* Main Program */
void main()
{
int inp1[100][100],inp2[100][100],m1,n1,m2,n2,i,j,option=0;
clrscr();

/* The values of the matrices are accepted */
printf("Enter the dimensions of the first matrix in the form mXn: ");
scanf("%dX%d",&m1,&n1);
printf("Enter the values of the first matrix:\n");
for(i=0;i<m1;++i)
{
for(j=0;j<n1;++j)
scanf("%d",&inp1[i][j]);
}
printf("\n\nEnter the dimensions of the second matrix in the form mXn: ");
scanf("%dX%d",&m2,&n2);
printf("Enter the values of the second matrix:\n");
for(i=0;i<m2;++i)
{
for(j=0;j<n2;++j)
scanf("%d",&inp2[i][j]);
}

/* Main Menu - where the user chooses what he wants to do */
while(option==0)
{
clrscr();
printf("You can perform the following operations on the matrices:\n\n1. Addition\n2. Multiplication\n3. Transposition\n4. Determinant\n5. Exit\n\nEnter Choice: ");
scanf("%d",&option);
clrscr();

/* Performs addition on the two matrices */
if(option==1)
{
add(inp1,m1,n1,inp2,m2,n2);
getch();
}

/* Performs multiplication on the two matrices */
if(option==2)
{
multiply(inp1,m1,n1,inp2,m2,n2);
getch();
}

/* Transposes a specified matrix */
while(option==3)
{
printf("Which matrix would you like to transpose?\n\n1. First Matrix\n2. Second Matrix\n\nEnter Choice: ");
scanf("%d",&option);
clrscr();
if(option==1 || option==2)
{
if(option==1)
transpose(inp1,m1,n1);
if(option==2)
transpose(inp2,m2,n2);
getch();
}
else
option=3;
}

/* Finds the determinant of a specified matrix */
while(option==4)
{
printf("Find the determinant of which matrix?\n\n1. First Matrix\n2. Second Matrix\n\nEnter Choice: ");
scanf("%d",&option);
clrscr();
if(option==1 || option==2)
{
if(option==1)
{
if(m1==n1)
printf("The determinant of the matrix is: %d",determinant(inp1,m1));
else
printf("The derterminant of the matrix cannot be determined.");
}
if(option==2)
{
if(m2==n2)
printf("The determinant of the matrix is: %d",determinant(inp2,m2));
else
printf("The derterminant of the matrix cannot be determined.");
}
getch();
}
else
option=4;
}

/* Exits the program */
if(option==5)
break;

/* Returns the control back to main menu */
option=0;
}
}
/* Main Program Ends */

/* Performs addition on the two specified matrices */
void add(int arr1[][100],int m1,int n1,int arr2[][100],int m2,int n2)
{
int arr3[100][100],i,j;
if(m1==m2 && n1==n2)
{
for(i=0;i<m1;++i)
{
for(j=0;j<n1;++j)
arr3[i][j]=arr1[i][j]+arr2[i][j];
}
printf("The sum of the two matrices is given below:\n\n");
print(arr3,m1,n1);
}
else
printf("The matrices cannot be added");
}

/* Performs multiplication on the two specified matrices */
void multiply(int arr1[][100],int m1,int n1,int arr2[][100],int m2,int n2)
{
int arr3[100][100],i,j,k;
if(n1==m2)
{
for(i=0;i<m1;++i)
{
for(j=0;j<n2;++j)
{
arr3[i][j]=0;
for(k=0;k<n1;++k)
arr3[i][j]+=arr1[i][k]*arr2[k][j];
}
}
printf("The product of the two matrices is given below:\n\n");
print(arr3,m1,n2);
}
else
printf("The matrices cannot be multiplied");
}

/* Transposes a specified matrix */
void transpose(int arr[][100],int m,int n)
{
int arr2[100][100],i,j;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
arr2[j][i]=arr[i][j];
}
printf("The transpose of the matrix is given below:\n\n");
print(arr2,n,m);
}

/* Finds the determinant of a specified matrix */
int determinant(int arr[][100],int n)
{
int temp[100][100],a,b,result=0,i,j,k;
if(n==1)
return(arr[0][0]);
else
{
for(i=0;i<n;++i)
{
a=0;
b=0;
for(j=1;j<n;++j)
{
for(k=0;k<n;++k)
{
if(k!=i)
{
temp[a][b]=arr[j][k];
++b;
}
}
b=0;
++a;
}
result+=arr[0][i]*determinant(temp,n-1)*pow(-1,i);
}
}
return result;
}

/* Prints a matrix of specified dimension */
void print(int arr[][100],int m,int n)
{
int i,j;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
printf("%d\t",arr[i][j]);
printf("\n");
}
}

No comments: