Public Group

N-dimensional arrays

This topic is 3835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

The code below is my program which uses the prim algorithm to generate a two-dimensional maze using dynamic memory allocation. (The size of the maze is specified in the command line arguments)
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

struct cell
{
bool n,s,w,e,inset;
};

int main( int argc, char *argv[] )
{
ofstream OutputFile; // the file the IN array will be saved to
OutputFile.open("maze.dat");

int xdim, ydim; // x and y spans
//xdim = atoi(argv[1]);
//ydim = atoi(argv[2]);

xdim=atoi(argv[1]);
ydim=atoi(argv[2]);
cell** IN;
IN = new cell*[xdim];
for(int i = 0; i < xdim; i++)
{
IN = new cell[ydim];
}

cell** EX;
EX = new cell*[xdim];
for(int i = 0; i < xdim; i++)
{
EX = new cell[ydim];
}

ADJ = new cell*[xdim];
for(int i = 0; i < xdim; i++)
{
ADJ = new cell[ydim];
}

int NUMADJ = 0;//number of cells still in the ADJ set

// initially sets everything to false except the fact that everything is in the EX set
for(int i=0;i<xdim;i++)
{
for(int j=0;j<ydim;j++)
{
IN[j].n = false;
EX[j].n = false;
IN[j].s = false;
EX[j].s = false;
IN[j].w = false;
EX[j].w = false;
IN[j].e = false;
EX[j].e = false;
IN[j].inset = false;
EX[j].inset = true;
}
}

//picks a random cell to be in the IN set. and it's neighbors to be in the ADJ set
srand(time(0));
int x = rand()%xdim;
int y = rand()%ydim;
EX[x][y].inset = false;
IN[x][y].inset = true;
if(x>0)
{
EX[x-1][y].inset = false;
}
if(x<(xdim-1))
{
EX[x+1][y].inset = false;
}
if(y>0)
{
EX[x][y-1].inset = false;
}
if(y<(ydim-1))
{
EX[x][y+1].inset = false;
}

//as long as there are cells in the ADJ set it picks a random cell in ADJ
//put it in IN, put it's neighbors in ADJ, and connect it with a random cell in IN
{
{
x = rand()%xdim;
y = rand()%ydim;
}

IN[x][y].inset = true;
if((x>0)&&EX[x-1][y].inset)
{
EX[x-1][y].inset = false;
}
if((x<(xdim-1))&&EX[x+1][y].inset)
{
EX[x+1][y].inset = false;
}
if((y>0)&&EX[x][y-1].inset)
{
EX[x][y-1].inset = false;
}
if((y<(ydim-1))&&EX[x][y+1].inset)
{
EX[x][y+1].inset = false;
}

//now we connect cell x,y with a random cell in IN that is adjacent to it
int looop=1;
while (looop)
{
int a = rand()%xdim;
int b = rand()%ydim;
if ( IN[a].inset && (a==(x-1)) && (b==y) )
{
IN[a].e = true;
IN[x][y].w = true;
looop = 0;
}
else if ( IN[a].inset && (a==(x+1)) && (b==y) )
{
IN[a].w = true;
IN[x][y].e = true;
looop = 0;
}
else if ( IN[a].inset && (a==x) && (b==(y-1)) )
{
IN[a].n = true;
IN[x][y].s = true;
looop = 0;
}
else if ( IN[a].inset && (a==x) && (b==(y+1)) )
{
IN[a].s = true;
IN[x][y].n = true;
looop = 0;
}
}
}
//this writes the IN array to a file in an easy-to-parse manner

for(int i=0;i<xdim;i++)
{
for(int j=0;j<ydim;j++)
{
if((i==(xdim-1))&&(j==(ydim-1))) break;
OutputFile << i <<"x"<< j <<":"<< IN[j].n <<"."<< IN[j].s <<"."<< IN[j].w <<"."<< IN[j].e << " ";
}
}
OutputFile << xdim-1 <<"x"<< ydim-1 <<":"<< IN[xdim-1][ydim-1].n <<"."<< IN[xdim-1][ydim-1].s <<"."<< IN[xdim-1][ydim-1].w <<"."<< IN[xdim-1][ydim-1].e;

OutputFile.close();
return 0;
}

This program works great and I even modified it to generate 3d mazes instead. This was quite easy. My question: How would I go about doing it n-dimensionally, where n was also defined in command line arguments by the user. I know of no way in which to handle an n-dimensional array and dynamically allocate it at run-time. side note: there is one way i could do this but it is messy and i'd honestly prefer something cleaner. i could just have a one dimensional array and do divisions and mods and handle remainders to find out where each element was in the higher-dimensional sense. this is so messy though. if there's no better way to do it than that then i'd rather abandon the problem and move onto something more productive

Share on other sites
Use a 1 dimensional array and adjust your n indices to index into it accordingly, or just use a boost::multi_array.

Share on other sites
Quote:
 Original post by Driv3MeFarUse a 1 dimensional array and adjust your n indices to index into it accordingly, or just use a boost::multi_array.

Is your first suggestion basically the same as what I said?

also.. boost: multi_array doesn't seem to do what I want. all the calls to the array are still very dimension-specific. I don't even understand how you could make a call to an array of arbitrary dimensions.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• Forum Statistics

• Total Topics
633690
• Total Posts
3013339
×