Click here to Skip to main content
15,898,036 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: cout vs. printf? Pin
Rickard Andersson208-Jul-03 2:15
Rickard Andersson208-Jul-03 2:15 
GeneralRe: cout vs. printf? Pin
Rage8-Jul-03 3:23
professionalRage8-Jul-03 3:23 
GeneralRe: cout vs. printf? Pin
Ryan Binns8-Jul-03 3:50
Ryan Binns8-Jul-03 3:50 
GeneralRe: cout vs. printf? Pin
keegan8-Jul-03 3:23
keegan8-Jul-03 3:23 
AnswerRe: cout vs. printf? Pin
AlexO8-Jul-03 3:59
AlexO8-Jul-03 3:59 
GeneralRe: cout vs. printf? Pin
DaveE9th8-Jul-03 8:16
DaveE9th8-Jul-03 8:16 
Questionbinary sort function? Pin
DaveE9th7-Jul-03 23:08
DaveE9th7-Jul-03 23:08 
AnswerRe: binary sort function? Pin
Ryan Binns7-Jul-03 23:28
Ryan Binns7-Jul-03 23:28 
Why doesn't using double work? You only need to make a few changes:
double values[] = { 10.0, 20.0, 25.0, 40.0, 90.0, 100.0 };

int compare (const void * a, const void * b)
{
   double _a = *(double*)a;
   double _b = *(double*)b;
   if( ABS(_a - _b) < 0.00000001 )
      return 0;
   else if( _a < _b)
      return -1;
   else
      return 1;
}

int main ()
{
   double * pItem;
   double key = 40;
   pItem = (double*) bsearch (&key, values, 6, sizeof (double), compare);
   if (pItem!=NULL)
      printf ("%lf is in the array",*pItem);
   else
      printf ("%lf is not in the array",key);
   return 0;
}
Since doubles are not exact, when you compare them, you might want to add in a delta value, where numbers that are within this amount of each other are considered equal, like I've put in the compare function. If you don't want this, then change the first if statement so it is just (_a == _b).

BTW, when you're using ints, I don't recommend just returning the difference between them, rather return -1 if a < b or 1 if a > b or 0 if they are equal. Some implementations of the bsearch() function rely on these values being exact.

Hope this helps,

Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

GeneralRe: binary sort function? Pin
DaveE9th8-Jul-03 0:17
DaveE9th8-Jul-03 0:17 
GeneralRe: binary sort function? Pin
Ryan Binns8-Jul-03 0:23
Ryan Binns8-Jul-03 0:23 
GeneralRe: binary sort function? Pin
Rage8-Jul-03 0:23
professionalRage8-Jul-03 0:23 
GeneralRe: binary sort function? Pin
DaveE9th8-Jul-03 7:58
DaveE9th8-Jul-03 7:58 
GeneralVC++ hangs... Pin
Rage7-Jul-03 22:56
professionalRage7-Jul-03 22:56 
GeneralRe: VC++ hangs... Pin
Toni788-Jul-03 15:20
Toni788-Jul-03 15:20 
Question%1f\n? Pin
DaveE9th7-Jul-03 22:55
DaveE9th7-Jul-03 22:55 
AnswerRe: %1f\n? Pin
Rage7-Jul-03 22:59
professionalRage7-Jul-03 22:59 
AnswerRe: %1f\n? Pin
Cedric Moonen7-Jul-03 23:05
Cedric Moonen7-Jul-03 23:05 
AnswerRe: %1f\n? Pin
Rage7-Jul-03 23:05
professionalRage7-Jul-03 23:05 
GeneralRe: %1f\n? Pin
DaveE9th7-Jul-03 23:55
DaveE9th7-Jul-03 23:55 
GeneralRe: %1f\n? Pin
Rage8-Jul-03 0:39
professionalRage8-Jul-03 0:39 
GeneralRe: %1f\n? Pin
Ryan Binns8-Jul-03 0:50
Ryan Binns8-Jul-03 0:50 
GeneralRe: %1f\n? Pin
Rage8-Jul-03 1:08
professionalRage8-Jul-03 1:08 
GeneralRe: %1f\n? Pin
Ryan Binns8-Jul-03 1:11
Ryan Binns8-Jul-03 1:11 
GeneralRe: %1f\n? Pin
Rage8-Jul-03 1:12
professionalRage8-Jul-03 1:12 
GeneralRe: %1f\n? Pin
Ryan Binns8-Jul-03 1:14
Ryan Binns8-Jul-03 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.