Hello all,

I have what is probably a silly question... I want to fill a 2-dimensional array called instantAct with values from another 2-d array called mS.

The two arrays have different dimensions, but they have the same number of values. I want my loop to take values from certain columns (all rows) of mS and put them in instantAct. instantAct will be completely filled.

The problem is that my code is not doing anything... instantAct is remaining empty. I have run cout on mS and there are values in it, and my indexing to get them is correct.

My question is, why is instantAct still empty? Is it NOT possible to use a complicated loop (like my loop for k) to equate two arrays?

I would really appreciate any help.

``````for ( int i = 0; i < array.NumberOfIntervals(); i++ )
{
for (int j = 0; j < nC; j++)
{
for (int k = (nC * xAg) - nC; k < (nC * xAg); k++)
{
instantAct[ i ][ j ] = mS[ i ][ k ];
}
}
}``````

Before I answer, I have a couple questions.

What are the dimensions of the arrays? Are they dynamically allocated or static?

Basically, you really need to post some more relevant code, there's not enough here to work with (for me anyway).

Yeah I feel the same. What is the return value of array.NumberOfIntervals(), and what has it to do with your actual arrays instantAct and mS? And what is xAg?

Thanks Fbody for your help.

The arrays are dynamic. I import their dimensions from some text files (output from another calculation). xAg is 1 here (just an identifier that tells me which column to start getting stuff from in array mS).

Thanks also Emilo35... array.NumberOfIntervals() is a function that tells me how many rows all of the arrays have. If it makes it easier, we can replace that with some constant number.

Sorry you guys that this is such a pain!

``````double **instantAct, **mS;
instantAct     = new double *[ array.NumberOfIntervals() ];
mS             = new double *[ array.NumberOfIntervals() ];
for ( i = 0; i < array.NumberOfIntervals(); i++)
{
instantAct[ i ]     = new double [ nC ];
m_bPS[ i ]          = new double [ nC * nPS ];
}

for ( int i = 0; i < array.NumberOfIntervals(); i++ )
{
for (int j = 0; j < nC; j++)
{
for (int k = (nC * xAg) - nC; k < (nC * xAg); k++)
{
instantAct[ i ][ j ] = mS[ i ][ k ];
}
}
}``````

i would say your best bet is to walk through your code with the debugger and see what is happening.

NathanOliver, I'll take your advice. From what you guys are saying, it sounds like there's no reason that equating two loops where one has complicated indexing should not work. So it must be an error/typo I'm making. Thanks!

An alternative is to turn your array row/column integer into a linear value and then back again using (for example) mod and div.
As an example, if your source array consists of 4 rows x 6 columns, then you can iterate using a for loop across the columns then down the rows (or the other way around, depends on how one array maps to the other), but you use the column and row to compute an "absolute" index. This is how arrays are actually stored in memory.
Assume you go across the columns for each row, the index for a given array item located at the co-ordinates r,c (i.e. row "r", column "c"), the absolute index is 6 * (r % 4) + c.
for row 0, column 0 the index is 6 * (0 % 4) + 0 = 0.
for row 0, column 5 the index is 6 * (0 % 4) + 5 = 5.
for row 1, column 0 the index is 6 * (1 % 4) + 0 = 6.
for row 1, column 5 the index is 6 * (1 % 4) + 5 = 11.
....
for row 3, column 0 the index is 6 * (3 % 4) + 0 = 18.
for row 3, column 5 the index is 6 * (3 % 4) + 5 = 23.

To map this index back to a differently dimentioned array, perform the reverse function starting with the index and calculating the "row and column".

This can be put into a function very easily which will work with any sized arrays.
Better still, because arrays are stored in a linear fashion like this in memory, you probably don't even need to do the reverse function. Just use the linear index as an offset into the target/destination array after casting it's address to a simple int*.

``````int source[R][C];
int dest[X],Y];

int *pdest = (int *)dest;
pdest[(C * (r % R) + c)] = source[r][c];``````