The exercise was to write a my_find function in C++ using C style constructions known from standard qsort, bsearch like functions. The suggested prototype was:
void* my_find(void *key, void *array, int element_size, int n);The key is a key we are about to find in the array of n elements where each element has the size element_size. The function should return the pointer to the element or null pointer if it does not exist. After over one hour, as an effect of a serious effort of one of my fairly good freshmen I got the following buggy code:
The problem is with the ArrayCOPY pointer which makes no progress with the iteration of outer loop in the case of false match.
Instead of correcting the above I would like to give similar code where the inner loop is a separate function thus making the whole thing easier to code:
Conclusion
The idea behind the student's solution was correct from the early beginning. The code seemed to be ready after approximately 15 minutes. Over next 40 minutes unsystematic (and unsuccessful) attempts were made to find and correct bugs. The point is: even if the task seems to be easy and straightforward, you need to keep calm and follow the rules (good programming practices) as usually.