# Dynamic 2-d Array

• 12-15-2010
SpockRox08
Dynamic 2-d Array
I was wondering if I could add a row and a column to an array while the program is running?
• 12-15-2010
rogster001
well you have you j and i the wrong way around in the second function is one problem
• 12-15-2010
chameleons
You can't do that with arrays. The allocation is done once and only once.
However, you can do that with vectors.
• 12-15-2010
SpockRox08
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)
• 12-15-2010
tabstop
Quote:

Originally Posted by SpockRox08
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.
• 12-15-2010
SpockRox08
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
• 12-15-2010
tabstop
Code:

`augTable[i*co+1+j]`
Oh for the love of Pete.
Code:

`augTable[i*(co+1)+j]`
• 12-15-2010
SpockRox08
thanks
• 12-15-2010
whiteflags
>> 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.
• 12-15-2010
tabstop
Quote:

Originally Posted by whiteflags
>> 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.
• 12-15-2010
whiteflags
Why not?
• 12-15-2010
tabstop
Quote:

Originally Posted by whiteflags
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.