To perform following operations: -> Addition of two matrices, Transpose & Sum of upper and lower triangular

Program:

#include<stdio.h>
void main()
 {
   int i,j,c,r,k;
   int a[20][20],b[20][20],ma[20][20],choice;
   printf("Enter an operation");
   printf("\n1. Addition of matrices\n2. Transpose\n3. Upper triangular matrix\n4. Lower triangular matrix\n");
 scanf("%d",&choice);
switch(choice)
{
    case 1:
         printf("\nEnter the value for row and column: ");
   scanf("%d%d",&c,&r);
   printf("\nEnter the value for matrix A\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
         scanf("%d",&a[i][j]);
        }

     }
   printf("\nEnter the value for matrix B\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
          scanf("%d",&b[i][j]);
        }

     }
      printf("\nMatrix A is\n");
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("%d",*(*(a+i)+j));
         }
       printf("\n");
     }
      printf("\nMatrix B is:\n");
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t%d",*(*(b+i)+j));
         }

       printf("\n");
     }

   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          *(*(ma+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
         }}
          printf("\n\tThe addition matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t%d",*(*(ma+i)+j));
         }
       printf("\n");
     }
     break;
  case 2:
      printf("\n\tEnter the value for row and column:  ");
   scanf("%d%d",&c,&r);
   printf("\n\tEnter the value for matrix \n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
         scanf("%d",&a[i][j]);
        }

     }
     printf("\n\t matrix is:\n");
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t%d",*(*(a+i)+j));
         }
       printf("\n");
     }
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        { *(*(b+i)+j)=*(*(a+j)+i);
        }
     }
      printf("\n\t Transpose of the matrix is:\n");
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t%d",*(*(b+i)+j));
         }
       printf("\n");
     }
     break;
  case 3:
      printf("\n\tEnter the value for row and column:  ");
   scanf("%d%d",&c,&r);
   printf("\n\tEnter the value for matrix \n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
         scanf("%d",&a[i][j]);
        }

     }
     printf("\n\t matrix is:\n");
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t%d",*(*(a+i)+j));
         }
       printf("\n");
     }
     printf("Upper triangular matrix is:\n");
      for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         { if(i>=j)
         *(*(a+i)+j)=0;
         }
         }
        for(i=0;i<c;i++)
        {
            for(j=0;j<r;j++)
        {
            printf("%d  ",*(*(a+i)+j));
        }
        printf("\n");
        }

         break;
    case 4:
         printf("\n\tEnter the value for row and column:  ");
   scanf("%d%d",&c,&r);
   printf("\n\tEnter the value for matrix \n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
         scanf("%d",&a[i][j]);
        }

     }
     printf("\n\t matrix is:\n");
     for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t%d",*(*(a+i)+j));
         }
       printf("\n");
     }

     printf("lower triangular matrix is:\n");
       for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         { if(i<=j)
         *(*(a+i)+j)=0;
         }
         }
        for(i=0;i<c;i++)
        {
            for(j=0;j<r;j++)
        {
            printf("%d  ",*(*(a+i)+j));
        }
        printf("\n");
        }
         break;
  }
 }

OUTPUT:

Matrix operations

Leave a Reply

Your email address will not be published.