Matrix multiplication using two dynamically allocated array

This post will guide you to create a program which can accept two matrix according to the given number of rows and column. The arrays are dynamically initialized and then allocated memory.

Screenshots [Demo] :


The Code:
#include<stdio.h>
main()
{
    int **pa1,**pa2,i,j,r,c,k,s=0;
	printf("Enter the number of rows: -->");
	scanf ("%d",&r);
	printf("Enter the number of columns: -->");
	scanf ("%d",&c);
	// Dynamically allocating the rows of first array pa1
	pa1=(int **)malloc(r*sizeof(int *)); 
	// Dynamically allocating the rows of Second array pa2
	pa2=(int **)malloc(r*sizeof(int *)); 
	for(i=0;i<c;i++)
	{
		// Dynamically allocationg the columns of the first array pa1
		pa1[i]=(int *)malloc(c*sizeof(int));
		// Dynamically allocationg the columns of the second array pa2 
		pa2[i]=(int *)malloc(c*sizeof(int));
	}
	printf("Give The Data Of First Dynamically allocated array :\n");	
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			// Storing the data as normal 2D array
			scanf("%d",&pa1[i][j]); 
		}
	}
	printf("Give The Data Of Second Dynamically allocated array :\n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			// Storing the data as noraml 2D array
			scanf("%d",&pa2[i][j]); 
		}
	}
	printf("The product of the two Dynamically allocated array is :\n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			for(k=0;k<r;k++)
			{
				// Mul. The two array's elements
				s=s+(pa1[i][k]*pa2[k][j]); 
			}
			printf("%d ",s);
			s=0;
		}
		printf("\n");
	}
}

Found bugs, Please Report us !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution