Write a C program to Multiply two matrices.

 TASK-

Write a C program to Multiply two matrices.

LOGIC BUILDING:

In the View of simply math, Two matrices multiplying, must be in order of multiplication. e.g
Mat1 (3x4) can only be multiplied with matrix have 4 number of row e.g 4x5 in simplest form any matrix with order 4xn (where n can be any integer) can be multiplied with mat1.

The resultant matrix e.g multiplying mat1(2x3) and mat2(3x4) would give mat3(2x4).

Working with same concept Lets begin the coding:

CODE:

#include<stdio.h>
int main(){
int mat1[2][3],mat2[3][4],mat3[2][4],i,j,k,sum=0;
//Asking for entries in mat1
printf("Enter the Entries of mat1(2x3): ");
for (i=0;i<2;i++)
{
	for (j=0;j<3;j++)
	{
		printf("\n Entry [%d][%d] ",i+1,j+1);
		scanf("%d",&mat1[i][j]);
	}
}
//Asking for entries in mat2
printf("\nEnter the Entries of mat2(3x4): ");
for (i=0;i<3;i++)
{
	for (j=0;j<4;j++)
	{
		printf("\n Entry [%d][%d] ",i+1,j+1);
		scanf("%d",&mat2[i][j]);
	}
}
//Here multiplication begins:
for (i=0;i<2;i++)
{
	for (j=0;j<4;j++)
	{
		for (k=0;k<3;k++)
		{
			sum=sum+(mat1[i][k]*mat2[k][j]);
		}
		mat3[i][j]=sum;
		sum=0;
	}
}
//Printing the result;
printf("\n\nmat1 x mat2: \n\n");
for (i=0;i<2;i++)
{
	for (j=0;j<4;j++)
	{
		printf("%d\t",mat3[i][j]);
	}
	printf("\n");
}
getchar();
return 0;
}

Expected Output:





NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

1 Comments

Post a Comment
Previous Post Next Post