Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Assignment No.

2 Matrix Operations
#include<stdio.h>
//Global declaration.
int a[12][13],b[12][13],c[12][13],d[12][13];
//Function prototype declaration.
void input_mat(int x[12][13],int r,int c);
void clear_mat(int x[12][13],int r,int c);
void add_mat(int x[12][13],int r1,int c1,int y[12][13],int r2,int c2);
void mul_mat(int x[12][13],int r1,int c1,int y[12][13],int r2,int c2);
void print_mat(int x[12][13],int r,int c);
void trans_mat(int x[12][13],int r,int c);
void sadpoint(int x[12][13],int m,int n);
void main()
{
int i,r1,r2,c1,c2,m,p;
printf("\n This program performs following operation on matrices:\n");
for(p=0;p<100;p++)
{
printf("\n 1.Addition\n 2.Multiplication\n 3.Transpose\n 4.Saddl
epoint\n 5.Exit\n Please enter your selection:\t");
scanf("%d",&m);
if(m==1 || m==2)
{
//Input section for array A.
printf("\nEnter selections for array A");
printf("\nEnter no. of rows\t");
scanf("\t %d",&r1);
printf("\nEnter no. of columns\t");
scanf("\t %d",&c1);
input_mat(a,r1,c1);
print_mat(a,r1,c1);
//Input section for array B.
printf("\nEnter selections for array B");
printf("\nEnter no. of rows\t");
scanf("\t %d",&r2);
printf("\nEnter no. of columns\t");
scanf("\t %d",&c2);
input_mat(b,r2,c2);
print_mat(b,r2,c2);
//Addition section
if(m==1)
{
if(r1==r2 && c1==c2)
{
printf("\nAddition of both matrices is:
\n");
add_mat(a,r1,c1,b,r2,c2);
print_mat(c,r1,c1);
clear_mat(c,r1,c1);
}
}
//Multiplication section.
if(m==2)

{
if(c1==r2)
{
printf("\nMultiplication of both matrice
s is: \n");
mul_mat(a,r1,c1,b,r2,c2);
print_mat(c,r2,c1);
clear_mat(c,r2,c1);
}
}
}
if(m==3 || m==4)
{
//Input section for array.
printf("\nEnter selections for array");
printf("\nEnter no. of rows\t");
scanf("\t %d",&r1);
printf("\nEnter no. of columns\t");
scanf("\t %d",&c1);
input_mat(a,r1,c1);
print_mat(a,r1,c1);
//Transpose section.
if(m==3)
{
printf("\nTranspose of array is: \n");
trans_mat(a,r1,c1);
print_mat(d,r1,c1);
clear_mat(d,r1,c1);
}
//Saddle point section.
if(m==4)
{
sadpoint(a,r1,c1);
}
}
if(m==5)
{
printf("Thank You.\nCredits: Coded by Aayush \n\n");
break;
}
}
}
void input_mat(int x[12][13],int r,int c)
{
int i,j;
printf("\nEnter elements of matrix:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&x[i][j]);
}
}
}
void print_mat(int x[12][13],int r,int c)

{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t %d",x[i][j]);
}
printf("\n");
}
}
void add_mat(int x[12][13],int r1,int c1,int y[12][13],int r2,int c2)
{
int i,j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=x[i][j]+y[i][j];
}
}
}
void clear_mat(int x[12][13],int r,int c)
{
int i,j;
printf("\n \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
x[i][j]=0;
}
}
}
void mul_mat(int x[12][13],int r1,int c1,int y[12][13],int r2,int c2)
{
int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<r2;k++)
{
c[i][j]=c[i][j]+(x[i][j]*y[i][j]);
}
}
}
}
void trans_mat(int x[12][13],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=x[j][i];

}
}
}
void sadpoint(int x[12][13],int m,int n)
{
int i,j,k,min,max,col;
for(i=0;i<m;i++)
{
min=x[i][0];
for(j=0;j<n;j++)
{
if(x[i][j]<=min)
{
min=x[i][j];
col=j;
}
}
max=x[0][col];
for(k=0;k<m;k++)
{
if(x[k][col]>=max)
{
max=x[k][col];
}
}
if(max==min)
{
printf("\nSaddle point is %d\n",x[i][col]);
}
}
}

You might also like