Thread: how to find if array is cyclic

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    Red face how to find if array is cyclic

    Hello,
    I want to write a code, it gets two arrays, each one got size 10.
    I have to check if the second array is cyclic comparing to the first array, and I have to find and return the cyclic number, to be more clear heres an example:

    first array: 1 | 3 | 5 | 9

    second array: 5 | 9 | 1 | 3 cyclic number=2


    can someone show me how to write this code using basic commands only (while if for ... ) ?

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    The basic way is to have a doubly-nested loop :
    Code:
    for each offset from 0 to length-1
    {
      for each index from 0 to length-1
      {
        if a[offset+index mod length] not equal b[index] exit inner loop and start with next offset
      }
      if you didn't exit the loop prematurely, all elements match with this offset so print the offset and exit
    }
    print no match found
    A bit more advanced :
    Create a 3rd array which is twice the length of the first array.
    Copy the contents of the first array to the start of the third array, and then copy it again to the middle so you have two copies back to back of the first array. Loop through each offset value from 0 to size of array-1 and use memcmp to compare the second array to the length 10 array starting at array_three[offset].
    When you get a match you've found the offset.

    You could also use cross-correlation between the two arrays and look for the first peak - that will work even if there's some noise in your data.
    Last edited by KCfromNC; 05-04-2012 at 02:07 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Can You explain what offset mean? (I'm new to C )

    Also, if I have the same question but the difference is that I have to determine if the second array is cyclic and was added to it a scalar, for example:

    1 | 3 | 5 | 9

    cyclic number=2 scalar=3 we get: 8 | 12 | 4 | 6

    I have to return cyclic number and the scalar, how is the code different now?

    Thanks
    Last edited by SA80; 05-05-2012 at 03:22 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Offset is just another word for start. It's not C specific either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cyclic dependencies, observer, or...
    By g4j31a5 in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2011, 05:45 AM
  2. Replies: 26
    Last Post: 05-03-2011, 11:52 AM
  3. Cyclic Redundancy Check: disable
    By samuelmoneill in forum C Programming
    Replies: 5
    Last Post: 04-27-2009, 02:29 AM
  4. cyclic buffer
    By Fortune in forum C Programming
    Replies: 2
    Last Post: 04-23-2009, 05:19 AM
  5. Need a little help with Cyclic Numbers Program
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 05:01 PM