Thursday, 4 July 2019

C Program for Matrix Operations


Represent matrix using a two-dimensional array and perform addition, multiplication, transpose and saddle point (with and without pointer).

#include<stdio.h>

void add(int mat1[10][10], int mat2[10][10], int m, int n);
void mul(int mat1[10][10], int mat2[10][10], int m, int n);
void trns(int mat1[10][10],int m, int n);
void saddle(int mat1[10][10],int m, int n);


int main()
{
      int mat1[10][10],mat2[10][10],i,j,n,m,ch,z;
      setbuf(stdout, NULL);
      printf("\nEnter the rows and columns of first matrix : ");
      scanf("%d%d",&m,&n);
      printf("\nEnter elements of first matrix by rows : ");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  scanf("%d",&mat1[i][j]);
            }
      }
      for(i=0;i<m;i++)
      {
            printf("\n");
            for(j=0;j<n;j++)
            {
                  printf("%d\t",mat1[i][j]);
            }
      }
      printf("\nEnter the rows and columns of second matrix : ");
      scanf("%d%d",&m,&n);
      printf("\nEnter elements of second matrix by rows : ");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  scanf("%d",&mat2[i][j]);
            }
      }
      for(i=0;i<m;i++)
      {
            printf("\n");
            for(j=0;j<n;j++)
            {
                  printf("%d\t",mat2[i][j]);
            }
      }
      do
      {
            printf("\n1.Addition \n2.Multiplication \n3.Transpose \n4.Saddle Point");
            printf("\nEnter choice: ");
            scanf("%d", &ch);
            switch(ch)
            {
            case 1:
                  add(mat1, mat2, m, n);
                  break;
            case 2:
                  mul(mat1, mat2, m, n);
                  break;
            case 3:
                  trns(mat1, m, n);
                  break;
            case 4:
                  saddle(mat1, m, n);
                  break;
            default:
                  printf("\nInvalid choice.");
                  break;
            }
            printf("\nDo you wish to continue? \nType 1 for Yes 0 for No\n");
            scanf("\n%d",&z);

      }
      while(z==1);
      return 0;
}
void add(int mat1[10][10], int mat2[10][10], int m, int n)
{
      int i,j, c[10][10];
      printf("\nAddition is : ");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  *(*(c+i)+j)=*(*(mat1+i)+j)+*(*(mat2+i)+j);
            }
      }
      for(i=0;i<m;i++)
      {
            printf("\n");
            for(j=0;j<n;j++)
            {
                  printf("%d\t",*(*(c+i)+j));
            }
      }
}
void mul(int mat1[10][10], int mat2[10][10], int m, int n)
{
      int sum, k, i, j, c[10][10];
      printf("\nMultiplication of matrix is : ");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  sum=0;
                  for(k=0;k<m;k++)
                  {
                        sum=sum+mat1[i][k]*mat2[k][j];
                  }
                  c[i][j]=sum;
            }
      }
      for(i=0;i<m;i++)
      {
            printf("\n");
            for(j=0;j<n;j++)
            {
                  printf("%d\t",c[i][j]);
            }
      }
}
void trns(int mat1[10][10],int m, int n)
{
      int i, j, c[10][10];
      printf("\nTranspose of first matrix is : ");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  *(*(c+j)+i)=*(*(mat1+i)+j);
            }
      }
      for(j=0;j<m;j++)
      {
            printf("\n");
            for(i=0;i<n;i++)
            {
                  printf("%d\t", *(*(c+j)+i));
            }
      }

}

void saddle(int mat1[10][10], int m, int n)
{
      int i,j,k,min,max,col;
      for(i=0;i<m;i++)
      {
            min=mat1[i][0];
            for(j=0;j<n;j++)
            {
                  if(mat1[i][j]<=min)
                  {
                        min=mat1[i][j];
                        col=j;
                  }
            }
            max=mat1[0][col];
            for(k=0;k<m;k++)
            {
                  if(mat1[k][col]>=max)
                  {
                        max=mat1[k][col];
                  }
            }
            if(max==min)
                  printf("saddle pt. is at mat1[%d] [%d]" , i,col);
      }

}



Output:


Enter the rows and columns of first matrix : 2
2

Enter elements of first matrix by rows : 1
2
3
4

1 2
3 4
Enter the rows and columns of second matrix : 2
2

Enter elements of second matrix by rows : 3
4
5
6

3 4
5 6
1.Addition 
2.Multiplication 
3.Transpose 
4.Saddle Point
Enter choice: 1

Addition is : 
4 6
8 10
Do you wish to continue? 
Type 1 for Yes 0 for No
1

1.Addition 
2.Multiplication 
3.Transpose 
4.Saddle Point
Enter choice: 2

Multiplication of matrix is : 
13 16
29 36
Do you wish to continue? 
Type 1 for Yes 0 for No
1

1.Addition 
2.Multiplication 
3.Transpose 
4.Saddle Point
Enter choice: 3

Transpose of first matrix is : 
1 3
2 4
Do you wish to continue? 
Type 1 for Yes 0 for No
1

1.Addition 
2.Multiplication 
3.Transpose 
4.Saddle Point
Enter choice: 4
saddle pt. is at mat1[1] [0]
Do you wish to continue? 
Type 1 for Yes 0 for No

No comments:

Post a Comment

Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...