Showing posts with label matrix program. Show all posts
Showing posts with label matrix program. Show all posts

Sunday, 18 August 2019

Trace of a matrix - C program


/*trace of a matrix*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i,j,a[20][20];
                float s=0.0;
                printf("enter the elements of input matrix:");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                scanf("%d",&a[i][j]);
                }
                printf("the input matrix is:");
                printf("\n");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                {
                                                if(i==j)
                                                                s=s+a[i][j];
                                }
                }
                printf("\n the trace is=%f\n",s);
}
RESULT:
enter the elements of input matrix:1 2 3 4
the input matrix is:
1       2
3       4

 the trace is=5.000000
Press any key to continue

Saturday, 17 August 2019

C program for inverse of a matrix


/*Inverse of a matrix*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i,j,det,k,a[20][20];
                float I[20][20],U[20][20];
                printf("enter the elements of input matrix:");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                scanf("%d",&a[i][j]);
                }
                printf("the input matrix is:");
                printf("\n");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                /*matrix determinant*/
                det=(a[0][0]*a[1][1])-(a[0][1]*a[1][0]);
                printf("\n The determinant is=%d\n",det);
                /*matrix inverse*/
                if(det==0)
                                printf("The matrix inverse is not possible:");
                else
                {
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                {
                                                                if(i==j)
                                                                {
                                                                                I[i][j]=(a[(i+1)%2][(j+1)%2]);
                                                                }
                                                                else
                                                                {
                                                                                I[i][j]=-(a[i][j]);
                                                                }
                                                }
                                }
                                printf("The inverse matrix is \n");
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                                printf("%.4f\t",(I[i][j]/det));
                                                printf("\n");
                                }
                                /*product matrix*/
                                for(i=0;i<2;i++)
                                {for(j=0;j<2;j++)
                               
                                {
                                                U[i][j]=0;
                                                for(k=0;k<2;k++)
                                                                U[i][j]=U[i][j]+a[i][k]*I[k][j];
                                }
                                }
                                printf("\n the product matrix is:\n");
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                                printf("%.4f\t",(U[i][j]/det));
                                                printf("\n");
                                }
                }
}
RESULT 1:
enter the elements of input matrix:1 2 3 4
the input matrix is:
1       2
3       4
 The determinant is=-2
The inverse matrix is
-2.0000 1.0000
1.5000  -0.5000
 the product matrix is:
1.0000  0.0000
0.0000  1.0000
Press any key to continue
RESULT 2:
enter the elements of input matrix:1 1 1 1
the input matrix is:
1       1
1       1
 The determinant is=0
The matrix inverse is not possible:
Press any key to continue

Thursday, 15 August 2019

C programming for multiplication of two matrices

/*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 elements 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 elements 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("multiplication of the matrix is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<p;j++)
                                                printf("%d\t",c[i][j]);
                                    printf("\n");
                        }
}