473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

can bsearch tell me the index?

bsearch finds me only the first occurrence of something I'm looking for,
but I would like to know the place in the list where it is found.
The index of it's place in the array.
So that I can check if there are more to find on the next places.
I need to find all occurences.
Maybe anyone of you know even a better way?

thanks, in advance!

Michiel Rapati
Nov 14 '05 #1
6 5317
"Michiel Rapati-Kekkonen" <no******@rapix.xx> wrote in message
news:10*************@corp.supernews.com...
bsearch finds me only the first occurrence of something I'm looking for,
but I would like to know the place in the list where it is found.
The index of it's place in the array.
So that I can check if there are more to find on the next places.
I need to find all occurences.
Maybe anyone of you know even a better way?


If you call...

T *where = bsearch(key, base, N, sizeof *base, compar);

....then, assuming non null return, the index is...

size_t index = where - base;

From there, the simplest option is to iterate forwards and backwards from index to find
the equivalents. Or you can write your own customised search function. But that's really
an algorithm issue, and not topical in clc.

--
Peter
Nov 14 '05 #2


Michiel Rapati-Kekkonen wrote:
bsearch finds me only the first occurrence of something I'm looking for,
but I would like to know the place in the list where it is found.
The index of it's place in the array.
So that I can check if there are more to find on the next places.
I need to find all occurences.
Maybe anyone of you know even a better way?


I do believe your approach is the best method for searching an
existing array. Once the array is sorted, use function bsearch
to search for the key value. If it is found, then search the
elements adjacent to the key element for duplicates.

Example using a integer array;

#include <stdio.h>
#include <stdlib.h>

#define SZ 6

void printarray(int *iia,const char *s);
int cmp(const void *v1, const void *v2);
void printfindmore(int *ia, int elemnr);

int main(void)
{
int key, *ip,elem,iarray[SZ] = {3,5,1,3,4,3};

printarray(iarray,"Unsorted");
qsort(iarray,SZ,sizeof *iarray,cmp);
printarray(iarray,"\nSorted Assending");
key = 3;
printf("\n\tSearching for value: %d\n",key);
ip = bsearch(&key,iarray,SZ,sizeof *iarray,cmp);
if(!ip)
printf("The value: %d was not found\n",key);
else
{
elem = ip - iarray;
printf("The value: %d was found at element %d\n",
key, elem);
printfindmore(iarray,elem);
}
return 0;
}

void printarray(int *ia,const char *s)
{
int i;

printf("\t%s\n",s);
for(i = 0; i < SZ; i++)
printf("array[%d] = %d\n",i,ia[i]);
return;
}

int cmp(const void *v1, const void *v2)
{
const int *i1 = v1;
const int *i2 = v2;

return (*i1 < *i2)?-1:(*i1 != *i2);
}

