Matrix Multiplication using array
In this example, we will understand to perform Matrix Multiplication using Array and know how to write, compile and debug it in C language and also learn how to implement it in c.
In order to do this you will need
- Basic knowledge of c programming language.
- As well as know how to operate codeblocks.
An array is a fixed-size sequential collection of elements of same data types that share a common name.It is simply a group of data types. An array is a derived data type.An array is used to represent a list of numbers or a list of names.
So let's begin with our program
1.Open a codeblocks software.
2.Open new empty file.
3.Copy and paste the code from below.
Input code-
//Write a program for Matrix Multiplication.
#include<stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],d[2][2],i,j,k,m[2][2]={0};
printf("Enter first 2*2 matrix elements");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given 2*2 matrix elements of first: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
printf("Enter second 2*2 matrix elements");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Given 2*2 matrix elements of second: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
m[i][j]=m[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Mul result: \n ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t %d",m[i][j]);
}
printf("\n");
}
return 0;
}
Now simply build the code. Look for any error. Now run the code.
For output enter any four number for first matrix and press enter key then again enter four number for second matrix and press enter key.
Click the following button to download a program for Matrix Multiplication using Array.
Thats it.Thank you for scrolling.
Tags:
Array