Hi,
I'm trying to dynamically create a matrix, let's call it M, which can be accesed using the usual M[i][j] notation. That's it, it should behave as if I had declared it as a double M[5][5], except the size should be a variable rather than 5.
This is the simplest example:
Please, tell me there's a better solution that the horrible hack I did!!Code:#include <iostream.h> void createMatrix(int size) { double *M = new double[size*size]; // doesn't work :( /* // This does work, but is really awful... there's got to be a better way! double **M = new double*[size]; for (int k = 0; k < size; k++) M[k] = new double[size]; */ // this MUST work: for(int i = 0; i < size; i++) for(int j = 0; j < size; j++) M[i][j] = 3.141592; // whatever delete[] M; } int main() { int n; cout << "Size of the matrix? "; cin >> n; createMatrix(n); return 0; }