Matrix Operations-Addition,Subtraction,Transpose - C Program

Program:
#include<stdio.h>
void readmatrix(int x[10][10],int,int);
void sum(int y[10][10],int z[10][10],int s[10][10],int,int);
void showmatrix(int g[10][10],int,int);
void difference(int d[10][10],int y[10][10],int z[10][10],int,int);
void transpose(int t[10][10],int y[10][10],int,int);
main()
{
    int a[10][10],b[10][10],c[10][10],d[10][10],t[10][10],m,n,p,q;
    printf("Enter the order of first matrix:\n");
    scanf("%d%d",&m,&n);
    printf("Enter the order of second matrix:\n");
    scanf("%d%d",&p,&q);
    if(m==p&&n==q)
    {
        printf("Enter the elements for the first matrix:\n");
        readmatrix(a,m,n);
        printf("Enter the elements for the second matrix:\n");
        readmatrix(b,p,q);
        printf("First matrix:\n");
        showmatrix(a,m,n);
        printf("Transpose of the above matrix:\n");
        transpose(t,a,m,n);
        showmatrix(t,n,m);
        printf("Second matrix:\n");
        showmatrix(b,p,q);
        printf("Transpose of the above matrix:\n");
        transpose(t,b,m,n);
        showmatrix(t,n,m);
        sum(a,b,c,p,q);
        printf("Sum of the matrix:\n");
        showmatrix(c,m,q);
        difference(d,a,b,m,n);
        printf("Difference of the matrix:\n");
        showmatrix(d,p,q);
        }
    else
        {
        printf("Operation is not possible...\n");
        }
    }
void readmatrix(int x[10][10],int u,int v)
    {
    int i,j;
   
    for(i=1;i<=u;i++)
        {
        for(j=1;j<=v;j++)
            {
            scanf("%d",&x[i][j]);
            }
        }
    }

void sum(int y[10][10],int z[10][10],int s[10][10],int m,int n)
    {
    int i,j;
    for(i=1;i<=m;i++)
        {
        for(j=1;j<=n;j++)
            {
            s[i][j]=y[i][j]+z[i][j];
            }
        }
    }
void showmatrix(int g[10][10],int f,int d)
    {
    int i,j;
        for(i=1;i<=f;i++)
        {
        for(j=1;j<=d;j++)
                    {
            printf("%d\t",g[i][j]);
            }
        printf("\n");
                }
    }
void difference(int d[10][10],int y[10][10],int z[10][10],int m,int n)
    {
    int i,j;
     for(i=1;i<=m;i++)
                {
                for(j=1;j<=n;j++)
                        {
                        d[i][j]=y[i][j]-z[i][j];
                        }
                }
    }
void transpose(int t[10][10],int y[10][10],int m,int n)
    {
    int i,j;
    for(i=1;i<=n;i++)
        {
        for(j=1;j<=m;j++)
            {
            t[i][j]=y[j][i];
            }
        }
    }

Output:
nn@linuxmint ~ $ gcc c19.c
nn@linuxmint ~ $ ./a.out
Enter the order of first matrix:
2
2
Enter the order of second matrix:
2
2
Enter the elements for the first matrix:
9
8
7
6
Enter the elements for the second matrix:
5
4
3
2
First matrix:
9    8   
7    6   
Transpose of the above matrix:
9    7   
8    6   
Second matrix:
5    4   
3    2   
Transpose of the above matrix:
5    3   
4    2   
Sum of the matrix:
14    12   
10    8   
Difference of the matrix:
4    4   
4    4   
nn@linuxmint ~ $

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...