I was wondering if I could add a row and a column to an array while the program is running?
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.
well you have you j and i the wrong way around in the second function is one problem
Thought for the day:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
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'"
You can't do that with arrays. The allocation is done once and only once.
However, you can do that with vectors.
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)
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
and it looks like thisCode: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; }
Oh for the love of Pete.Code:augTable[i*co+1+j]
Code:augTable[i*(co+1)+j]
thanks
>> 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.
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.