PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Problems with a simple linear search
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problems with a simple linear search

Réponse
 
LinkBack Outils de la discussion
Vieux 09/05/2008, 16h31   #1
nembo kid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problems with a simple linear search

I have an issue with a simple function that has to make a linear search
for a key into an array.

If the key is found in the array, the function it has to return 1 to the
caller and pass array index through a out parameter.

The issue is that the out parameter is not being updated.

If I return the position to the caller (instead to use a out parameter)
all is ok.

But I would to use a boolean (1 or 0) value as return value to the
caller: 1 stays for found, 0 stays for not found.

Thanks in advance and apologies for my bad english.

Here it is my piece of code.

/*** Code starts here ***/

/* Search for number 'x' in array v[] */
/* If 'x' found pass array position through 'pos' parameter */
/* and returns 1 to caller; else returns 0 */

int linsearch (int v[], int nmax, int x, int pos)
{

int i;

for (i=0; i<nmax; i++)
{
if(v[i]==x)
{
pos=i; /* pass the index where found */
return 1; /* returns 1 (found) */
}
}

return 0; /* Exit from for...so returns 0 */
}

int main (void)
{
int myvet[] = {5,4,3,2,1};

int key = 2; /* Number to find */

int index=0; /* Actual parameter that holds index of element */

if(linsearch(myvet, 5, 2, index))
printf ("\nNumber %d found at position %d ", key, index);
else
printf ("\nNumber %d not found", key);

return 0;
}

/*** Code ends here ***/

  Réponse avec citation
Vieux 09/05/2008, 16h40   #2
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with a simple linear search

nembo kid wrote:
> I have an issue with a simple function that has to make a linear
> search for a key into an array.
>
> If the key is found in the array, the function it has to return 1 to
> the caller and pass array index through a out parameter.
>
> The issue is that the out parameter is not being updated.
>
> If I return the position to the caller (instead to use a out
> parameter) all is ok.
>
> But I would to use a boolean (1 or 0) value as return value to the
> caller: 1 stays for found, 0 stays for not found.
>
> Thanks in advance and apologies for my bad english.
>
> Here it is my piece of code.
>
> /*** Code starts here ***/
>
> /* Search for number 'x' in array v[] */
> /* If 'x' found pass array position through 'pos' parameter */
> /* and returns 1 to caller; else returns 0 */
>
> int linsearch (int v[], int nmax, int x, int pos)


pos is being passed by value. A copy of it is made and passed into the
function. pos becomes a local variable here, only visible during the
lifetime of the function. Any changes to this local pos will never be seen
by the caller.

> {
>
> int i;
>
> for (i=0; i<nmax; i++)
> {
> if(v[i]==x)
> {
> pos=i; /* pass the index where found */


This only changes the local copy of pos, the local variable. The local
variable pos disappears when this function is returned (the next line).

> return 1; /* returns 1 (found) */
> }
> }
>
> return 0; /* Exit from for...so returns 0 */
> }
>
> int main (void)
> {
> int myvet[] = {5,4,3,2,1};
>
> int key = 2; /* Number to find */
>
> int index=0; /* Actual parameter that holds index of element */
>
> if(linsearch(myvet, 5, 2, index))


index is passed by value. The contents of index (0) is copied and passed to
the function.

> printf ("\nNumber %d found at position %d ", key, index);
> else
> printf ("\nNumber %d not found", key);
>
> return 0;
> }
>
> /*** Code ends here ***/


Okay, so the question is, how to fix this? You need to pass a pointer to
the function you wish to change. Change the signature of linsearch to:
int linsearch (int v[], int nmax, int x, int* pos)
now pos is a pointer to an integer. Since you want to change what the
pointer points to, not the pointer itself, you need a level of indirection.
*pos=i; /* pass the index where found */

* used in this method is to dereference the pointer. Basically saying,
"what pos points to".

One other change is needed when you call linsearch. Instead of passing the
variable, you need to pass a pointer to the variable:
if(linsearch(myvet, 5, 2, &index))

& used this way is "adress off" basically saying, "pass the address of
index".

--
Jim Langston
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 09/05/2008, 16h41   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with a simple linear search

nembo kid wrote:
> I have an issue with a simple function that has to make a linear search
> for a key into an array.
>
> If the key is found in the array, the function it has to return 1 to the
> caller and pass array index through a out parameter.
>
> The issue is that the out parameter is not being updated.
>
> If I return the position to the caller (instead to use a out parameter)
> all is ok.
> [...]


This is Question 4.8 in the comp.lang.c Frequently
Asked Questions (FAQ) list, <http://www.c-faq.com/>. The
question's title in the FAQ doesn't seem at first blush to
have much to do with your problem, but trust me and read on.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 09/05/2008, 17h01   #4
nembo kid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problems with a simple linear search

Jim Langston ha scritto:

> & used this way is "adress off" basically saying, "pass the address of
> index".


Very clear. I fix it by using a pointer.
Thanks again to others too.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h13.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13287 seconds with 12 queries