Hello,
I am a new programmer. I am trying to copy the elements of this 20x3 array into a 1x60 or 60x1 array

an[60]=A[20][3]

one of my attempts is:

``````for (i=0;i<20;i++) {
for(j=0;j<3;j++){
at1[i]=A[i][j];
at2[i]=A[i][j];
at3[i]=A[i][j];}
}
an[60]={at1,at2,at3};``````

## All 8 Replies

Add another variable k to keep track of the an[] array position.

i tried
for(int z=0;z<60;z++){
an[z] ={at1,at2,at3}; }
but it doesnt work

Of course not. Go back to your first post and fix it there.

You really need to think when programming. Not just throw something together for the pure joy of editing your code.

when i attempt to do it this way, all elements of the new array is constant

``````for(int z=0;z<60;z++){
for (i=0;i<20;i++){
for(j=0;j<3;j++) {
an[z]=A[i][j]; } // j
} // i
} //z``````

You only need 2 for loops, not 3. Figure out a different way to initialize and increment what you have called z.

And please start using [code=syntax] ... code tags ... [/code]

You only need 2 for loops, not 3. Figure out a different way to initialize and increment what you have called z.

Exactly. What you have will go through the loops 3600 times to move 60 values. A bit overkill.
Using an index does not have to be defined via a loop. You can just keep track of it without looping by adding one to it.

Initialize the z variable before the loops and increment it in each iteration of the j loop.

You need to realize the conversion between 2d array and 1d array.
Namely, that 1D_Row = 2dRow * MAX_ROW + 2D_Column;
In code it would look like this :

``````for(int row = 0; row < MAX_ROW; ++row){
for(int col = 0; col < MAX_COL; ++col){
Array1D[col + row*MAX_ROW] = Array2d[row][col]
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.