I agree that it ought to work, and in gdb, the val;ue of the local variable
SmallestElement is correct in all the recursive calls. Something happens
when the outermost call returns.
I'm just guessing, but it might have something to do with the fact your
function exits without returning a value (except when Length == 1, of
course). Try re-writing it something like this:
if Length == 1
return SmallestElement;
else{
// find the smallest element in the rest of the array
temp = recursiveMinimum(array, --Length)
//compare it with smallest found so far
if Array[temp] < value {
SmallestElement = temp;
value = Array[temp];
}
return SmallestElement;
}
I haven't tried this, by the way.
This is irrelevant to the issue, but your algorithm can fail if the list
contains 0. If 0 is in the list, and is smaller than the smallest element
found so far, value becomes 0. In the next recursive call, becaue value is
0, it will automatically become Array[Length - 1], whether Array[Length - 1]
is less than 0 or not. You should perhaps declare a boolean that is true the
first time the function is called, and false otherwise.
_________________________________________________________________
Cheer a special someone with a fun Halloween eCard from American Greetings!
Go to http://www.msn.americangreetings.com/index_msn.pd?source=msne134
|