c program to multiply two matrices

devquora
devquora

Posted On: Jan 27, 2023

c program to multiply two matrices

 

Overview:

First of all in this article, I will introduce you with the mathematical concept Matrix and Arithmatical operation Multiplication. After that I will explain What is an array? and What is Multidimensional Array? Moreover, I will show Demonstration for multiplication of matrices. At last, I will show a C program to multiply two matrices with output in front of you.

Table of contents:

  1. What is Matrix?
  2. What is Multiplication?
  3. What is an Array?
  4. What is multi dimensional array?
  5. Demonstration for multiplication of matrices.
  6. C program to multiply two matrices
  7. Conclusion

What is Matrix?

A matrix is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns. The matrix of m rows and n column is known as m X n matrix. Matrices are often denoted using capital letters such as A, B and C. For example, a 3 X 3 matrix is looks like as follows:

MatrixDemo

What is Multiplication?

Multiplication is the process of multiplying two numbers or matrices. The multiplication of two matrices A and B is denoted as A * B.

What is an Array?

An array is a container for the collection of items that stores these items at contiguous memory locations. An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it.

What is multi dimensional array?

Multidimensional array is an array of arrays. You can declare a two-dimensional (3d) array. For example, float x[3][4]; Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3];

Demonstration for multiplication of matrices.

Demo for multiplication of Matrix

C program to multiply two matrices

#include <stdio.h>

#define SIZE 3 // Size of the matrix

int main()
{
    int A[SIZE][SIZE]; // Matrix 1 
    int B[SIZE][SIZE]; // Matrix 2
    int C[SIZE][SIZE]; // Resultant matrix
    
    int row, col, i, sum;


    /* Input elements in first matrix from user */
    printf("Enter elements in matrix A of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix from user */
    printf("\nEnter elements in matrix B of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /*
     * Multiply both matrices A*B
     */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            sum = 0;
            /*
             * Multiply row of first matrix to column of second matrix
             * and store sum of product of elements in sum.
             */
            for(i=0; i<SIZE; i++)
            {
                sum += A[row][i] * B[i][col];
            }

            C[row][col] = sum;
        }
    }

    /* Print product of the matrices */
    printf("\nProduct of matrix A * B = \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            printf("%d ", C[row][col]);
        }
        printf("\n");
    }

    return 0;
}

Matrices Multiplication

Conclusion:

This Program multiply the elements of the two 3 x 3  matrices.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..