Dynamic allocation of multidimensional arrays
-
How can I create a multidimensional array of int's (or whatever) when I don't know the dimensions at compile time? I've tried alot of stuff, but the compiler still hasn't accepted any of my desperate tries. Please help me... :confused: In my case, I want the user to decide at runtime the lenght and width of a labyrint, that should be generated afterwards. I'm aware that there are other alternatives to multidimensional arrays, and I'm open to other good suggestions too. Sprudling
-
How can I create a multidimensional array of int's (or whatever) when I don't know the dimensions at compile time? I've tried alot of stuff, but the compiler still hasn't accepted any of my desperate tries. Please help me... :confused: In my case, I want the user to decide at runtime the lenght and width of a labyrint, that should be generated afterwards. I'm aware that there are other alternatives to multidimensional arrays, and I'm open to other good suggestions too. Sprudling
This is the best I've managed to do so far...
#include "iostream.h"
int main(int argc, char* argv[])
{
// Set the dimensions of the array
int nA = 5;
int nB = 4;// "Create" the 1. dimension of the array
int** a = new int*[nA];// "Create" the 2. dimension of the array
for (int i1 = 0; i1 < nA; i1++)
{
a[i1] = new int[nB];
}// Initialize all the array elements
for (i1 = 0; i1 < nA; i1++)
{
for (int i2 = 0; i2 < nB; i2++)
{
a[i1][i2] = (i1 * nB) + i2;
}
}// Print all the array elements to the screen
for (i1 = 0; i1 < nA; i1++)
{
for (int i2 = 0; i2 < nB; i2++)
{
cout << "[" << i1 << "][" << i2 << "] : " << a[i1][i2] << endl;
}
}// Deallocate the array
for (i1 = 0; i1 < nA; i1++)
{
delete[] a[i1];
}
delete[] a;return 0;
}It seems to work ok, but there is alot of code just for creating an array, and I'll have to keep track of the size of the 1. dimension of the array, and that's not always simple. Isn't there a better way? :confused: Sprudling
-
This is the best I've managed to do so far...
#include "iostream.h"
int main(int argc, char* argv[])
{
// Set the dimensions of the array
int nA = 5;
int nB = 4;// "Create" the 1. dimension of the array
int** a = new int*[nA];// "Create" the 2. dimension of the array
for (int i1 = 0; i1 < nA; i1++)
{
a[i1] = new int[nB];
}// Initialize all the array elements
for (i1 = 0; i1 < nA; i1++)
{
for (int i2 = 0; i2 < nB; i2++)
{
a[i1][i2] = (i1 * nB) + i2;
}
}// Print all the array elements to the screen
for (i1 = 0; i1 < nA; i1++)
{
for (int i2 = 0; i2 < nB; i2++)
{
cout << "[" << i1 << "][" << i2 << "] : " << a[i1][i2] << endl;
}
}// Deallocate the array
for (i1 = 0; i1 < nA; i1++)
{
delete[] a[i1];
}
delete[] a;return 0;
}It seems to work ok, but there is alot of code just for creating an array, and I'll have to keep track of the size of the 1. dimension of the array, and that's not always simple. Isn't there a better way? :confused: Sprudling
The way you're doing it is the correct way. :) --Mike-- http://home.inreach.com/mdunn/ Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese? Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?
-
The way you're doing it is the correct way. :) --Mike-- http://home.inreach.com/mdunn/ Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese? Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?
this is the perfect place for some cool template code :) any takers?
-
The way you're doing it is the correct way. :) --Mike-- http://home.inreach.com/mdunn/ Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese? Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?
What if I want like 10 different arrays with 10 or more dimensions? That would be alot of code. And are you telling me that C++ hasn't support for dynamic multidimensional arrays?! If so, I'm sure this has been a "problem" for a long time, and someone must have come up with something... A quick and easy general way to create arrays of N dimensions. If there exist one, I really want to know :) Sprudling
-
How can I create a multidimensional array of int's (or whatever) when I don't know the dimensions at compile time? I've tried alot of stuff, but the compiler still hasn't accepted any of my desperate tries. Please help me... :confused: In my case, I want the user to decide at runtime the lenght and width of a labyrint, that should be generated afterwards. I'm aware that there are other alternatives to multidimensional arrays, and I'm open to other good suggestions too. Sprudling