Created
March 6, 2016 16:36
-
-
Save dsouzadyn/7414410733c36bcf3dec to your computer and use it in GitHub Desktop.
C program for matrix multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2016 Dylan Dsouza | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void main() | |
{ | |
int c1 = 3; // Columns of first matrix | |
int r1 = 1; // Rows of first matrix | |
int c2 = 2; // Columns of second matrix | |
int r2 = 3; // Rows of second matrix | |
/* | |
* | |
* Please note that if user must input rows and columns, code can be modified accordingly. | |
* | |
*/ | |
int mat1[r1][c1]; // Init matrix 1 | |
int mat2[r2][c2]; // Init matrix 2 | |
int c; // Init counter varable c | |
int d; // Init counter variabls d | |
/* | |
* Read input for matrix 1 | |
*/ | |
for(c = 0; c < r1; c++) | |
{ | |
for(d = 0; d < c1; d++) { | |
printf("Enter %dx%d : ", c+1, d+1); | |
scanf("%d", &mat1[c][d]); | |
printf("\n"); | |
} | |
} | |
/* | |
* Read input for matrix 2 | |
*/ | |
for(c = 0; c < r2; c++) | |
{ | |
for(d = 0; d < c2; d++) { | |
printf("Enter %dx%d : ", c+1, d+1); | |
scanf("%d", &mat2[c][d]); | |
printf("\n"); | |
} | |
} | |
/* | |
* Check if matrix multiplication is possible. | |
*/ | |
if(c1 == r2) | |
{ | |
/* | |
* Init the product matrix | |
*/ | |
int prod[r1][c2]; | |
for(c = 0; c < r1; c++) | |
{ | |
for(d = 0; d < c2; d++) | |
{ | |
prod[c][d] = 0; | |
} | |
printf("\n"); | |
} | |
/* | |
* Compute the solution. | |
*/ | |
for(c = 0; c < r1; c++) | |
{ | |
int a = c; | |
for(d = 0; d < c2; d++) | |
{ | |
int m = 0; | |
while(m < c1) { | |
prod[c][d] += mat1[c][m] * mat2[m][d]; | |
m++; | |
} | |
} | |
} | |
/* | |
* Print the solution | |
*/ | |
printf("Solution is:\n"); | |
int x, y; | |
for(x = 0; x < r1; x++) | |
{ | |
for(y = 0; y < c2; y++) | |
{ | |
printf("%d ", prod[x][y]); | |
} | |
printf("\n"); | |
} | |
} | |
else | |
{ | |
/* | |
* If not possible notify the user accordingly | |
*/ | |
printf("Can't multiply %dx%d matrix with %dx%d matrix.\n",r1,c1,r2,c2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment