0

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};

Edited by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

5
Contributors
8
Replies
14
Views
7 Years
Discussion Span
Last Post by firstPerson
Featured Replies
  • 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 : [code] for(int row = 0; row < MAX_ROW; ++row){ for(int col = 0; col < MAX_COL; ++col){ Array1D[col + row*MAX_ROW] = … Read More

0

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

0

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.

0

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

Edited by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

0

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]

Edited by Fbody: n/a

0

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.

0

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

2

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]
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.