Thread: Dynamic 2-d Array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    25

    Exclamation Dynamic 2-d Array

    I was wondering if I could add a row and a column to an array while the program is running?
    Last edited by SpockRox08; 12-15-2010 at 03:59 AM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well you have you j and i the wrong way around in the second function is one problem
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    You can't do that with arrays. The allocation is done once and only once.
    However, you can do that with vectors.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    thanks.
    I have another question. I am trying to copy a smaller array in to a larger one. it is 2-D. but I want the last row and column in the bigger array to be empty. (the larger one is one row and col larger than the smaller)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SpockRox08 View Post
    thanks.
    I have another question. I am trying to copy a smaller array in to a larger one. it is 2-D. but I want the last row and column in the bigger array to be empty. (the larger one is one row and col larger than the smaller)
    Define "empty". Every piece of memory in your computer has something in it. You can't make it not store something. Whether it's meaningful is another story, but it will contain some value.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Yeah I know it will have "junk" in it initially. Basically I want to "skip" those spots. so the table will read

    100 101 102 103 104 105 "junk"
    106 107 108 109 110 111 "junk"
    112 113 114 115 116 117 "junk"
    118 119 120 121 122 123 "junk"
    124 125 126 127 128 129 "junk"
    "junk" "junk" "junk" "junk" "junk"

    what i have just copies the numbers in order and it won't seem to skip

    Code:
        for(int i=0; i<ro; i++)
        {
            for( int j=0; j<co; j++)
            {
                    augTable[i*co+1+j]=this->table[i*co+j];
    
                
            }
    
        }
    
        for(int i=0;i<=this->rows;i++)
    	{
    		for(int j=0;j<=this->cols;j++)
    		{
    			cout<<augTable[i*(this->cols+1)+j]<<" ";
    		}
    		cout<<endl;
    	}
    and it looks like this

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    augTable[i*co+1+j]
    Oh for the love of Pete.
    Code:
    augTable[i*(co+1)+j]

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    thanks

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> augTable[i*co+1+j]=this->table[i*co+j];

    Notice how the subscripting changed between the two tables.

    Let i == 1 and j == 2 and co == 3

    i*co+j == 3+2 == 5
    i*co+1+j == 3+3 == 6

    Neither is this different:
    i*(co+1)+j == 4+2 == 6

    So that could be part of the problem.

    Since the old matrix is a flat array like the new matrix, a simple copy based on the old dimensions should be enough to ensure a good copy.

    for (i = 0; i < (ro*co); i++) augtable[i] = table[i];

    Although, it may still look like some elements that used to be in, say [1][0], are now in [0][3] when you print it out. Most likely a cosmetic problem only, since if you used the old dimensions, the table would appear normal with empty cells at the end.

    Nothing you can do without changing your data structure.
    Last edited by whiteflags; 12-15-2010 at 12:11 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whiteflags View Post
    >> augTable[i*co+1+j]=this->table[i*co+j];

    Notice how the subscripting changed between the two tables.

    Let i == 1 and j == 2 and co == 3

    i*co+j == 3+2 == 5
    i*co+1+j == 3+3 == 6

    Neither is this different:
    i*(co+1)+j == 4+2 == 6

    So that could be part of the problem.

    Since the old matrix is a flat array like the new matrix, a simple copy based on the old dimensions should be enough to ensure a good copy.

    for (i = 0; i < (ro*co); i++) augtable[i] = table[i];

    Although, it may still look like some elements that used to be in, say [1][0], are now in [0][3] when you print it out. Most likely a cosmetic problem only, since if you used the old dimensions, the table would appear normal with empty cells at the end.

    Nothing you can do without changing your data structure.
    I have to say, whiteflags, this post makes no sense.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Why not?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whiteflags View Post
    Why not?
    You correctly note that it both of these are flat tables, then proceed to give a "solution" that works in every case except when you have flat tables. If you want to skip the last element of a row, you cannot just walk through a flat table element by element filling as you go -- you must allow for skipping that element.

    And of course if you use the old dimensions, you won't see any empty elements at the end -- there's no room for the empty elements in the old dimensions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 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, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM