2d array

1. Given two matrices of order M1*N1 and M2*N2. Write function to perform matrix multiplication and display the resultant matrix if matrix multiplication is possible otherwise display the message "Invalid Matrices". 


Sol: #include int main() 


int m, n, p, q, c, d, k, sum = 0; 


 int first[10][10], second[10][10], z[10][10];

 printf("\nEnter the number of rows and columns of first matrix:\n"); 

 scanf("%d%d", &m, &n); 

 printf("\nEnter the number of rows and columns of second matrix:\n"); 

 scanf("%d%d", &p, &q);

 if ( n != p )

 {

 printf("\nMatrices with entered orders can't be multiplied with each other.\n"); 

 printf("\nThe column of first matrix should be equal to row of second.\n"); 

 }

 else 

 { 

 printf("\nEnter the elements of first matrix:\n"); 

 for ( c = 0 ; c < m ; c++ ) 

 for ( d = 0 ; d < n ; d++ )

 scanf("%d", &first[c][d]); 

 printf("\nEnter the elements of second matrix:\n");

 for ( c = 0 ; c < p ; c++ ) 

 for ( d = 0 ; d < q ; d++ ) 

 scanf("%d", &second[c][d]); 

 for ( c = 0 ; c < m ; c++ ) 

 { 

 for ( d = 0 ; d < q ; d++ ) 

 { 

 for ( k = 0 ; k < p ; k++ ) 

 { 

 sum = sum + first[c][k]*second[k][d]

 }

 z[c][d] = sum; sum = 0; }

 }

 printf("\nThe matrix after multiplication is : \n"); 

 for ( c = 0 ; c < m ; c++ )

 { for ( d = 0 ; d < q ; d++ )

 printf("%d\t", z[c][d]); printf("\n"); 

 }

 }

 return 0; 

} 












2. Develop a program to calculate XA+YB where A and B are the matrices and X=2 and Y=3 



Sol:



 #include int main() { float a[2][2], b[2][2], result[2][2]; printf("Enter elements of 1st matrix\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter a%d%d: ", i + 1, j + 1); scanf("%f", &a[i][j]); } printf("Enter elements of 2nd matrix\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter b%d%d: ", i + 1, j + 1); scanf("%f", &b[i][j]); } for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { result[i][j] = a[i][j] + b[i][j]; } printf("\nSum Of Matrix:"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("%.1f\t", result[i][j]); if (j == 1) printf("\n"); } return 0; } 




3.

A square matrix is said to be symmetric if transpose of a matrix is equal to the original matrix. Develop a C program to check whether the given square matrix is symmetric or not?


 Sol: 


#include int main() { int i, j, rows, columns, a[10][10], b[10][10], Count = 1; printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); printf("\n Please Enter the Matrix Elements \n"); for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j;columns++) { scanf("%d", &a[rows][columns]); } } for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j; columns++) { b[columns][rows] = a[rows][columns]; } } for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { if(a[rows][columns] != b[rows][columns]) { Count++; break; } } } if(Count == 1) { printf("\n The Given Matrix is a Symmetric Matrix "); } else { printf("\n The Given Matrix is Not a Symmetric Matrix "); } return 0; }

Comments

Popular posts from this blog

1d array

strings