solving find more comfortable and when I excute ./generate 1000 50 | ./find 127 it gives me output like this : haystack[0] = haystack[1] = haystack[2] = haystack[3] = haystack[4] = . . . haystack[1000] = Didn't find a needle in a haystack. it's weird why it does run this way anyone has an idea what is the problem?


That output is as expected.

When you type in numbers manually, the terminal will echo the typed characters.

When using pipes | or input redirection like < numbers.txt, there is no local echo, therefore you don't see the numbers even though they were passed to the programme.

If the result Didn't find a needle in a haystack. is wrong, that might be due to your code not working correctly.

To locate the error, there can be multiple ways.

One would be to disable sorting and try ./generate 1000 50 | sort -n | ./find 127. sort -n sorts numerically. If that works, you have a mistake in sort. If it doesn't, you have a mistake in search. In either case, you might have made mistakes in both.

Another way to specifically debug sort is to print the array before and after sorting, and testing that with short (<10 numbers) arrays. Specifically watch out for unsorted parts and numbers of input no longer present in output (for example due to duplication of another number).

Also, using debug50 and stepping through code line by line might help once you've narrowed down the location of the bug. In this case, with sort and search called exactly once with well-known input, pen and paper would do as well.

You must log in to answer this question.

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