Friday 16 August 2019

C programming for addition, subtraction, multiplication of matrices


/*Addition and subtraction of matrices */
#include<stdio.h>
void
 main()
{
                int i,j,m,n,a[20][20],b[20][20],c[20][20],d[20][20];
                printf("Enter the order of matrices:");
                scanf("%d  %d",&m,&n);
                printf("Enter the element of matrix A:");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                scanf("%d",&a[i][j]);
                                printf("\n");
                }
                printf("the input matrix A is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                                printf("Enter the element of matrix B:");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                scanf("%d",&b[i][j]);
                                printf("\n");
                }
                printf("the input matrix B is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",b[i][j]);
                                printf("\n");
                }
                /*matrix addition*/
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                c[i][j]=a[i][j]+b[i][j];
                }
                printf("the summation matrix is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",c[i][j]);
                                printf("\n");
                }
                /*matrix subtraction*/
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                d[i][j]=a[i][j]-b[i][j];
                }
                printf("the summation matrix is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",d[i][j]);
                                printf("\n");
                }
}

RESULT:
Enter the order of matrices:3 3
Enter the element of matrix A:1 2 3 4 5 6 7 8 9
the input matrix A is:
1       2       3
4       5       6
7       8       9
Enter the element of matrix B:12
4 56 11 7 45 0 4 6
the input matrix B is:
12      4       56
11      7       45
0       4       6
the summation matrix is:
13      6       59
15      12      51
7       12      15
the summation matrix is:
-11     -2      -53
-7      -2      -39
7       4       3
Press any key to continue
/*multiplication of matrices*/
#include<stdio.h>
void
 main()
{
                int i,j,m,n,p,k,a[20][20],b[20][20],c[20][20];
                printf("Enter the order of matrix A:");
                scanf("%d  %d",&m,&n);
                printf("Enter the order of matrix B:");
                scanf("%d  %d",&n,&p);
                printf("Enter the element of matrix A:");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                scanf("%d",&a[i][j]);
                                printf("\n");
                }
                printf("the input matrix A is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                printf("Enter the element of matrix B:");
                for(i=0;i<n;i++)
                {
                                for(j=0;j<p;j++)
                                                scanf("%d",&b[i][j]);
                                printf("\n");
                }
                printf("the input matrix B is:\n");
                for(i=0;i<n;i++)
                {
                                for(j=0;j<p;j++)
                                                printf("%d\t",b[i][j]);
                                printf("\n");
                }
                /*matrix multiplication*/
                for(i=0;i<m;i++)
                {
                                for(j=0;j<p;j++)
                                {
                                                c[i][j]=0;
                                                for(k=0;k<n;k++)
                                                {
                                                                c[i][j]=c[i][j]+a[i][k]*b[k][j];
                                                }
                                }
                }
                printf("the multiplication matrix is :\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<p;j++)
                                                printf("%d\t",c[i][j]);
                                printf("\n");
                }
}
RESULT:
Enter the order of matrix A:3 2
Enter the order of matrix B:2 3
Enter the element of matrix A:1 2 3 4 5 6
the input matrix A is:
1       2
3       4
5       6
Enter the element of matrix B:45 65 33 11 0 5
the input matrix B is:
45      65      33
11      0       5
the multiplication matrix is :
67      65      43
179     195     119
291     325     195
Press any key to continue

No comments:

Post a Comment

Thanks for comment.