Thread: Finding All Adjecent Elements in a 2D array in C Language..

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Finding All Adjecent Elements in a 2D array in C Language..

    Hi All. I am working on a project where at one point i am stucked. my Question is for example i have the following 2D array containg 3 different integers..

    2 2 2 2 1
    1 2 2 2 1
    3 3 2 3 2
    3 1 3 3 1
    1 1 2 3 1
    1 3 1 3 3
    what i want is to find the longest adjacent elements chain of array of any number containing in the array. like in the above array the longest chain is of digit 2.

    2 2 2 2
    2 2 2
    2
    Can anyone just Guide me what i have to do for Achieving this Goal. Thanks.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You could have two loops, the outer one to hold the value of the current element value then the inner one steps through the entire array and counts the adjacent occurences of the current element,

    The below definitely will not work and do the whole thing for you and there are gaps in the logic and things you need to figure out in order to complete the task, and the idea shown definitely depends on your data only being a simple data set say, 0 - 20, otherwise i would do things a bit differently. I am not about to do homework, but it should give you some ideas on one approach to start you off

    Code:
    for(i = 0; i < maxVal; i++)
    {
        currentVal = array[i];
        temp = array[i];
        
        for j = 0; j < maxVal; j++)
        {
            
            if(array[j] == array[i])
            {
                adjCount++;
            }
            else
            {
                lastHigh[temp] = adjCount;
                adjCount = 0;
            }
            
        }
        
    }
    Last edited by rogster001; 12-09-2010 at 06:59 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM