# Thread: Copying an array into another

1. ## Copying an array into another

Hi i'm currently working on trying to create a concentric magic square for an array of size n.

So far what i have done in my method was determine the smalles magic square i can generate.

i.e if n is even the smallest magic square i can make is is a 4 x4
and if n is odd a 3 x 3

however i'm having trouble to copy the elements of the smallest magic square into the nxn array.

For example give a n=7

i have generated the 3x3 magic square:
8 1 6
3 5 7
4 9 2

and i want to place this magic square in the middle of the 7 x 7 array as follows

0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 8 1 6 0 0
0 0 3 5 7 0 0
0 0 4 9 2 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

here is the snippet of my code for it:
Code:
```for(i=m/2-1;i<n+2;i++) {
for(j=m/2-1;j<n+2;j++) Magic[i][j]=innerMagic[i-2][j-2];

}```
it doesn't work for all other cases.

Please go easy on me. I'm new to C programming

2. Consider this as a prototype for a loop

Code:
```for ( outer = offsetPosition, inner = 0 ; inner < innerMax ; outer++, inner++ )
larger[outer] = smaller[inner];```

3. More of the code would likely help us with context.

You don't tell us what 'm' is - is that the size of the major array, so is that '7' in your example that works?

Looking quickly, this construct in your for loop gives me concern:
Code:
`i<n+2`
Why '+2'?

It seems the max for the loop should somehow be related to the sizes of the two arrays, and not fixed to being 2 more than one of the arrays.

4. Code:
```int corner = m / 2 - 1;

for(i = corner; i < corner + n; i++)
for(j = corner; j < corner + n; j++)
Magic[i][j]=innerMagic[i - corner][j - corner];```

5. thanks. I got it to work.

Popular pages Recent additions