|
① 二矩阵相加 说明三个行列数完全相同的二维数组用于表示矩阵: int A[m][n],B[m][n],C[m][n]; 给A、B二矩阵赋初值,而后计算C=A+B(矩阵加)。 【解答程序】
1 #include<iostream> 2 using namespace std; 3 int main() 4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 5 const int m=3; 6 const int n=4; 7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int A[m][n]={{1,2,3,4},{5,6,7,8},{0,2,4,6}}; 8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int B[m][n]={{1,0,2,1},{2,1,1,0},{3,6,5,2}}; 9 int i,j,C[m][m]; 10 cout<<"---A---"<<endl; 11 for(i=0;i<m;++i) 12![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 13 for(j=0;j<n;++j) 14 cout<<A[i][j]<<" "; 15 cout<<endl; 16 } 17 cout<<"---B---"<<endl; 18 for(i=0;i<m;++i) 19![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 20 for(j=0;j<n;++j) 21 cout<<B[i][j]<<" "; 22 cout<<endl; 23 } 24 cout<<"---C---"<<endl; 25 for(i=0;i<m;++i) 26![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 27 for(j=0;j<n;++j) 28![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 29 C[i][j]=A[i][j]+B[i][j]; 30 cout<<C[i][j]<<" "; 31 } 32 cout<<endl; 33 } 34 return 0; 35 } 36![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
运行结果:
---A---
1 2 3 4
5 6 7 8
0 2 4 6
---B---
1 0 2 1
2 1 1 0
3 6 5 2
---C---
2 2 5 5
7 7 8 8
3 8 9 8
② 二矩阵相乘 说明三个行列数完全相同的二维数组用于表示矩阵: int A[m][n],B[n][t],C[m][t]; 给A、B二矩阵赋初值,而后计算C=A*B(矩阵乘)。 【解答程序】
1 #include<iostream> 2 using namespace std; 3 int main() 4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 5 const int m=3; 6 const int n=4; 7 const int t=3; 8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int A[m][n]={{1,2,3,4},{5,6,7,8},{0,2,4,6}}; 9![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int B[n][t]={{1,0,2},{2,1,1},{5,3,1},{3,6,5}}; 10 int i,j,k,C[m][t]; 11 cout<<"---A---"<<endl; 12 for(i=0;i<m;++i) 13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 14 for(j=0;j<n;++j) 15 cout<<A[i][j]<<" "; 16 cout<<endl; 17 } 18 cout<<"---B---"<<endl; 19 for(i=0;i<n;++i) 20![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 21 for(j=0;j<t;++j) 22 cout<<B[i][j]<<" "; 23 cout<<endl; 24 } 25 cout<<"---C---"<<endl; 26 int sum; 27 for(i=0;i<m;++i) 28![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 29 for(j=0;j<t;++j) 30![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 31 sum=0; 32 for(int k=0;k<n;++k) 33 sum+=A[i][k]*B[k][j]; 34 C[i][j]=sum; 35 cout<<C[i][j]<<" "; 36 } 37 cout<<endl; 38 } 39 return 0; 40 } 41![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
运行结果:
---A---
1 2 3 4
5 6 7 8
0 2 4 6
---B---
1 0 2
2 1 1
5 3 1
3 6 5
---C---
32 35 27
76 75 63
42 50 36
③ 实现具有动态大小的矩阵乘法 例如:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 6 int m,n; 7 int i,j,k; 8 cout<<"Enter m,n(A--m*n,B--n*m):"; 9 cin>>m>>n; 10 int **A,**B,**C; //A、B、C都是二维数组 11 A=new int*[m]; //A矩阵分配m行 12 for(int i=0;i<m;i++) 13 A[i]=new int[n]; //A矩阵每行分配n个元素(下同B、C) 14 B=new int*[n]; 15 for(int i=0;i<n;i++) 16 B[i]=new int[m]; 17 C=new int*[m]; 18 for(int i=0;i<m;i++) 19 C[i]=new int[m]; 20 cout<<"-------A-------"<<endl; 21 for(i=0;i<m;i++) 22![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 23 for(j=0;j<n;j++) 24![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 25 A[i][j]=i+1; 26 cout<<setw(4)<<A[i][j]<<" "; 27 } 28 cout<<endl; 29 } 30 cout<<"-------B-------"<<endl; 31 for(i=0;i<n;i++) 32![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 33 for(j=0;j<m;j++) 34![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 35 B[i][j]=A[j][i]; 36 cout<<setw(4)<<B[i][j]<<" "; 37 } 38 cout<<endl; 39 } 40 cout<<"-------C-------"<<endl; 41 int sum; 42 for(i=0;i<m;i++) 43![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 44 for(j=0;j<m;j++) 45![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 46 sum=0; 47 for(k=0;k<n;k++) 48 sum+=A[i][k]*B[k][j]; 49 C[i][j]=sum; 50 cout<<setw(4)<<C[i][j]<<" "; 51 } 52 cout<<endl; 53 } 54 return 0; 55 } 56![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
运行结果:
-------A-------
1 1 1 1
2 2 2 2
3 3 3 3
-------B-------
1 2 3
1 2 3
1 2 3
1 2 3
-------C-------
4 8 12
8 16 24
12 24 36
|