Page 1 of 1

How to find if value exists in array?

PostPosted: Sun Jul 01, 2012 11:20 am
by lverona
Hey fellas!

In PHP there is a function in_array() which says whether a particular value is present in an array or not. Is there anything of that sort in GE or C for that matter?

The particular problem is that I have a robot going through a maze. I need it to remember to which coordinates it has been already and store it in an array. I want the robot, when choosing a direction, check if the coordinate before him is in his memory, meaning that he visited it before, and not go there. How do I do that?

Re: How to find if value exists in array?

PostPosted: Sun Jul 01, 2012 2:47 pm
by lverona
Found this function:

Code: Select all
int find_index(int a[], int num_elements, int value)
{
   int i;
   for (i=0; i<num_elements; i++)
   {
    if (a[i] == value)
    {
       return(value);  /* it was found */
    }
   }
   return(-1);  /* if it was not found */
}


Seems to work although GE issues a conversion warning.

Re: How to find if value exists in array?

PostPosted: Sun Jul 01, 2012 6:27 pm
by skydereign
So you are just looking for a search function. That one that you have is the standard search. It shouldn't have any conversion warnings though.

Re: How to find if value exists in array?

PostPosted: Sun Jul 01, 2012 8:18 pm
by lverona
It gives me this:

Warning line 9: Trying convert from 'const * long int' to '* int'