Write a C++ Program to Multiply two Matrices by Passing Matrix to Function

Multiplying two matrices is a common task in linear algebra and is frequently encountered in various scientific and engineering applications.

In this article, we will explore how to write a C++ program to multiply two matrices by passing matrices to a function.

To start, let’s understand the basics of matrix multiplication.

When we multiply two matrices A and B, the resulting matrix C is obtained by taking the dot product of each row of matrix A with each column of matrix B.

The resulting element in the i-th row and j-th column of matrix C is obtained by summing the products of the i-th row of matrix A with the j-th column of matrix B. In mathematical notation, this can be represented as:

C_ij = Σ(A_ik * B_kj) for k = 1 to n

Where n is the number of columns in matrix A and the number of rows in matrix B.

Now, let’s move on to writing the program in C++.

The first step is to declare a function that takes the two matrices A and B as inputs and returns the resulting matrix C.

Here is an example of how this can be done:

#include <iostream>
using namespace std;

const int MAX = 100;

void matrixMultiplication(int a[][MAX], int b[][MAX], int c[][MAX], int n, int m, int p) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < p; j++) {
            c[i][j] = 0;
            for (int k = 0; k < m; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
}

In this function, we have used a nested loop structure to calculate each element of the resulting matrix C.

The first two loops are used to iterate over the rows and columns of matrix C, while the innermost loop calculates the dot product of each row of matrix A with each column of matrix B.

Now that we have defined the matrix multiplication function, we can call it from the main function and pass the matrices A and B as arguments.

Here is an example of how this can be done:

int main() {
    int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
    int n, m, p;

    cout << "Enter the number of rows and columns of matrix A: ";
    cin >> n >> m;

    cout << "Enter the elements of matrix A: ";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> A[i][j];
        }
    }

    cout << "Enter the number of rows and columns of matrix B: ";
    cin >> m >> p;

    cout << "Enter the elements of matrix B: ";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < p; j++) {
            cin >> B[i][j];
        }
    }

    matrixMultiplication(A, B, C, n, m, p);

    cout << "Resulting matrix C: " << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < p; j++) {
            cout << C[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

In the main function, we first declare the matrices A, B, and C and read in the dimensions and elements of matrices A and B from the user.

Next, we call the matrix multiplication function with the matrices A and B and store the resulting matrix C in a separate matrix. Finally, we print out the resulting matrix C.

It’s worth noting that the dimensions of the matrices A and B must be compatible for multiplication.

Specifically, the number of columns in matrix A must be equal to the number of rows in matrix B.

If the dimensions are not compatible, the program will not run correctly and may produce unexpected results.

In conclusion, we have covered how to write a C++ program to multiply two matrices by passing matrices to a function.

By understanding the basics of matrix multiplication and using nested loops, we were able to implement a function that calculates each element of the resulting matrix C.

We then demonstrated how to call this function from the main function and pass in the matrices A and B as arguments.

With this knowledge, you should be able to apply matrix multiplication to a wide range of scientific and engineering applications in C++.