An C program to Multiply two matrices (matrix)

By | August 30, 2013

Let’s identify variables needed for this program.

In this program, we need to save matrices which consists Rows and Columns. To save this we need Two dimensional ARRAY.

Multi dimensional Array is a nothing different than any Array but the only difference is that it has more than one deminsion to it e.g. square has two dimension and cube has three dimension. The Dimension of array is decided by us in number of square brackets [] selected. If We select two dimension than we have to take two square brackets[][]. It has to be accessed with the help of index number ranging from 0 to n-1 and 0 to m-1. (e.g. num[n][m] will have num[0][0],num[0][1],num[2][2] so on.) 

First few variables will be the one which will save the values entered by the user and it will be array A[3][3] and B[3][3]. Here we are taking 3 x 3 matrices hence taking number 3 in both the square brackets. Next variable will be array C[3][3] for saving resultant value which will be displayed later. other variables will be I, J and K which will be for FOR Loop so in all Five variables selected.
The identified variables are A[3][3],B[3][3],C[3][3],I,J,K;

int A[3][3],B[3][3],C[3][3],I,J,K;

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of accessing the two dimension array A[0][0],A[..][..],..A[2][2]. Here we have the index values ranging from zero to two (i.e. n-1) for ROWS and index values ranging from zero to two (i.e. n-1) for COLUMNS . Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to 2. Now here we need nested loop one loop inside the other. So that we can access all the Nine elements with it. We will need four nested loop,that is  in all eight FOR loops. First two nested loops for scanning required array values of Two matrices. Next nested loop will be nested with three FOR loop for multiplication of matrices and save it in the third empty matrix. and fourth nested loop for printing resultant array values as output.

This let’s you understand nested loop

for(I=0;I<3;I++)
    {
        for(J=0;J<3;J++)
        {
            scanf("%d",&A[I][J]);
        }
    }

Multiplication of two matrices is a bit complex as we have to do dot product of first row with first column multiply and add there product all the elements of the first matrix with row and other matrix with column. The matrices can only be added if the column of first matrix is equal to row of the second. To known more please click here 

for(I=0;I<3;I++)
    {
        for(J=0;J<3;J++)
        {
            C[I][J]=0;
            for(K=0;K<3;K++)
            {
                C[I][J]=C[I][J]+A[I][K]*B[K][J];
            }
        }
    }

Finally print the Numbers of Two Dimension Resultant Array in the Matrix form by using Tab (\t) and New Line (\n) character in the nested Loop at the end of the program.

for(I=0;I<3;I++)
    {
        for(J=0;J<3;J++)
        {
            printf("%d\t",C[I][J]);
        }
        printf("\n");
    }

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int A[3][3],B[3][3],C[3][3],I,J,K;
  5.     clrscr();
  6.     printf("ENTER 3X3 MATRIX A VALUES\n");
  7.     for(I=0;I<3;I++)
  8.     {
  9.         for(J=0;J<3;J++)
  10.         {
  11.             scanf("%d",&A[I][J]);
  12.         }
  13.     }
  14.     printf("ENTER 3X3 MATRIX B VALUES\n");
  15.     for(I=0;I<3;I++)
  16.     {
  17.         for(J=0;J<3;J++)
  18.         {
  19.             scanf("%d",&B[I][J]);
  20.         }
  21.     }
  22.     for(I=0;I<3;I++)
  23.     {
  24.         for(J=0;J<3;J++)
  25.         {
  26.             C[I][J]=0;
  27.             for(K=0;K<3;K++)
  28.             {
  29.                 C[I][J]=C[I][J]+A[I][K]*B[K][J];
  30.             }
  31.         }
  32.     }
  33.     printf("RESULT 3X3 MATRIX C VALUES ARE :\n");
  34.     for(I=0;I<3;I++)
  35.     {
  36.         for(J=0;J<3;J++)
  37.         {
  38.             printf("%d\t",C[I][J]);
  39.         }
  40.         printf("\n");
  41.     }
  42.     getch();
  43. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.

SCREEN SHOTS:-

C_program_Matrix_Multiplication

C_program_Matrix_Multiplication_Output

Leave a Reply