सी स्रोत कोड/मैट्रिक्स गुणन
दिखावट
/* Program to implement matrix multiplication. */
/* Performed by Milan. */
/* Edited by Gergo: resolved some portability issues */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[10][10], b[10][10], c[10][10];
int i, j, m, n, x, y, k;
/* Entering values for matrix a...*/
printf ("\n Enter the rows and columns of a: ");
scanf ("%d%d", &m, &n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf (" a[%d][%d] = ", (i+1), (j+1));
scanf ("%d", &a[i][j]);
}
printf ("\n");
}
/* Entering values for matrix b... */
printf ("\n Enter the rows and columns of b: ");
scanf ("%d%d", &x, &y);
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
printf (" b[%d][%d] = ", (i+1), (j+1));
scanf ("%d", &b[i][j]);
}
printf ("\n");
}
/* Calculations */
if ( n != x)
{
printf ("\n Error!! Rows of a don't match with columns of b.");
printf ("\n Multiplication Impossible.");
exit(EXIT_FAILURE);
}
else
{
for (i = 0; i < m; i++)
{
for (j = 0; j < y; j++)
{
c[i][j] = 0;
for (k = 0; k < n; k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
/* Output */
printf ("\n The resultant c is:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < y; j++)
{
printf (" c[%d][%d] = %d", (i+1), (j+1), c[i][j]);
}
printf ("\n");
}
return 0;
}
/* Output:
Enter the rows and columns of a: 2 2
a[1][1] = 1
a[1][2] = 2
a[2][1] = 3
a[2][2] = 4
Enter the rows and columns of b: 2 2
b[1][1] = 2
b[1][2] = 1
b[2][1] = 2
b[2][2] = 2
The resultant c is:
c[1][1] = 6 c[1][2] = 5
c[2][1] = 14 c[2][2] = 11
*/