矩陣乘法 Matrix Multiplication
// matrixMultiplication.cpp
//
// It's a 2x3 matrices program of matrix multiplication using pointers in C++
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, j, k, sum;
// matrix space
const int ROW_A = 2, COL_A = 3;
const int ROW_B = 3, COL_B = 2;
// define matrix value
int a[ROW_A][COL_A] = {{1,3,1},{1,2,3}};
int b[ROW_B][COL_B] = {{1,3},{3,5},{3,3}};
int c[ROW_A][COL_B];
// define 3 pointers to first address
int *pa, *pb, *pc;
pa = &a[0][0];
pb = &b[0][0];
pc = &c[0][0];
// caclulation
for ( i=0; i<ROW_A; ++i ) {
for (j=0; j<COL_B; ++j) {
sum = 0;
for( k=0; k<COL_A; ++k ) {
sum += *(pa+i*COL_A+k) * *(pb+k*COL_B+j);
}
*(pc+i*COL_B+j) = sum;
}
}
// print matrix a
for ( i=0; i<ROW_A; ++i) {
for (j=0; j<COL_A; ++j) {
cout << *(pa+i*COL_A+j) << " ";
}
cout << endl;
}
cout << '\n';
// print matrix b
for (i=0; i<ROW_B; ++i) {
for (j=0; j<COL_B; ++j) {
cout << *(pb+i*COL_B+j) << " ";
}
cout << endl;
}
cout << '\n';
//print multiple matrix c
for (i=0; i<ROW_A; ++i) {
for (j=0; j<COL_B; ++j) {
cout << *(pc+i*COL_B+j) << " ";
}
cout << endl;
}
return 0;
}
Ref.
http://en.wikipedia.org/wiki/Matrix_multiplication

