C program that multiplies the matrix a by the matrix b and stores the result in the matrix c
Posted by Samath
Last Updated: May 04, 2016
  1568

Consider the following variable declarations. Assume each variable stores the values in a matrix. Write code that multiplies the matrix a by the matrix b and stores the result in the matrix c .
float a[2][3],b[3][2],c[2][2];

Solution:

#include<stdio.h>
int main(int argc, char *argv[])
{
	float a[2][3], b[3][2], c[2][2];
	int i, j;
	float value,temp1=0,temp2=0;
	printf("Enter Matrix 1\n");
	for (i = 0; i < 2; i++){
		printf("Enter Elements of Row %d \n", i + 1);
		for (j = 0; j < 3; j++){
			printf("Enter value of Column %d ", j + 1);
			scanf("%f", &value);
			a[i][j] = value;
		}
	}
	printf("Enter Matrix 2\n");
	for (i = 0; i < 3; i++){
		printf("Enter Elements of Row %d \n", i + 1);
		for (j = 0; j < 2; j++){
			printf("Enter value of Column %d ", j + 1);
			scanf("%f", &value);
			b[i][j] = value;
		}
	}
	for (i = 0; i < 2; i++){
		for (j = 0; j < 3; j++){
			temp1 = temp1 + a[i][j] * b[j][i];
			temp2 = temp2 + a[i][j] * b[j][1];
		}
		c[i][0] = temp1;
		c[i][1] = temp2;
		temp1 = 0;
		temp2 = 0;
	}
	for (i = 0; i < 2; i++){
		for (j = 0; j < 2; j++)
			printf("%.2f	", c[i][j]);
		printf("\n");
	}
	
	return 0;
}