Here's a tiny little class that's bothering me:
class matrix{
string name;
int rowSize, columnSize;
// I could declare double m[100][100]; here but that would be ugly
public:
void createMat(){
cout << "Enter Matrix Name:";
cin >> name;
cout << "Enter number of rows: ";
cin >> rowSize;
cout << "Enter no. of columns: ";
cin >> columnSize;
double m[rowSize][columnSize]; //needs to be available to readMat()
cout << "Enter matrix (row wise):\n";
for(int i = 0; i < rowSize; i++){
for(int j = 0; j < columnSize; j++){cin >> m[i][j];}
cout<<'\n';
}
}
void readMat(){
cout << "Matrix " << name << " :\n";
for(int i = 0 ; i < rowSize; i++){
for(int j = 0; j < columnSize; j++){ cout << m[i][j] << ' ';}
cout<<'\n';
}
}
};
How can I make m
available to both createMat()
and readMat()
in an optimal way?
Is trying to allow the user to enter the dimensions of the matrix unnecessary?
From my point of view, I feel that me defining the maximum size of the matrix would be too limiting in case more elements are required or too much if not as many elements are required.