Thread: programming n-dimensional cubes

1. programming n-dimensional cubes

My task is to write a c++ program that will generate an n-dimensional cube. Each point is a bit string, and connected to each point by edges are n other points, where n is the dimension. The other points are one bit changed from the original bit string. For example:

00 10

01 11

Would define a square. Making a cube would entail duplicating the square, adding a 0 bit to the end of the original points, and adding a 1 to the end of the new points, then joining the two squares together into a cube.

I dont know how to dynamically allocate an array inside an object, which would help. Can anyone give me any pointers as to the best way to approach this? Thanks,

Jason D.

2. new and delete can be used to create arrays
Code:
```int * anArray = NULL;
int ** a2dArray = NULL;

anArray = new int [10];  //the constant 10, can be replaced by a var.

if( anArray == NULL)
{
cout << "call to new failed!";
}
else
{
//now anArray points to 10 ints, so it is the same thing as an array
//anArray[0] thru anArray[9] are valid
}

delete [] anArray; // must remember to free the mem when done with it!!

That is to make a 1d array
2d is a little tricker

int size = 3;
a2dArray = new int * [size]; //notice i want to create 3 new pointers, these new pointers will be used to make 1d arrays, giving us a 2d array.

if (a2dArray == NULL)
{
cout << "call to new failed!";
}
else
{
for( int i=0 ; i<size ; i++)
{
a2dArray[i] = new int[size]; //creates 3 new ints
}
}

now a2dArray[0..3][0..3] are valid.
to free the 2d array

for( int i=0 ; i<size ; i++)
{
delete [] a2dArray[i];
}
delete [] a2dArray;```

3. Yeah I know how to declare arrays dynamically. And I just figured out something. My problem is doing so inside a class declaration. Such as:

class object
{
public:
int *arrayname;
somefunction();
};

My compiler tells me "error C2252: 'vertices' : pure specifier can only be specified for functions." when I use the following code:

class object
{
public:
int *arrayname=NULL;
somefunction();
};

What does it mean by a pure specifier? I have always made it a habit to initialize my pointers to NULL... can I not do that here?

Thanks for your help thus far.

Jason D.

4. You cannot intialize a variable inside a structure or class! Remove the NULL and initialize the variable in the class' constructor.

Popular pages Recent additions