multidmensional dynamic array

This is a discussion on multidmensional dynamic array within the C++ Programming forums, part of the General Programming Boards category; I need that twodimensional bool array to write in all possible combinations of 0 and 1 without repeating. So if ...

  1. #16
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I need that twodimensional bool array to write in all possible combinations of 0 and 1 without repeating.

    So if I have a variable x = 3 (which means number of cells)

    There are 3^2 (8) possible combinations.

    Now the array should look like:

    Code:
    0: 0 0 0
    1: 0 0 1
    2: 0 1 0
    3: 0 1 1
    4: 1 0 0
    5: 1 0 1
    6: 1 1 0
    7: 1 1 1
    Any help on implementing this would be highly appreciated!

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If I were given so many arbitrary restrictions like you have in your assignment. I would write my own custom container class. But I guess I am full of crazy ideas like that---or perhaps I understand that the whole objective of the assignment is to get you to realize that is the best option.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm probably being stupid, but why do you need a 2D array for this?

    I also think that tabstop has given the answer. Use bool (*table)[2] = new bool [somenumber][2]

    Or just use a fixed size array if you can be reasonably sure that x isn't going to be enormous - there is no great difference in how much memory you can allocate for a dynamic array or a fixed array, and until you actually USE the mmemoy in a fixed array, it won't actually use up (much) RAM in your machine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by l2u View Post
    I need that twodimensional bool array to write in all possible combinations of 0 and 1 without repeating.

    So if I have a variable x = 3 (which means number of cells)

    There are 3^2 (8) possible combinations.

    Now the array should look like:

    Code:
    0: 0 0 0
    1: 0 0 1
    2: 0 1 0
    3: 0 1 1
    4: 1 0 0
    5: 1 0 1
    6: 1 1 0
    7: 1 1 1
    Any help on implementing this would be highly appreciated!
    This is not a two-dimensional array with exactly two columns. This is a two-dimensional array where the columns (n) and rows (2**n) are both dynamic. Is it a requirement to use a two-dimensional array to solve this problem? It's pretty easy to get from 7 to 1 1 1 without it.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I also think that tabstop has given the answer. Use bool (*table)[2] = new bool [somenumber][2]
    You can make this prettier with a well-placed typedef.
    Code:
    typedef bool (*TwoBools)[2];
    TwoBools *table = new TwoBools[somenumber]
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array?
    By BSmith4740 in forum C Programming
    Replies: 8
    Last Post: 06-30-2008, 11:52 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21