Swapping contiguous layers in 3d array

Hi guys
I'm really new to C++, so I don't quite get connection between arrays and pointers and how to use it in elegant way. Anyway, I have a 3d array, with four layers filled with 1s,2s,3s and 4s respectively. It's declaration is as follows: double dArray[4][3][3]. And what I need to do is to swap contiguous layers. Could anyone give me any hint? And why can't I perform following assignment: dArray[i]=dArray[i+1];? I get the
'=' : left operand must be l-value
compiler error, but I don't understand why and what it means in this context.
You can't assign arrays, you need to access each single element or use STL containers
you cant copy arrays like that, here is an example of one way to copy the contents of one array to another:
EDIT: yes, this would be the 'access each single element' approach Bazzy is referring to.
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

#include <iostream>
using namespace std;

int main()
{
	int a;
	int array1[10] = {1,2,3,4,5,6,7,8,9,10};
	int array2[10] = {0,0,0,0,0,0,0,0,0,0};
	
	cout << "\n\n";

	cout << "array1: ";
	for(a=0;a<=9;a++)  cout << array1[a] << " ";
	
	cout << "\n\n";
	
	cout << "array2: ";
	for(a=0;a<=9;a++)  cout << array2[a] << " ";
	
	cout << "\n\n";
	
	for(a=0;a<=9;a++) array2[a] = array1[a];// copying contents of array1 to array2
	
	cout << "array1: ";
	for(a=0;a<=9;a++)  cout << array1[a] << " ";
	
	cout << "\n\n";
	
	cout << "array2: ";
	for(a=0;a<=9;a++)  cout << array2[a] << " ";

	return 0;
}





array1: 1 2 3 4 5 6 7 8 9 10

array2: 0 0 0 0 0 0 0 0 0 0 

array1: 1 2 3 4 5 6 7 8 9 10

array2: 1 2 3 4 5 6 7 8 9 10
Last edited on
Obviously, if you're going to be making switches and not just copies, you'll need a temporary place-holder to save the values of array2 before they get replaced by array1's so that you can replace array1 with array2's values.
true, you could work this sort of logic into your programming:
1
2
3
4
5

temporary array = array1
array1=array2
array2=temporary array

Last edited on
Obviously, if you're going to be making switches and not just copies, you'll need a temporary place-holder to save the values of array2 before they get replaced by array1's so that you can replace array1 with array2's values.



True. An example of that is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a, b, c;
int main()
{
a = 20;
b = 25;
swap(&a,&b);
}

void swap(int &a, int &b)
{
c = a;
a = b;
b = a;
} 


i'm not sure if my syntax is exactly correct. I've been playing in java & just switched back over.

& if you need to do it with an array, I suggest you use a for loop.
Last edited on
Topic archived. No new replies allowed.