Thread: programming n-dimensional cubes

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    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. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    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. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    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. #4
    Unregistered
    Guest
    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 subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 05:27 AM
  3. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  4. Have you ever had a four dimensional experience
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 08-18-2003, 06:13 PM
  5. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM