C program which perform operation on matrix ,operation such as addition ,subtraction,multiplication,transpose etc.This is a menu driven program where user can enter their choice and perform various mathematical operation.
C Program to perform operation on matrices
#include <stdio.h> #include <conio.h> #define MAX 3 void create ( int [3][3] ) ; void display ( int [3][3] ) ; void matadd ( int [3][3], int [3][3], int [3][3] ) ; void matsub ( int [3][3], int [3][3], int [3][3] ) ; void matmul ( int [3][3], int [3][3], int [3][3] ) ; void upper(int m1[3][3]) ; void lower(int m1[3][3]) ; void transpose ( int [3][3], int [3][3] ) ; void symmetry (int [3][3]); void trace (int [3][3]); void main( ) { int mat1[3][3], mat2[3][3], mat3[3][3], mat4[3][3], mat5[3][3], mat6[3][3],ch ; clrscr( ) ; while(1) { printf("nnEnter your choice:n"); printf("1. Additionn2. Subtractionn3. Multiplicationn4. Upper Matrixn"); printf("5. Tracen6. Transposen7. Symmetryn8.Lower n9. EXITn"); scanf("%d",&ch); if(ch==1||ch==2||ch==3) { printf ( "nEnter elements for first array: nn" ) ; create ( mat1 ) ; printf ( "nEnter elements for second array: nn" ) ; create ( mat2 ) ; printf ( "nFirst Matrix: n" ) ; display ( mat1 ) ; printf ( "nSecond Matrix:n" ) ; display ( mat2 ) ; } if(ch==9) exit(1); if(ch==5||ch==7||ch==8||ch==4||ch==6) { printf ( "nEnter elements for first array: nn" ) ; create ( mat1 ) ; printf ( "nFirst Matrix: n" ) ; display ( mat1 ) ; } switch(ch) { case 1: matadd ( mat1, mat2, mat3 ) ; printf ( "nAfter Addition: n" ) ; display ( mat3 ) ; break; case 2: matsub ( mat1, mat2, mat6 ) ; printf ( "nAfter Substraction: n" ) ; display ( mat6 ) ; break; case 3: matmul ( mat1, mat2, mat4 ) ; printf ( "nAfter Multiplication: n" ) ; display ( mat4 ) ; break; case 4: printf("nChecking Matrix is Upper triangular or not"); upper(mat1); break; case 5: printf("nnFinding Trace of Matrix "); trace (mat1); break; case 6: transpose ( mat1, mat5 ) ; printf ( "nnTranspose of first matrix: n" ) ; display ( mat5 ) ; break; case 7: printf("nnChecking Matrix is Symmetric or not"); symmetry(mat1); break; case 8: printf("nnChecking Matrix is Lower triangular or not"); lower(mat1); break; case 9: exit(0); default: printf("Wrong Choicen"); } } getch( ) ; } /* creates matrix mat */ void create ( int mat[3][3] ) { int i, j ; printf ( "nEnter the element: " ) ; for ( i = 0 ; i < MAX ; i++ ) { for ( j = 0 ; j < MAX ; j++ ) { scanf ( "%d", &mat[i][j] ) ; } } } /* displays the contents of matrix */ void display ( int mat[3][3] ) { int i, j ; for ( i = 0 ; i < MAX ; i++ ) { for ( j = 0 ; j < MAX ; j++ ) printf ( "%dt", mat[i][j] ) ; printf ( "n" ) ; } } /* adds two matrices m1 and m2 */ void matadd ( int m1[3][3], int m2[3][3], int m3[3][3] ) { int i, j ; for ( i = 0 ; i < MAX ; i++ ) { for ( j = 0 ; j < MAX ; j++ ) m3[i][j] = m1[i][j] + m2[i][j] ; } } /* subtract two matrices m1 and m2 */ void matsub ( int m1[3][3], int m2[3][3], int m3[3][3] ) { int i, j ; for ( i = 0 ; i < MAX ; i++ ) { for ( j = 0 ; j < MAX ; j++ ) m3[i][j] = m1[i][j] - m2[i][j] ; } } /* multiplies two matrices m1 and m2 */ void matmul ( int m1[3][3], int m2[3][3], int m3[3][3] ) { int i, j, k ; for ( k = 0 ; k < MAX ; k++ ) { for ( i = 0 ; i < MAX ; i++ ) { m3[k][i] = 0 ; for ( j = 0 ; j < MAX ; j++ ) m3[k][i] += m1[k][j] * m2[j][i] ; } } } /* obtains transpose of matrix m1 */ void transpose ( int m1[3][3], int m2[3][3] ) { int i, j ; for ( i = 0 ; i < MAX ; i++ ) { for ( j = 0 ; j < MAX ; j++ ) m2[i][j] = m1[j][i] ; } } // Function for finding Upper Triangular Matrix void upper(int m1[3][3]) { int i,j; int count = 0; for(i = 0; i <= 2; i++) { for(j = 0; j <= 2; j++) { if(i <= j) { if(m1[i][j] == 0) count++; } } } if(count == 3) printf("nnThe Matrix is upper triangularn"); else printf("nnThe Matrix is not upper triangularn"); } // Function for finding Lower Triangular Matrix void lower(int m1[3][3]) { int count = 0; int i,j; for(i = 0; i <= 2; i++) { for(j = 0; j <= 2; j++) { if(i >= j) { if(m1[i][j] == 0) count++; } } } if(count == 3) printf("nnThe Matrix is lower triangularn"); else printf("nnThe Matrix is not lower triangularn"); } //check symmetry of matrix void symmetry(int m1[3][3]) { int count=0; int mt[3][3]; int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { mt[j][i] = m1[i][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(m1[i][j] == mt[i][j]) { count++; } else { printf("nnMATRIX IS NOT SYMMETRIC"); } } } if(count==9) printf("nnMATRIX IS SYMMETRIC"); } //find trace of matrix void trace (int m1[3][3]) { int i,j,r,c,sum=0; printf("nnEnter row size"); scanf("%d",&r); printf("nnEnter coloumn size"); scanf("%d",&c); if (r==c) { for (i=0;i<r;i++) { sum=sum+m1[i][i]; } printf("nTrace of the MATRIX is %d",sum); } else { printf("nENTER SQUARE MATRIX"); } }
Output