Trying to assign array values to another array
Hey all. I'm new to C++ programing, although not stranger to programing at all (I have a lot of experience in Java and PHP). Anyway, I'm trying to assing values of two-dimensional array to a previously declared array. I'm using the basic formula:
Let's suppose the array with the values I wanna copy are:
Code:
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
and then I have the other array:
The obvious way of doing it would be:
Code:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
array2[i][j] = array1[i][j];
}
}
Yeah, but it ain't working. I'll try to explain my best the results:
When i = 0 and j = 0, array2 value becomes {{1,0,0},{1,0,0},{1,0,0}}. See the problem already? It assings array1[0][0] to array2[0][0], array2[1][0] and array2[2][0], whereas it should assing it to array2[0][0] only. By the time it reaches i = 0 and j = 2, array2 will be {{1,2,3},{1,2,3},{1,2,3}}.
When the program has i = 1 and j = 0, it array2 will be {{4,2,3},{4,2,3},{4,2,3}}. You see? It assigned array1[1][0] to array2[0][0], array2[1][0] and array[2][0] again! By the time i = 2 and j = 2, array2 = {{7,8,9},{7,8,9},{7,8,9}}.
If you think of both arrays as matrices, when an element is assinged from the first matrix to the second, it is assigned to all lines in that collumn instead of the desired line and collumn.
So, I kindly ask you guys: WHAT THE HELL IS GOING ON?