Tuesday 1 November 2011

2. Matrix Operations



  Title : Matrix Operations */




/* header files */
#include<stdio.h>
#include<conio.h>
 int main(void) /* starting of main */
{
int Mat1[3][3]={0,0}, Mat2[3][3]={0,0}, MatResult[3][3]={0,0};
int iSwChoice=0, iChoice=0, icRow1=0, icColumn1=0, icRow2=0, icColumn2=0;
char Choice;

int create(int [3][3], int, int);       /* function prototype */
int display(int [3][3], int, int);
int addition(int [3][3], int[3][3], int, int, int, int, int [3][3]);
int multiplication(int [3][3], int [3][3], int, int, int, int [3][3]);
int transpose(int [3][3], int, int, int [3][3]);
int saddlept(int [3][3], int, int);

clrscr();                               /* Clearing the screen */

do /* starting of do while loop */
{
printf("\n\tMenu");
printf("\n1 : Create Matrices");
printf("\n2 : Display Matrices");
printf("\n3 : Addition");
printf("\n4 : Multiplication");
printf("\n5 : Transpose");
printf("\n6 : Saddle Point");
printf("\n7 : Exit");

printf("\nEnter your choice\t");
scanf("%d",&iSwChoice);

switch (iSwChoice) /* starting of switch */
{
case 1 : printf("Enter the number of rows and columns of first matrix (A) ");
scanf("%d%d",&icRow1,&icColumn1);
printf("\nEnter the elements of first matrix (A) ");
create(Mat1,icRow1,icColumn1);
printf("Enter the number of rows and columns of second matrix (B) ");
scanf("%d%d",&icRow2,&icColumn2);
printf("\nEnter the elements of second matrix (B) ");
create(Mat2,icRow2,icColumn2);
break;

case 2 : printf("\n\nThe first matrix (A) is\n");
display(Mat1,icRow1,icColumn1);
printf("\n\nThe second matrix (B) is\n");
display(Mat2,icRow2,icColumn2);
break;

case 3 : printf("\n\nThe addition of matrices is\n");
addition(Mat1,Mat2,icRow1,icColumn1,icRow2,icColumn2,MatResult);
break;


case 4 : printf("\nThe multiplication of A and B is\n");
multiplication(Mat1,Mat2,icRow1,icColumn1,icRow2,MatResult);
break;

case 5 : printf("\nTranspose\n1 : A'\n2 : B'\nEnter your choice\t");
scanf("%d",&iChoice);
switch(iChoice)
{
case 1 : printf("Transpose of A is ");
transpose(Mat1,icRow1,icColumn1,MatResult);
display(MatResult,icColumn1,icRow1);
break;
case 2 : printf("Transpose of B is ");
transpose(Mat2,icRow2,icColumn2,MatResult);
display(MatResult,icColumn2,icRow2);
break;
default : printf("Entered choice is not valid");
}
break;

case 6 : printf("\nSaddle Point of\n1 : A\n2 : B\nEnter your choice\t"); scanf("%d",&iChoice);
switch(iChoice)
{
case 1 : saddlept(Mat1,icRow1,icColumn1);
break;

case 2 : saddlept(Mat2,icRow2,icColumn2);
break;

default : printf("Entered choice is not valid");
}
break;
} /* End of switch */
} /* End of do while loop */

while(iSwChoice<=6&&iSwChoice!=0);

getch();
return 0;
} /* End of main */

/* For accepting the values of a matrix */
int create(int MatX[3][3], int iRow, int iColumn)
{                                             /* Function Definition */
int i=0, j=0;
for(i=0;i<iRow;i++)
{
for(j=0;j<iColumn;j++)
{
printf("\t");
scanf("%d",&MatX[i][j]);
}
}
return 0;
}

/* For displaying the values of a matrix */
int display(int MatX[3][3], int iRow, int iColumn)
{
int i=0, j=0;
for(i=0;i<iRow;i++)
{
printf("\n");
for(j=0;j<iColumn;j++)
{
printf("\t%d",MatX[i][j]);
}

}
return 0;
}

/* Addition of two matrices */
int addition(int MatX1[3][3], int MatX2[3][3], int iRow1, int iColumn1, int iRow2, int iColumn2, int MatAddX[3][3])
{
int i=0, j=0;
if(iRow1==iRow2&&iColumn1==iColumn2)
{
for(i=0;i<iRow1;i++)
{
for(j=0;j<iColumn1;j++)
{
MatAddX[i][j]=MatX1[i][j]+MatX2[i][j];
/* Corresponding elements of each matrix are
  added and saved in resultant matrix */
}

}
display(MatAddX,iRow1,iColumn1);
}
else
{
printf("Addition not possible as number of rows and columns of A and B does not match");
}
return 0;
}

int multiplication(int MatX1[3][3], int MatX2[3][3], int iRow1, int iColumn1, int iRow2, int MatMultX[3][3])
{
int i=0, j=0, k=0;
if(iColumn1==iRow2) /* Condition of multiplication is checked */
{
for(i=0;i<iRow1;i++)
{
for(j=0;j<iColumn1;j++)
{
MatMultX[i][j]=0;
for(k=0;k<iRow2;k++)
{
MatMultX[i][j]+=(MatX1[i][k]*MatX2[k][j]);
}
}
}
display(MatMultX,iColumn1,iRow2);
}
else
{
printf("Multiplication not possible");
}
return 0;
}

int transpose(int MatX[3][3], int iRow, int iColumn, int MatTrans[3][3])
{
int i=0, j=0;
for(i=0;i<iRow;i++)
{
for(j=0;j<iColumn;j++)
{
MatTrans[i][j]=MatX[j][i];
/* Interchanged rows and columns are copied in resultant matrix */
}
}
return 0;
}

int saddlept(int MatX[3][3], int iRow, int iColumn)
{
int i=0, j=0, k=0, min=0, max=0, column=0, flag=0;

if(iRow==iColumn)
/* Checking condition for saddle point
  i.e. should be a square matrix */
{
for(i=0;i<iRow;i++)
{
min=MatX[i][0];
for(j=0;j<iColumn;j++)
{
if(MatX[i][j]<=min)
/* Check for minimum value in row */
{
min=MatX[i][j];
column=j;
}
}

max=MatX[0][column];
for(k=0;k<iRow;k++)
{
if(MatX[k][column]>max)
/* Check for maximum value in the column */
{
max=MatX[k][column];
}
}
if(min==max)
{
printf("\nSaddle Point at (%d,%d) is %d",i+1,column+1,MatX[i][column]);
flag=0;
break;
}
else
{
flag=-1;
}
}
if(flag==-1)
{
printf("\nMatrix has no Saddle Point");
}
}
else
{
printf("Cannot Find Saddle Point as it is not a Square Matrix");
}
return 0;
}

No comments:

Post a Comment