Thread: Finding elements in an array

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Finding elements in an array

    Hi, I am having trouble figuring this out, hopefully someone can help me out.

    I have an array:

    0|8|38|0|0|0|0|0|0|7|37|0|0|0|0|0|0|

    Lets say someone searches for 6 consequtive 0s starting at 8 or 38.

    If it cant find them in that section before 7,37, I dont want it to go any further, how would I know when to stop?

    Also, lets say I do want it to look for the first 6 consequtive zeroes, and dont care if I skip over to the next section of 0s, so as above, but would go looking furhter into 7,37 section.

    That is just part of the array.

    Thanks.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    If it cant find them in that section before 7,37, I dont want it to go any further, how would I know when to stop?
    When it finds n consecutive 0's, return
    Code:
    #include <stdio.h>
    
    int find_n(int *, int, int, int);
    
    int main(void)
    {
        static int list[] = {0,8,38,0,0,0,0,0,0,7,37,0,0,0,0,0,0};
    
        printf("First 6 0's starting at index %d\n",
            find_n(list, 17, 0, 6));
    
        return 0;
    }
    
    int find_n(int *list, int length, int value, int n)
    {
        int i;
        int count = 0;
    
        for (i = 0; i < length; i++)
        {
            if (count == n)
                return i - count;
    
            if (list[i] == value)
                count++;
            else
                count = 0;
        }
    
        return -1;
    }

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I am now trying to specify a specific place to start the search, but I am having problems with this.

    Thanks

    Code:
    int find_consecutive(int *list, int length, int value, int n, int row_number)
    {
            int i=0;
            int count = 0;
            int *p, *follower,*j;
            printf("IN HERE\n");
             
            p = &list[0];
                    
            while(list[i] != row_number)
            {
                    i++;
                    *p++;
            }
    
            follower = p;
            *follower++;
            
            if(*follower > 0)
                    *follower++;
               
            for(j = follower; j < &list[length]; j++)
            {               
                    printf("J %d\n", *j);
                    if (count == n)
                                  return *j - count;
    
            if (list[*j] == value)
            {       
                count++; 
            }
            else
                count = 0;   
            }
            return -1;
    }
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I realized what I am doing wrong, but not sure how to correct it.

    When using j in the loop, I lose the index position of the array that I want.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left Shift elements of a 2D array
    By w2look in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:13 AM
  2. adding elements in array
    By reb221 in forum C Programming
    Replies: 6
    Last Post: 12-30-2008, 02:41 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM