I have one more question. This is another interative loop in the chapter on searching that I had to convert to a recursive function.
Should I rewrite this function and break it into to two as I did with the sequential search?
I have the same problem of incrementing the position in the list.
YEP!Code://Function written for Test 5 Question 22 template<class elemType> int orderedArrayListType<elemType>::binarySearch(const elemType& item) { static int first = 0; static int last = length - 1; static int mid = (first + last) / 2; if(first > last || mid == last || mid == first) return -1; if(list[mid] == item) return mid; else if (list[mid] > item) { mid--; return binarySearch(item); } else { mid++; return binarySearch(item); } }//end binarySearch
It looks like I have to rewrite this one two for the same reason. Declaring static variables is ok for the first time through the function but the second time the number, that is in the list, is not found!
BUMMER!



LinkBack URL
About LinkBacks




I didn't mean to offend!