0

for negative results ,its O.K.

0 , 1 , 2 , 3 , 4 , 4 , 5 , 6 , 7 , 7 , 8 , 9 , 11 , 22 , 33 , 66 , 78 , 88 , 455 , 999 , 
lower =0 ,upper = 19 ,mid = 9 ,values[mid] = 7 
lower =10 ,upper = 19 ,mid = 14 ,values[mid] = 33 
lower =15 ,upper = 19 ,mid = 17 ,values[mid] = 88 
lower =15 ,upper = 16 ,mid = 15 ,values[mid] = 66 
lower =15 ,upper = 14 ,mid = 14 ,values[mid] = 33 
returns false 0
Didn't find needle in haystack.

But for positive results

The command  ./generate 1000 50 | ./find 127 generates:-
value = 127, lower =0 ,upper = 999 ,mid = 499 ,values[mid] = 32474 
value = 127, lower =0 ,upper = 498 ,mid = 249 ,values[mid] = 15570 
value = 127, lower =0 ,upper = 248 ,mid = 124 ,values[mid] = 8178 
value = 127, lower =0 ,upper = 123 ,mid = 61 ,values[mid] = 4185 
value = 127, lower =0 ,upper = 60 ,mid = 30 ,values[mid] = 1812 
value = 127, lower =0 ,upper = 29 ,mid = 14 ,values[mid] = 975 
value = 127, lower =0 ,upper = 13 ,mid = 6 ,values[mid] = 423 
value = 127, lower =0 ,upper = 5 ,mid = 2 ,values[mid] = 169 
value = 127, lower =0 ,upper = 1 ,mid = 0 ,values[mid] = 94 
value = 127, lower =1 ,upper = 1 ,mid = 1 ,values[mid] = 127 
returns true 1

Didn't find needle in haystack.

The first part printf in the search function, which gives "returns true"

but the find function prinf gives "Didn't find needle in haystack." which indicates that the code finds 127 & the following code prints "returns true 1"

printf("returns true 1\n"); return true;

the error may be in the following code

bool search(int value, int values[], int n)
{
    int lower = 0;
    int upper = n -1   ;
    return searchrecur(value, values , lower , upper);

./generate 10 50 | ./find 1222

1222 , 9601 , 29632 , 30275 , 38284 , 46108 , 46592 , 52692 , 64187 , 64657 ,

FIRST ITERATION GIVES:

value = 1222, lower =0 ,upper = 9 ,mid = 4 ,values[mid] = 38284

SECOND ITERATION GIVES:

>    value = 1222, lower =0 ,upper = 3 ,mid = 1 ,values[mid] = 9601 

THIRD ITERATION GIVES:

>    value = 1222, lower =0 ,upper = 0 ,mid = 0 ,values[mid] = 1222 

WHILE THE FINAL RESULTS COMES LIKE THIS:

THE FINAL ITERATION GIVES:

>    true 3

THEN THE CONTROLL RETURNS TO THE LOOPING:

THIRD LOOP CLOSES FIRST

>     final return is  true

SECOND LOOP CLOSES NEXT

>    final return is  (null)

AND FIRST LOOP CLOSES LAST

>    final return is  (null)



GIVEING THE FINAL WRONG RESULT

>    Didn't find needle in haystack.

THE QUSION IS WHY IS THAT!!!!!!!!

AND HOW TO CONTROLL THE FLOW OF THE RESULTS OF THE PROCESS TO BE IN ONE DIRECTION.
2
  • Just to be sure, you are actually returning true in your searchrecur() function after you print that statement? – curiouskiwi Jan 25 '15 at 20:12
  • Yes, I added that to the end of my post.In the example, 3 loops close in the reverse order overwriting the final correct answer with the first step result ,which obviously did not find the answer!!! – RNA Jan 29 '15 at 21:57
0

The final return, (i.e. True) was masked by the return of the first loop, (i.e. False) GIVEING THE FINAL WRONG RESULT Didn't find needle in haystack. My solution was to proceed the recursive call (i.e. modified call to searchrecur(value, values , lower , upper);) with return.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .