Created
October 14, 2012 10:54
-
-
Save messense/3888244 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 | |
* 已知一个矩阵A为{{3,0,4,5},{6,2,1,7},{4,1,5,8}} | |
* 另一个矩阵B为{{1,4,0,3},{2,5,1,6},{0,7,4,4},{9,3,6,0}} | |
* 求出A与B的乘机矩阵C[3][4]并输出出来,其中C中的每个元素C[i][j]等于∑3k=0 A[i][k]*B[k][j]。 | |
*/ | |
#include <iostream> | |
using namespace std; | |
void print_matrix3(int mt[3][4]) { | |
int i, j; | |
for(i = 0; i < 3; i++) { | |
for(j = 0; j < 4; j++) { | |
cout<<mt[i][j]; | |
if(j < 3) | |
cout<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
void print_matrix4(int mt[4][4]) { | |
int i, j; | |
for(i = 0; i < 4; i++) { | |
for(j = 0; j < 4; j++) { | |
cout<<mt[i][j]; | |
if(j < 3) | |
cout<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
int a[3][4] = {{3, 0, 4, 5},{6, 2, 1, 7},{4, 1, 5, 8}}; | |
int b[4][4] = {{1, 4, 0, 3},{2, 5, 1, 6},{0, 7, 4, 4},{9, 3, 6, 0}}; | |
int c[3][4]; | |
int i; | |
for(i = 0; i < 3; i++) { | |
int j; | |
for(j = 0; j < 4; j++) { | |
int k, sum = 0; | |
for(k = 0; k < 4; k++) { | |
sum += a[i][k]*b[k][j]; | |
} | |
c[i][j] = sum; | |
} | |
} | |
cout<<"Matrix A:"<<endl<<endl; | |
print_matrix3(a); | |
cout<<endl<<"Matrix B:"<<endl<<endl; | |
print_matrix4(b); | |
cout<<endl<<"Matrix C:"<<endl<<endl; | |
print_matrix3(c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment