Write a C Program for multiplication of two matrix using array

Multiplication of two matrix using array


Write a C Program for multiplication of two matrix using array. Here’s simple Program to multiply two matrix using array in C Programming Language.


What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

  • type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


Below is the source code for C Program for multiplication of two matrix using array which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program for multiplication of two matrix using array  */

#include <stdio.h>
 
int main()
{
    int A[3][3], B[3][3], C[3][3];
    int row, col, i, sum;
 
    /*
     * Reads elements in first matrix from user
     */
    printf("Enter elements in matrix A of size 3x3: \n");
    for(row=0; row<3; row++)
    {
        for(col=0; col<3; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }
 
    /*
     * Reads elements in second matrix from user
     */
    printf("\nEnter elements in matrix B of size 3x3: \n");
    for(row=0; row<3; row++)
    {
        for(col=0; col<3; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }
 
    /*
     * Multiplies both matrices A*B
     */
    for(row=0; row<3; row++)
    {
        for(col=0; col<3; col++)
        {
            sum = 0;
            /*
             * Multiplies row of first matrix to column of second matrix
             * And stores the sum of product of elements in sum.
             */
            for(i=0; i<3; i++)
            {
                sum += A[row][i] * B[i][col];
            }
 
            C[row][col] = sum;
        }
    }
 
    /*
     * Prints the product of matrices
     */
    printf("\nProduct of Matrix A * B = \n");
    for(row=0; row<3; row++)
    {
        for(col=0; col<3; col++)
        {
            printf("%d ", C[row][col]);
        }
        printf("\n");
    }
 
    return 0;
}

Output : :


/*  C Program for multiplication of two matrix using array  */

Enter elements in matrix A of size 3x3: 
1 2 3 
4 5 6 
7 8 9 

Enter elements in matrix B of size 3x3: 
9 8 7 
6 5 4 
3 2 1 

Product of A * B = 
30 24 18 
84 69 54 
138 114 90

Above is the source code for C Program for multiplication of two matrix using array which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply