Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Multiplication of two Matrices

Matrix multiplication is a fundamental operation in linear algebra and is used in many fields, including computer science. If you're working with matrices in programming languages like C, C++, Python, or Java, you'll likely need to multiply them at some point. In C and C++, you can use loops to perform the calculation, while in Python and Java, built-in functions are available like numpy.dot() and ArrayMultiplicator.multiply(). The key thing to keep in mind is that the dimensions of the matrices need to match up for multiplication to work - specifically, the number of columns in the first matrix must be equal to the number of rows in the second matrix. Once you've got that sorted out, multiplying matrices becomes just another task on your programming checklist!

(a.) C Code

#include <stdio.h>

void multiplyMatrices(int mat1[][3], int mat2[][2], int result[][2], int r1, int c1, int r2, int c2) {
  if (c1 != r2) {
      printf("Matrix multiplication not possible.
");
      return;
  }
  for (int i = 0; i < r1; i++) {
      for (int j = 0; j < c2; j++) {
          result[i][j] = 0;
          for (int k = 0; k < c1; k++) {
              result[i][j] += mat1[i][k] * mat2[k][j];
          }
      }
  }
}

int main() {
  int mat1[2][3] = {{1, 2, 3}, {4, 5, 6}};
  int mat2[3][2] = {{7, 8}, {9, 10}, {11, 12}};
  int result[2][2];
  int r1 = 2, c1 = 3, r2 = 3, c2 = 2;
  multiplyMatrices(mat1, mat2, result, r1, c1, r2, c2);
  printf("Resultant Matrix: 
");
  for (int i = 0; i < r1; i++) {
      for (int j = 0; j < c2; j++) {
          printf("%d ", result[i][j]);
      }
      printf("
");
  }
  return 0;
}
Output:-
Enter the number of rows and columns of the first matrix: 2 3
Enter the number of rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Resultant Matrix:
58 64
139 154

(b.) C++ Code


using namespace std;

void multiplyMatrices(int mat1[][3], int mat2[][2], int result[][2], int r1, int c1, int r2, int c2) {
  if (c1 != r2) {
      cout << "Matrix multiplication not possible." << endl;
      return;
  }
  for (int i = 0; i < r1; i++) {
      for (int j = 0; j < c2; j++) {
          result[i][j] = 0;
          for (int k = 0; k < c1; k++) {
              result[i][j] += mat1[i][k] * mat2[k][j];
          }
      }
  }
}

int main() {
  int mat1[2][3] = {{1, 2, 3}, {4, 5, 6}};
  int mat2[3][2] = {{7, 8}, {9, 10}, {11, 12}};
  int result[2][2];
  int r1 = 2, c1 = 3, r2 = 3, c2 = 2;
  multiplyMatrices(mat1, mat2, result, r1, c1, r2, c2);
  cout << "Resultant Matrix: " << endl;
  for (int i = 0; i < r1; i++) {
      for (int j = 0; j < c2; j++) {
          cout << result[i][j] << " ";
      }
      cout << endl;
  }
  return 0;
}
Output:-
Enter the number of rows and columns of the first matrix: 2 3
Enter the number of rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Resultant Matrix:
58 64
139 154

(c.) Python Code

def multiply_matrices(mat1, mat2):
    result = [[0 for _ in range(len(mat2[0]))] for _ in range(len(mat1))]
    for i in range(len(mat1)):
        for j in range(len(mat2[0])):
            for k in range(len(mat2)):
                result[i][j] += mat1[i][k] * mat2[k][j]
    return result
        
if __name__ == "__main__":
    mat1 = [[1, 2, 3], [4, 5, 6]]
    mat2 = [[7, 8], [9, 10], [11, 12]]
    result = multiply_matrices(mat1, mat2)
    print("Resultant Matrix:")
    for row in result:
        print(" ".join(map(str, row)))
Enter the number of rows and columns of the first matrix: 2 3
Enter the number of rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Resultant Matrix:
58 64
139 154

(d.) JavaScript Code

function multiplyMatrices(mat1, mat2) {
        let result = [];
        for (let i = 0; i < mat1.length; i++) {
            result[i] = [];
            for (let j = 0; j < mat2[0].length; j++) {
                let sum = 0;
                for (let k = 0; k < mat1[0].length; k++) {
                    sum += mat1[i][k] * mat2[k][j];
                }
                result[i][j] = sum;
            }
        }
        return result;
    }
    
    const mat1 = [[1, 2, 3], [4, 5, 6]];
    const mat2 = [[7, 8], [9, 10], [11, 12]];
    const result = multiplyMatrices(mat1, mat2);
    
    console.log("Resultant Matrix:");
    for (let i = 0; i < result.length; i++) {
        console.log(result[i].join(" "));
    }
Enter the number of rows and columns of the first matrix: 2 3
Enter the number of rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Resultant Matrix:
58 64
139 154