Monday, April 3, 2023

Write a C++ Program to Multiply Two Matrix.

C++ Program to Multiply Two Matrices Write a C++ Program to Multiply Two Matrix.

In linear algebra, the multiplication of two matrices is defined as a way to combine the rows and columns of the matrices to obtain a new matrix. The multiplication of two matrices can be performed only if the number of columns of the first matrix is equal to the number of rows of the second matrix.

Let's assume we have two matrices A and B with dimensions m x n and n x p respectively. The product of matrices A and B, denoted as C, is defined as a new matrix with dimensions m x p. The elements of the resultant matrix C are obtained by multiplying the elements of each row of matrix A with the corresponding elements of each column of matrix B and adding up the products. The general formula for the product of matrices A and B is:

C(i,j) = ∑(k=1 to n) A(i,k) * B(k,j)

where C(i,j) denotes the element in the i-th row and j-th column of the resultant matrix C.

To compute the product of matrices A and B, we can use nested loops. The outer loop iterates over the rows of matrix A, the inner loop iterates over the columns of matrix B, and the innermost loop iterates over the common dimension of the matrices (i.e., n).

Here's an example of how to multiply two matrices A and B:

A = [1 2 3      B = [7 8

       4 5 6]            9 10

                            11 12]


C = [1*7 + 2*9 + 3*11  1*8 + 2*10 + 3*12

     4*7 + 5*9 + 6*11  4*8 + 5*10 + 6*12]


C = [58  64

     139 154]


In the above example, the product of matrices A and B is a new matrix C with dimensions 2 x 2. The first element of C, C(1,1), is obtained by multiplying the elements of the first row of A with the corresponding elements of the first column of B and adding up the products. Similarly, the second element of C, C(1,2), is obtained by multiplying the elements of the first row of A with the corresponding elements of the second column of B and adding up the products. The same process is followed to compute the other elements of C.
Let’s write a C++ program on it.


#include <iostream>
using namespace std;

int main()
{
    int m, n, p, q, i, j, k;
    cout << "Enter the number of rows and columns of the first matrix: ";
    cin >> m >> n;
    cout << "Enter the number of rows and columns of the second matrix: ";
    cin >> p >> q;
    if (n != p)
    {
        cout << "Matrices cannot be multiplied.";
        return 0;
    }
    int a[m][n], b[p][q], c[m][q];
    cout << "Enter the elements of the first matrix:\n";
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            cin >> a[i][j];
        }
    }
    cout << "Enter the elements of the second matrix:\n";
    for (i = 0; i < p; i++)
    {
        for (j = 0; j < q; j++)
        {
            cin >> b[i][j];
        }
    }
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < q; j++)
        {
            c[i][j] = 0;
            for (k = 0; k < n; k++)
            {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    cout << "Resultant matrix:\n";
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < q; j++)
        {
            cout << c[i][j] << " ";
        }
        cout << "\n";
    }
    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 the elements of the first matrix:
1 2 3
4 5 6
Enter the elements of the second matrix:
7 8
9 10
11 12
Resultant matrix:
58  64  
139 154 

The program works as follows:

  1. It prompts the user to input the dimensions of the first matrix and the second matrix (number of rows and columns).
  2. It checks if the matrices can be multiplied by comparing the number of columns of the first matrix with the number of rows of the second matrix.
  3. It prompts the user to input the elements of the first matrix and the second matrix.
  4. It multiplies the matrices using nested loops and stores the result in another matrix.
  5. It prints the resultant matrix.

Notes:

  • The program uses the iostream library for input and output operations.
  • The using namespace std; statement allows us to use standard C++ functions and objects without having to prefix them with std::.
  • The program uses integer data type for the matrices and the elements, but the data type can be changed to float or double if needed.
  • The program uses a two-dimensional array to store the elements of the matrices and the resultant matrix.
  • The nested loops are used to iterate over the rows and columns of the matrices and perform the multiplication operation.
  • The program uses the cout statement to print output to the console.
  • The return 0; statement at the end of the main() function is used to indicate that the program has executed successfully.

Overall, this program provides a basic understanding of how to multiply two matrices using nested loops in C++. It can be further modified or improved based on specific requirements.

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...