矩阵相乘C++代码
2018-07-20 来源:open-open
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<iomanip>
using namespace std;
void multmat(int A[], int B[], int C[], int m, int n, int p)
{
int i, j, k;
for(i = 0; i < m; ++i)
for(j = 0; j < p; ++j)
{
int s = 0;
for(k = 0; k < n; ++k)
s += A[i*n + k]*B[k*p + j];
C[i*p + j] = s;
}
return;
}
int main(void)
{
const int MAX = 1000;
int A[MAX], B[MAX], C[MAX];
int m, n, p;
cout << "input m,n,p: ";
cin >> m >> n >> p;
if(m*n >= MAX || n*p >= MAX || m*p >= MAX)
{
cout << "mem ex." << endl;
return -1;
}
cout << "input matrix A: " << endl;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
cin >> A[i*n + j];
cout << "input matrix B: " << endl;
for(int i = 0; i < n; ++i)
for(int j = 0; j < p; ++j)
cin >> B[i*p + j];
multmat(A, B, C, m, n, p);
cout<< "the matrix C is: "<< endl;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < p; ++j)
{
cout << setw(3) << C[i*p + j] << ' ';
}
cout << endl;
}
system("pause");
return 0;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:C语言实现堆排序代码
最新资讯
热门推荐