-3
我試圖寫一個C++程序,增加了兩個的(m×n)的矩陣。這裏是代碼:C++矩陣加
/* Program: Add two (m x n) matrices */
#include <iostream>
#include <conio.h>
#define n 5
using namespace std;
void inputMatrix (int MatX[][n],int m, int n0);
void outputMatrix (int MatX[][n],int m, int n0);
void addMatrix (int A[][n],int B[][n],int C[][n],int m, int n0);
main()
{
/* Input Matrix Dimensions */
int m,n0;
cout<<"Enter No. of (Rows,Columns) in Matrix: ";
cin>>m>>n0;
/* Initialize Matrices */
int MatA[m][n],MatB[m][n],MatC[m][n];
/* Input Matrix Elements */
cout<<"############### Matrix A ###############"<<endl;
inputMatrix(MatA,m,n0);
cout<<"############### Matrix B ###############"<<endl;
inputMatrix(MatB,m,n0);
/* Compute & Display Result */
addMatrix(MatA,MatB,MatC,m,n0);
cout<<"########################################"<<endl;
outputMatrix(MatC,m,n0);
getch();
}
void inputMatrix (int MatX[][n],int m, int n0)
{
for (int i = 0; i<m; i++)
for (int j = 0; j<n0; j++)
{
cout<<"Element["<<i<<","<<j<<"] Value: ";
cin>>MatX[i][j];
}
}
void outputMatrix (int MatX[][n],int m, int n0)
{
cout<<"########### Resultant Matrix ###########"<<endl;
for (int i = 0; i<m; i++)
{
for (int j = 0; j<n0; j++)
cout<<"X["<<i<<","<<j<<"] = "<<MatX[i][j]<<'\t';
cout<<endl;
}
}
void addMatrix (int A[][n],int B[][n],int C[][n],int m, int n0)
{
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++)
C[i][j] = A[i][j] + B[i][j] ;
}
/*
1. Here, matrices A,B,C are 'm x n', but evaluations are made for 'm x n0'.
int MatA[m][n],MatB[m][n],MatC[m][n];
where user chooses dimensions of matrices (m x n0) to work upon.
This is because we have to initialize a matrix as A[][n] atleast. So we need atleast one parameter to be fixed for the compiler to allocate space.
2. Also, no Array values are returned explicitly.
void inputMatrix (...)
void outputMatrix (...)
void addMatrix (...)
*/`
該程序工作正常。 我的疑惑是:
- 當矩陣A,B的工作,C怎麼辦職能inputMatrix(),outputMatrix(),addMatrix工作在馬塔,MATB,MATC? 他們有單獨的副本嗎?他們是否直接在原始地址上工作?
- 我應該怎麼做,如果我想與尺寸「的M×N」(而不是「米×N0」),其中m的矩陣計算執行該方案,正由用戶輸入?
***其中m,n是由用戶輸入***最終,你將學習如何使用向量和其他合適的容器。 – drescherjm