How to find if value exists in array?

Non-platform specific questions.

How to find if value exists in array?

Postby lverona » Sun Jul 01, 2012 11:20 am

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?
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: How to find if value exists in array?

Postby lverona » Sun Jul 01, 2012 2:47 pm

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.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: How to find if value exists in array?

Postby skydereign » Sun Jul 01, 2012 6:27 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: How to find if value exists in array?

Postby lverona » Sun Jul 01, 2012 8:18 pm

It gives me this:

Warning line 9: Trying convert from 'const * long int' to '* int'
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest