Jump to content

Archive:Matrix multiplication using C: Difference between revisions

From IdeaWazaWiki
wikademia>Stinky sock
Removing all content from page
wikademia>Sebmol
m Reverted edits by Stinky sock (Talk) to last version by JWSchmidt using rollback
Line 1: Line 1:
<pre>/* Program to implement matrix multiplication. */
/* Performed by Milan. */


#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], b[10][10], c[10][10];
int i, j, m, n, x, y, k;
clrscr();
/* 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.");
getch();
exit(0);
}
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");
}
getch();
}
/* 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
*/
</pre>
[[Category:C source code examples]]

Revision as of 18:14, 7 April 2007

/* Program to implement matrix multiplication. */
/* Performed by Milan. */

#include <stdio.h>
#include <conio.h>

void main()
{
	int a[10][10], b[10][10], c[10][10];
	int i, j, m, n, x, y, k;
	clrscr();

	/* 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.");
		getch();
		exit(0);
	}
	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");
	}

	getch();
}
/* 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
*/