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

Program to enter n set of elements inside 1-D array and arrange these elements using bubble sort & selection sort.

Selection Sort:

#include<stdio.h>
main()
{
    int i,j,n,a[50],temp;
    printf("Enter the size of the array \n");
    scanf("%d",&n);
    printf("Enter elements\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",(a+i));
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(*(a+i)< *(a+j))
            {
                temp = *(a+i);
                *(a+i)=*(a+j);
                *(a+j)=temp;
            }
        }
    }
    printf("\nThe sorted array is ");
    for(i=0;i<n;i++)
    printf("%d",*(a+i));
}

 

Bubble Sort:

#include<stdio.h>
main()
{
    int a[100],i,j,n,temp;
    printf("Enter size of array");
    scanf("%d",&n);
    printf("Enter elements\n");
    for(i=0;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=(n-i);j++)
        {
            if(*(a+j)>*(a+j+1))
            {
                temp=*(a+j);
                *(a+j)=*(a+j+1);
                *(a+j+1)=temp;

            }
        }
    }

    printf("The sorted array is \n");
    for(i=1;i<=n;i++)
    {
        printf("%d\n",*(a+i));
    }
}

 

Output [Selection Sort]:

selection

Output [Bubble Sort]:

bubble