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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>

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<<world[i][j];

		}
		cout<<endl;
	}

}

void clear_screen();
int main(){

	char dummy;
	

	init();

	do{
			
		clear_screen();

		print();

		if(GetAsyncKeyState(VK_RIGHT)){
			
			dir=4;
		}
		if(GetAsyncKeyState(VK_LEFT)){
			dir=2;
		}
		if(GetAsyncKeyState(VK_UP)){
			dir=3;
		}
		if(GetAsyncKeyState(VK_DOWN)){
			dir=1;
		}

		update();



		Sleep(100);


	}while(true);

	cin>>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
@vlad from moscow

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.
@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.


@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.

@ Vlad
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.
@Vlad,

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

1
2
3
4
5
6
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

1
2
3
4
5
#include <iterator>
#include <algorithm>
....

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

Thanks Vlad,

I will give this a shot tonight after work, see what I come up with. It however seems to make more sense than going 4 loop deep, which was really confusing.

Mike
Topic archived. No new replies allowed.