# Finding elements in an array

• 04-05-2003
MethodMan
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.
• 04-05-2003
Brighteyes
Quote:

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; }```
• 04-05-2003
MethodMan
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; }```
• 04-05-2003
MethodMan
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.