Like Tree1Likes
  • 1 Post By nonoob

Copying an array into another

This is a discussion on Copying an array into another within the C Programming forums, part of the General Programming Boards category; Hi i'm currently working on trying to create a concentric magic square for an array of size n. So far ...

  1. #1
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22

    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.

    Can someone please help me here.

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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Consider this as a prototype for a loop

    Code:
    for ( outer = offsetPosition, inner = 0 ; inner < innerMax ; outer++, inner++ )
      larger[outer] = smaller[inner];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    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. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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];
    Alienchicken likes this.

  5. #5
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    thanks. I got it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying an array into another
    By Alienchicken in forum C Programming
    Replies: 0
    Last Post: 04-13-2011, 09:55 PM
  2. Array Copying
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 03-01-2011, 03:18 PM
  3. copying to an array
    By tedderry in forum C Programming
    Replies: 4
    Last Post: 01-19-2011, 05:06 PM
  4. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  5. array copying
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21