Archive:Matrix multiplication using C: Difference between revisions
wikademia>Remi0o m added cats |
wikademia>MichaelFrey mNo edit summary |
||
| Line 1: | Line 1: | ||
< | <source lang="c">/* Program to implement matrix multiplication. */ | ||
/* Performed by Milan. */ | /* Performed by Milan. */ | ||
| Line 97: | Line 97: | ||
c[2][1] = 14 c[2][2] = 11 | c[2][1] = 14 c[2][2] = 11 | ||
*/ | */ | ||
</ | </source> | ||
[[Category:C source code examples]] | [[Category:C source code examples]] | ||
[[Category:C]] | [[Category:C]] | ||
[[Category:Programming]] | [[Category:Programming]] | ||
Revision as of 06:46, 17 May 2007
<source lang="c">/* 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
- /
</source>