473,569 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(my vet, 5, 2, index))
printf ("\nNumber %d found at position %d ", key, index);
else
printf ("\nNumber %d not found", key);

return 0;
}

/*** Code ends here ***/

Jun 27 '08 #1
3 1840
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(my vet, 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(my vet, 5, 2, &index))

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

--
Jim Langston
ta*******@rocke tmail.com
Jun 27 '08 #2
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
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
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.

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3083
by: kaeli | last post by:
I have had a little free time lately to revisit a problem I have with the 3 column layout plus a header and footer. See this example: http://glish.com/css/7.asp There is a header and 3 columns. Pretend there's a footer at the bottom, too. :) Here are the issues I have been unable to solve without tables _somewhere_
6
7109
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3 fields on it: Date (DateSpan1 and DateSpan2), Originator, and GroupName. I have added a button that triggers a query and uses those fields as its...
7
5440
by: Timo Haberkern | last post by:
Hi there, i have some troubles with my TSearch2 Installation. I have done this installation as described in http://www.sai.msu.su/~megera/oddmuse/index.cgi/Tsearch_V2_compound_words <http://www.sai.msu.su/%7Emegera/oddmuse/index.cgi/Tsearch_V2_compound_words> I used the german myspell dictionary from...
2
3941
by: littlegirl | last post by:
hi guys can some one help me here i have to accept a number and preform a linear search for the numbers and if its not one of the number it has to say its invalid #include <iostream> using namespace std; int searchlist (int,int,int);
13
4323
by: Massimo Fabbri | last post by:
Maybe it's a little OT, but I'll give it try anyway.... I was asked to maintain and further develop an already existing small company's web site. I know the golden rule of "eternal" URIs, but in this case changing them cannot be avoided as they were badly chosen when thwe site was first delevoped: URLs with spaces, typos, etc. So I have...
3
10451
by: ntuyen01 | last post by:
Hi All, Does anyone has examples for Linear Regression formula using c#. Or any good third party software create this. Thanks in advance Regards, Ted
1
1666
by: geebanga88 | last post by:
HI i have a method that performs a linear search to find a given vowel. The vowel parameter is a given vowel, while vowel is the array with all the vowels. public static void DisplayFirstAppearance (char vowel, char vowels) { boolean element_found = false; int index; for (index = 0; element_found ==...
3
5996
by: jivelasquezt | last post by:
Hi all, I'm new to this group so I don't know if this question has been posted before, but does anyone knows about linear/integer programming routines in Python that are available on the web, more specifically of the branch and bound method. Thanks, Jorge Velasquez
55
6428
by: copx | last post by:
Can anyone point me to a simple, fast RRNG function to generate random ints within a specified range? It is important that each value within the range has the same probability (uniform distribution). I do not want to use the unreliable rand() function, but I do not want to bloat my code with something as complex as MT either. I am just looking...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.