Matrix Operations in C Language
Implement following Matrix operations as Given Below:
- Addition with arrays.
- Multiplication without pointers to arrays
- Transpose with arrays
- Saddle point without pointers to arrays
Matrix Operations in C Code
#include
#include
template
class matrix
{
public:
T mat[10][10];
int i;
matrix()
{
cout<<"\n\tMATRIX IS:";
for(i=0;i<m;i++) <br=""> for(j=0;j<n;j++) <br=""> mat[i][j]=0;
}
matrix(int m,int n)
{
cout<<"\n\tMATRIX IS:";
for(int i=0;i<m;i++) <br=""> for(int j=0;j<n;j++) <br=""> cin>>mat[i][j];
}
void display(int m,int n)
{
for(int i=0;i<m;i++) <br=""> {
cout<<"\n";
for(int j=0;j<n;j++) <br=""> {
cout<<"\t";
cout<<mat[i][j]; <br=""> }
}
getch();
}
};
template
void add(T1 mat1,T2 mat2,int r,int c)
{
int i,j;
float mat[20][20];
for(i=0;i<r;i++) <br=""> {
for(j=0;j<c;j++) <br=""> {
mat[i][j]=mat1[i][j]+mat2[i][j];
}
}
disp_res(mat,r,c);
getch();
}
template
void disp_res(T mat,int m,int n)
{
for(int i=0;i<m;i++) <br=""> {
cout<<"\n";
for(int j=0;j<n;j++) <br=""> {
cout<<"\t";
cout<<mat[i][j]; <br=""> }
}
getch();
}
template
void sub(T1 mat1,T2 mat2,int r,int c)
{
int i,j;
float mat[20][20];
for(i=0;i<r;i++) <br=""> {
for(j=0;j<c;j++) <br=""> {
mat[i][j]=mat1[i][j]-mat2[i][j];
}
}
disp_res(mat,r,c);
getch();
}
template
void mul(T1 mat1,T2 mat2,int r,int c,int a)
{
int i,j,k;
float mat[10][10];
for(i=0;i<r;i++) <br=""> {
for(j=0;j<a;j++) <br=""> {
mat[i][j]=0;
for(k=0;k<c;k++) <br=""> mat[i][j]+=mat1[i][k]*mat2[k][j];
}
}
disp_res(mat,r,a);
getch();
}
template
void trans(T mat,int m,int n)
{
int i,j;
T t;
if(m>=n)
{
for(i=0;i<m;i++) <br=""> {
for(j=0;j<i;j++) <br=""> {
t[i][j]=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=t[i][j];
}
}
disp_res(mat,n,m);
}
else
{
for(i=0;i<n;i++) <br=""> {
for(j=0;j<i;j++) <br=""> {
t[i][j]=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=t[i][j];
}
}
disp_res(mat,m,n);
}
getch();
}
void main()
{
int ch,r,c,ch1;
matrixt3();
do
{
clrscr();
cout<<"\n\t##### MATRIX OPERATIONS ######"
<<"\n\t1.INPUT"
<<"\n\t2.DISPLAY"
<<"\n\t3.ADDITION"
<<"\n\t4.SUBTRACTION"
<<"\n\t5.MULTIPLICATION"
<<"\n\t6.TRANSPOSE"
<<"\n\t7.EXIT";
cout<<"\n\tENTER UR CHOICE:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\tENTER NO. OF ROWS:";
cin>>r;
cout<<"\n\tENTER NO. OF COLUMNS:";
cin>>c;
matrixt1(r,c);
matrixt2(r,c);
break;
case 2: do
{
cout<<"\n\t#### DISPLAY MENU ####";
cout<<"\n\t1.1st MATRIX";
cout<<"\n\t2.2nd MATRIX";
cout<<"\n\t3.EXIT";
cout<<"\n\tENTER UR CHOICE:";
cin>>ch1;
clrscr();
switch(ch1)
{
case 1: cout<<"\n\t1st MATRIX";
t1.display(r,c);
break;
case 2: cout<<"\n\t2nd MATRIX";
t2.display(r,c);
break;
case 3: break;
}
}while(ch1!=3);
break;
case 3: cout<<"\n\tADDITION IS:";
add(t1.mat,t2.mat, r,c);
break;
case 4: cout<<"\n\tSUBTRACTION IS:";
sub(t1.mat,t2.mat, r,c);
break;
case 5: cout<<"\n\tMULTIPLICATION IS:";
mul(t1.mat,t2.mat, r,c,r);
break;
case 6: do
{
cout<<"\n\t#### TRANSPOSE MENU ####";
cout<<"\n\t1.1st MATRIX";
cout<<"\n\t2.2nd MATRIX";
cout<<"\n\t3.EXIT";
cout<<"\n\tENTER UR CHOICE:";
cin>>ch1;
clrscr();
switch(ch1)
{
case 1: cout<<"\n\tTRANSPOSE OF 1st MATRIX:";
trans(t1.mat,r,c);
trans(t1.mat,r,c);
break;
case 2: cout<<"\n\tTRANSPOSE OF 2nd MATRIX:";
trans(t2.mat,r,c);
trans(t2.mat,r,c);
break;
case 3: break;
}
}while(ch1!=3);
break;
case 7:
break;
}
}while(ch!=7);
}


Leave a Reply