void printfindmore(int *ia, int elemnr)
{
int i;

for(i = elemnr-1; i >= 0;i--)
{
if(ia[i] != ia[elemnr]) break;
printf("Value %d was also found at element %d\n",
ia[elemnr],i);
}
for(i = elemnr+1; i < SZ; i++)
{
if(ia[i] != ia[elemnr]) break;
printf("Value %d was also found at element %d\n",
ia[elemnr],i);
}
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3


Michiel Rapati-Kekkonen wrote:
bsearch finds me only the first occurrence of something I'm looking for,


The Standard says that if 2 of the elements in the array compare equal,
the element that is matched is unspecified. Therefore the match may
not be the first occurrence.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
On Sun, 20 Jun 2004, Michiel Rapati-Kekkonen wrote:
bsearch finds me only the first occurrence of something I'm looking for,
Actually, bsearch will return a pointer to some occurrence. If there is
more than one occurrence it can return a pointer to any of them.
but I would like to know the place in the list where it is found.
The index of it's place in the array.
So that I can check if there are more to find on the next places.
I need to find all occurences.
Maybe anyone of you know even a better way?


If I was searching an array of flogals then the code snippet might look
something like this:

flogals *result;
result = bsearch(key_i_desire,
array_of_flogals,
sizeof array_of_flogals / sizeof array_of_flogals[0],
sizeof array_of_flogals[0], compare_flogals);

Once I do this I can look at result[0]. I can also look at result[1] to
see if it is a match as well. I can also look at result[-1] but I have to
be careful that result != &array_of_flogals[0].

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #5
Even though I thanked you already in advance, I'll do it afterwards, too.
Your tips, they helped me perfectly out.

Michiel
Nov 14 '05 #6
Darrell Grainger wrote:
On Sun, 20 Jun 2004, Michiel Rapati-Kekkonen wrote:
bsearch finds me only the first occurrence of something I'm
looking for,


Actually, bsearch will return a pointer to some occurrence. If
there is more than one occurrence it can return a pointer to any
of them.
but I would like to know the place in the list where it is
found. The index of it's place in the array. So that I can
check if there are more to find on the next places. I need to
find all occurences. Maybe anyone of you know even a better
way?


If I was searching an array of flogals then the code snippet
might look something like this:

flogals *result;
result = bsearch(key_i_desire,
array_of_flogals,
sizeof array_of_flogals / sizeof array_of_flogals[0],
sizeof array_of_flogals[0], compare_flogals);

Once I do this I can look at result[0]. I can also look at
result[1] to see if it is a match as well. I can also look at
result[-1] but I have to be careful that result != &array_of
_flogals[0].


That can be done with code such as:

flogals *resultop, *temp;
.... above code ....
resultop = temp = result;
if (result) {
while ((temp > &array_of_flogals[0]) &&
(0 == compare_flogals(*temp, temp[-1])) )
temp--;
result = temp;
/* set temp to ptr to last+1 of array_of_flogals */
temp = &array_of_flogals[sizeof array_of_flogals /
sizeof array_of_flogals[0]];
while ((resultop < temp) &&
(0 == compare_flogals(*resultop, resultop[1])) )
resultop++;
}
/* Now result through resultop all point to the searched
* for value, and there are no such values outside that
* range, or result and resultop are both NULL. */

(Untested)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7

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

Similar topics

1
by: Ramprasad A Padmanabhan | last post by:
I have written a simple script to search a word in an array But bsearch does not seem to work here. I know I am missing out something very simple , But I am not able to find out what Thanks...
11
by: Ramprasad A Padmanabhan | last post by:
I have got a pretty simple script , that uses bsearch to look for a particular element The problem is , it simply segfaults inside the compare function. I have a similar script that works fine...
4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
2
by: Michiel Rapati-Kekkonen | last post by:
recently I was put on the right trail in the matter of searching in arrays of structs. I got it working, a bit. Unfortunately, as soon as I want more, I'm stuck again: it is basically a...
13
by: val | last post by:
Hi, I found that Windows CE doesn't include bsearch function. Can somebody point me into right direction in order to solve that issue Thanks, val
3
by: c_programmer | last post by:
I have a problem. There is a effective dated list FAMILY_ACCOUNTS stored in the memory. How to achieve the equivalent of the following SQL statement: select * from FAMILY_ACCOUNTS FA where...
2
by: Bit Byter | last post by:
I am hacking some legacy code and have put together a simple test to test some hashing funcs I've written. I now want to do a simplistic timing between the various structs. Here's a snippet: ...
4
by: Steph | last post by:
Hello, I have filled a dynamic array of strings (realloc() + malloc()) char **sMyArray; At the end, I get correctly sMyArray = "STRING_0", sMyArray = "STRING_1", etc... But I'm unable to...
4
by: Amandil | last post by:
Hi, all. I'd like to check whether a certain string (one that I got from a user, or read from a file) is contained in a table of strings. The format of the table is char *table = { "string1",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.