### 4x for loop, 2x2D array = Confusion

Hi guys,

As the title suggest, I have a bit of an issue. I want to move a 2d array inside another 2d array and so far i have the following:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168`` `````` #include #include #include #include #include #include using namespace std; int dir=0; char world[20][20]; char person[3][3]={ {'0','0','0'}, {'0','C','0'}, {'0','0','0'} }; void init(){ for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ world[i][j]=' '; } } } void update(){ for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ for(int k=0;k<3;k++){ for(int l=0;l<3;l++){ if(dir==1){ if(i==k+10 && j==l+10){ world[i-1][j]=person[k][l]; } } else if(dir==2){ if(i==k+10 && j==l+10){ world[i+1][j]=person[k][l]; } } else if(dir==3){ if(i==k+10 && j==l+10){ world[i][j-1]=person[k][l]; } } else if(dir==4){ if(i==k+10 && j==l+10){ world[i][j+1]=person[k][l]; } } } } } } } void print(){ for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ cout<>dummy; return 0; } void clear_screen(){//need windows header file for this HANDLE handle_out; COORD position; handle_out=GetStdHandle(STD_OUTPUT_HANDLE);//std output handle is the monitor position.X=0; position.Y=0; SetConsoleCursorPosition(handle_out,position);//sets cursor in the specified position }``````

There's movement, obviously it's not correct. Does anyone have any idea on how to free up the movement of the "person" array?

THanks,

Mike
You may try posting in nehe forums, they are good with OpenGl and graphics etc:
http://nehe.gamedev.net/
It is totally unclear what you are trying to do in function update. It is an example of how code shall not be written.
Last edited on

I was just playing around with it, it's not mean to be a serious program. The algorithm is the issue. All I want to do is move the person array, inside the world array, with each key stroke.
It is not importnat whether this is a serious program. The style of programming does depend on whether the prjgram is serious or not. This says that you can not program.
I do not understand why you are using four loops. In fact you need copy rows from person to world. All two-dimensional arrays are copied with two loops: one - for rows and other - for elements in a row.
Lay off. If you look closely to those 4 for loops you'd see that there are only 2 loops per array, the outer 2 for the world, the inner 2 for the person. He needs to re-think his logic.

@toomanystars
Perhaps if you tried thinking about printing things in layers (e.g. first the world, then the person) you might have more luck.
 @Ikaron @Vlad Lay off. If you look closely to those 4 for loops you'd see that there are only 2 loops per array, the outer 2 for the world, the inner 2 for the person. He needs to re-think his logic.

It seems that you have the same level of mastering the programming as the author of the thread, There shall be only two loops for copying one two-dimensioanl array into another.

I'm fully aware of that. Once again, he needs to re-think his logic, for if he does he and spends enough time on it he'll come to that realization himself. But not that you seem to actually care about that. You can take your "superiority" and shove it up your ass.

@ toomanystars
Feel free to send me a PM if you'd like me to help nudge you towards figuring out the problems with your program and get away from this thread.

I appreciate the contempt, and I total agree with you that I can't program. You will notice I never claimed otherwise. So, besides these obvious points, I am not sure what your point is, except maybe in some small way make yourself freel superior.

Sleep well tonight knowing your mastery of C++ is superior to an accountant who spends on average two hours a night working on his hobby.

Mike
@toomanystars: dont be disappointed because of cold replies from some cold person, there are knowledgable people in this forum( who believe learning is a continuous process and mastery is just an illusion), they will eagerly help u, and they are friendly.
Thanks anirudh sn,

The majority of the people here are great, I have no issue there. I can assure you that at my age someone being "cold" in an online forum does not concern me.

Mike
 @toomanystars

At first try to write stand-alone function that copies one two-dimensional array into another two dimensional array without using the variable that keeps the direction of pressed keys because such a function shall not depend on some other code.

To copy one row of a two-dimensionnal array into a row of other two dimensional array it is enough to use only one loop. There are several approaches.

For example

 ``123456`` ``````const size_t M = SomeValue; const size_t N = OtherValue; int a[M] = {}; int b[N]; for ( size_t i = 0; i < M; i++ ) b[i] = a[i];``````

Another way is to use pointters

`for ( int *p = a, *q = b; p != a + M; ++p, ++q ) *q = *p;`

The same can be done with using standard algorithm std::copy

 ``12345`` ``````#include #include .... std::copy( std::begin( a ), std::end( a ), std::begin( b ) );``````

where std::begin( a ) is equivalent to a (that is to the pointer to the first element of array a ) and std::end(a ) is equivalent to a + M.

So to copy not only a row but a whole two-dimensional array into another two-dimensional array you need only to add an outer loop for rows of arrays