Multiply Matrix and Multidimensional Array Operation

We have seen our previous post how to declare multidimensional arrays. Here we play with multidimensional arrays  like Sum of Matrix, Multiply of Matrix etc. We have already programmed sum and average of single dimension array in ours previous post. As we discuss earlier, more than 3 dimension arrays are difficult to imagine as well as implementation and execution. That’s why we first use two dimension array as for example.

C Program to store values into two-dimensional array:

 // C program to store marks of 3 year Semester and display it.
#include <stdio.h>



int main()
{
 int YEAR = 3;
 int SEM = 2;
 int marks[3][2];
 int i,j;
 clrscr();
 for (i = 0; i < YEAR; ++i) {
    for(j = 0; j < SEM; ++j) {
      printf("Year %d, Semester %d: ", i+1, j+1);
      scanf("%d", &marks[i][j]);
    }
 }

 printf("\nDisplaying values: \n\n");
 for ( i = 0; i < YEAR; ++i) {
    for( j = 0; j < SEM; ++j)
    {
       printf(" You have enter %d Year, %d Semester marks = %d\n", i+1, j+1, marks[i][j]);
    }
 }
 getch();
 return 0;
}

Output:

Multiply Matrix

C Program to Calculate sum of 2×2 Matrix:

#include <stdio.h>
int main()
{
    float a[2][2], b[2][2], c[2][2];
    int i, j;

    // Storing Values into matrix a using nested for loop
    clrscr();
    printf("Enter elements of 1st matrix\n");
    for(i=0; i<2; ++i)
       for(j=0; j<2; ++j)
       {
          printf("Enter a%d%d: ", i+1, j+1);
          scanf("%f", &a[i][j]);
       }

    // Storing Values into matrix b using nested for loop
    printf("Enter elements of 2nd matrix\n");
    for(i=0; i<2; ++i)
       for(j=0; j<2; ++j)
       {
          printf("Enter b%d%d: ", i+1, j+1);
          scanf("%f", &b[i][j]);
       }

    // adding corresponding elements of two arrays into third array c
    for(i=0; i<2; ++i)
       for(j=0; j<2; ++j)
       {
          c[i][j] = a[i][j] + b[i][j]; 
       }

    // Displaying the sum
    printf("\nSum Of Matrix:\n");

    for(i=0; i<2; ++i)
       for(j=0; j<2; ++j)
       {
          printf("%.1f\t", c[i][j]); 
 
          if(j==1) 
          printf("\n");
       }
getch();
    return 0;
}

Output:

sum

C Program to Calculate Multiplay of 2×2 Matrix:

An n x m matrix A and m x p matrix B can be multiplied together giving a n x p matrix C. It is important that the first matrix (A) has the same number of columns as the second (B) has rows.

To know more details click here

 #include <stdio.h>

int main()
{
 int a[2][2], b[2][2], result[2][2], i, j, k;

 
 // Storing elements of first matrix.
 printf("\nEnter elements of matrix 1:\n");
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 {
 printf("Enter elements a%d%d: ",i+1, j+1);
 scanf("%d", &a[i][j]);
 }

 // Storing elements of second matrix.
 printf("\nEnter elements of matrix 2:\n");
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 {
 printf("Enter elements b%d%d: ",i+1, j+1);
 scanf("%d",&b[i][j]);
 }

 // Initializing all elements of result matrix to 0
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 {
 result[i][j] = 0;
 }

 // Multiplying matrices a and b and
 // storing result in result matrix
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 for(k=0; k<2; ++k)
 {
 result[i][j]+=a[i][k]*b[k][j];
 }

 // Displaying the result
 printf("\nOutput Matrix:\n");
 for(i=0; i<2; ++i)
 for(j=0; j<2; ++j)
 {
 printf("%d ", result[i][j]);
 if(j == 2-1)
 printf("\n\n");
 }
 return 0;
}

Output:

multiply

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.