Array multiplication in Two-Dimensional Array using Array in C


The program is a C program that performs matrix multiplication on two matrices of size r x c. The program takes input for the number of rows and columns in the matrices and the elements of the matrices from the user. The program uses nested for loops to perform the matrix multiplication and stores the result in a new matrix "m". The program then prints the result matrix on the screen.

In detail, the program first prompts the user to enter the number of rows and columns in the matrices. It then prompts the user to enter the elements of the two matrices, "a" and "b" using two nested for loops. Next, the program uses another nested for loop to perform the matrix multiplication. The program multiplies the corresponding elements of the matrices "a" and "b" and stores the result in the new matrix "m". The program then prints the resulting matrix "m" on the screen.

Source Code

#include<stdio.h>
int main()
{
  int a[5][5],b[5][5],m[5][5];
  int i,j,r,c,t,k;
  printf("\nEnter No of Rows : ");
  scanf("%d",&r);
  printf("\nEnter No of Columns : ");
  scanf("%d",&c);
  printf("\nEnter A Matrix : ");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
       scanf("%d",&a[i][j]);
    }
  }
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
        printf("\t%d",a[i][j]);
    }
    printf("\n");
  }
  printf("\nEnter B Matrix : ");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
       scanf("%d",&b[i][j]);
    }
  }
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
       printf("\t%d",b[i][j]);
    }
    printf("\n");
  }
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      t=0;
      for(k=0;k<c;k++)
      {
         t+=(a[i][k]*b[k][j]);
      }
      m[i][j]=t;
    }
  }
  printf("\nResult Matrix : \n");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
       printf("\t%d",m[i][j]);
    }
    printf("\n");
  }
  return 0;
}
To download raw file Click Here

Output

Enter No of Rows : 2
Enter No of Columns : 2
Enter A Matrix : 2
3
4
5

2	3
4	5

Enter B Matrix : 5
6
7
8

5	6
7	8

Result Matrix : 
	31	36
	55	64


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C