Computer Science, asked by Ayaan111, 1 year ago

writa a program to multiply two matrices.

Answers

Answered by Agkc
0
Matrix multiplication in c language

#include

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);

if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter 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];
}

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

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}
Answered by shikhalodhi253
0

Answer:

Explanation:

/*

Matrix multiplication of two matrices taking no. of columns, no. of rows and no. of elements of both matrices from user

*/

#include<stdio.h>

#define mat 50                           // Macro definition

int main(){

   int a[mat][mat], b[mat][mat], product[mat][mat];

   int a_rows, a_columns;

    int b_rows, b_columns;

    int i, j, k;

    int sum = 0;

   printf("Enter the no. of rows and no. of columns of Matrix A\n ");

   scanf("%d",&a_rows);

   scanf("%d",&a_columns);

   printf("Enter the no. of elements of matrix A \n");

   for(i=0; i<a_rows; i++){

       for ( j=0; j<a_columns; j++){

           scanf("%d",&a[i][j]);

       }

   }

   printf("Enter the no. of rows and no. of columns of Matrix B\n");

   scanf("%d %d",&b_rows,&b_columns);

   if( a_columns != b_rows){

       printf("sorry! we cannot multiply these matrices");

   }

   else{

       printf("Enter the no. of number of elements of matrix B\n");

   for( i=0; i<b_rows; i++){

       for ( j=0; j<b_columns; j++){

           scanf("%d",&b[i][j]);

       }

   }

   printf("\n");

   for(i=0; i<a_rows; i++){

       for( j=0; j<b_columns; j++){

           for( k=0; k<a_columns; k++){

                     sum += a[i][k] * b[k][j];

                     }

                     product[i][j] = sum;  

                     sum = 0;      

           

       }

   }

   printf("resultant matrix\n");

   for(i=0; i<a_rows; i++){

       for( j=0; j<b_columns; j++){

           printf(" %d ",product[i][j]);

       }

       printf("\n");

   }}

   return 0;

}

Similar questions