Last active
August 29, 2015 14:00
-
-
Save dd1994/bf0523bff4df1752c1a0 to your computer and use it in GitHub Desktop.
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
//Matrix multiplication | |
//matrix1: m x s | |
//matrix2: s x n | |
#include <stdio.h> | |
void matrixInput(int m, int n, float (*a)[n]); | |
void MatrixMultiply(int m, int s, int n, | |
float (*matrix1)[s], float (*matrix2)[n], | |
float (*matrixResult)[n]); | |
int main(void) | |
{ | |
int i, j, k, m, s, n; | |
printf("Please input the m of matrix1\n"); | |
scanf("%d", &m); | |
printf("Please input the s of matrix1\n"); | |
scanf("%d", &s); | |
printf("Please input the n of matrix2\n"); | |
scanf("%d", &n); | |
float matrix1[m][s], matrix2[s][n], matrixResult[m][n]; | |
printf("Please input elements of matrix1:(%d*%d)\n", m, s); | |
matrixInput(m, s, matrix1); | |
printf("Please input elements of matrix2:(%d*%d)\n", s, n); | |
matrixInput(s, n, matrix2); | |
MatrixMultiply(m, s, n, matrix1, matrix2, matrixResult); | |
printf("the result:\n"); | |
for (i = 0; i < m; ++i) | |
{ | |
for (j = 0; j < n; ++j) | |
{ | |
printf("%f ", matrixResult[i][j]); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} | |
void MatrixMultiply(int m, int s, int n, | |
float (*matrix1)[s], float (*matrix2)[n], | |
float (*matrixResult)[n]) | |
{ | |
int i, j, k; | |
for (i = 0; i < m; ++i) | |
{ | |
for (j = 0; j < n; ++j) | |
{ | |
for (k = 0, matrixResult[i][j] = 0; k < s; ++k) | |
{ | |
matrixResult[i][j] += matrix1[i][k] * matrix2[k][j]; | |
} | |
} | |
} | |
} | |
void matrixInput(int m, int n, float (*a)[n]) | |
{ | |
int i, j; | |
for (i = 0; i < m; ++i) | |
{ | |
for (j = 0; j < n; ++j) | |
{ | |
scanf("%f", &a[i][j]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment