Multiplication of matrix

To find the product of two matrices.here we use arrays to define a matrix size and then using loops the multiplication prosedure is done for the matrices.

Inputs to the script

User has to enter matrix size and elements.

Conditions of multipication

Number of columns of first matrix must be equal to number of rows of second matrix.
col1 must be equal to row2

Output of the script

We will get product of the matrix in matrix form. Product matrix will have same number of rows of first matrix and same number of columns of second matrix.
result[row1][col2]  = matrix1[row1][col1] x matrix2[row2][col2]
example
result[2][3]  = matrix1[2][4] x matrix2[4][3]

Example 1

#include <stdio.h>
int main(void){
int i,j,k,sum;
int num1[2][3]={
                {1,2,3},
                {4,5,6}              
                };
int num2[3][2]={
                {7,8},
                {9,10},
		{11,12}              
                };                

int result[2][2]; 
for(k=0;k<2;k++){              
for(i=0;i<2;i++){
sum=0;
for(j=0;j<3;j++){
sum = sum + num1[k][j]*num2[j][i];
}
printf("%d ",sum);
}
printf("\n");
}
return 0;
}
Output
58 64
139 154

Example 2

This code will take two (any size) 2d matrices and return the result in matrix form.
#include <stdio.h>
 
int main()
{
  int r1, c1, r2, c2, i, j,a,sum = 0;
// r1 is number of rows and c1 is number of columns in 1st matrix
// r2 is number of rows and c2 is number of columns in 2nd matrix 
// Declare matrix ///
  int first[10][10], second[10][10], multiply[10][10];
 
  printf("Enter number of rows and columns of first matrix\n");
  scanf("%d%d", &r1, &c1);
  printf("Enter elements of first matrix\n");
 
  for (i = 0; i < r1; i++)
    for (j = 0; j < c1; j++)
      scanf("%d", &first[i][j]);
 
  printf("Enter number of rows and columns of second matrix\n");
  scanf("%d%d", &r2, &c2);
 
  if (c1!= r2)
    printf("The matrices can't be multiplied with each other.\n");
  else
  {
    printf("Enter elements of second matrix\n");
 
    for (i = 0; i < r2; i++)
      for (j = 0; j < c2; j++)
        scanf("%d", &second[i][j]);
 // Entry is over now multiplication of two matrices
 
    for (i = 0; i < r1; i++) {
      for (j = 0; j < c2; j++) {
        for (a = 0; a < r2; a++) {
          sum = sum + first[i][a]*second[a][j];
        }
 
        multiply[i][j] = sum;
        sum = 0;
      }
    }
    
// display the first , then second and then product matrix //
// display first martrix //
printf("First matrix \n");
   for (i = 0; i < r1; i++){
    for (j = 0; j < c1; j++){
      printf("%d ", first[i][j]);
	}
	printf("\n");
	}
printf("Second  matrix \n");
   for (i = 0; i < r2; i++){
    for (j = 0; j < c2; j++){
      printf("%d ", second[i][j]);
	}
	printf("\n");
	}
// display product matrix //	
    printf("Product of the matrices:\n");
     for (i = 0; i < r1; i++) {
      for (j = 0; j < c2; j++)
        printf("%d ", multiply[i][j]);
      printf("\n");
    }
  }
 
  return 0;
}
Addition & Subtraction of Matrix Two dimensional Array in C

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer