矩阵相乘问题
我同学给我一个矩阵相乘的程序(见附件),谁能还原出源代码?就是到输入完m行n列的时候,接着输入矩阵里的数,怎么才能通过输入的m,n控制键入A11一直到Amn。
矩阵乘法.rar
(5.14 KB)
#include <iostream> using namespace std; void input( int * * &a, int m, int n ,char c) { a = new int *[m]; for( int i = 0; i != m; ++i) { a[i] = new int[n]; for( int j = 0; j != n; ++j) { cout << c << i+1 << j+1 << "="; cin >> a[i][j]; } } } void print( int * * a, int m, int n) { for( int i = 0; i != m; ++i) { for( int j = 0; j != n; ++j) cout << " " << a[i][j]; cout << endl ; } cout << endl << endl ; } void matrix_mul(int * * a,int * * b,int * * &c,int m,int n,int r) { c = new int *[m]; for(int i=0; i != m; ++i) { c[i] = new int[r]; } for( i = 0; i != m; ++i) { for(int j = 0; j !=r; ++j) { c[i][j] = 0; for (int k = 0; k !=n; ++k) { c[i][j] += (a[i][k]) * (b[k][j]); } } } } void del(int * *a,int m) { for (int i = 0; i != m; ++i) delete a[i]; delete a; } int main() { int m,n,r,**A,**B,**C; cout << "本程序由nomify制作" << endl << endl << endl << endl << endl; cout << "该程序用于计算两个矩阵的乘积,其中m,n为矩阵A的行和列,n,r为矩阵B的行和列" << endl; cout << "m="; cin >> m; cout << "n="; cin >> n; cout << "r="; cin >> r; cout << "现在请输入A的值" << endl; input(A, m, n, 'A'); cout << "矩阵A=" << endl; print(A, m, n); cout << "现在请输入B的值" << endl; input(B, n, r, 'B'); cout << "矩阵B=" << endl; print(B, n, r); matrix_mul(A, B, C, m, n, r); cout << "答案C=" << endl; print(C, m, r); del(A,m); del(B,n); del(C,m); system("pause"); return 0; }自己写的一个