What’s the best algorithm to find the smallest non zero positive value from a fixed number (in this case 3) of values or return 0 if there are no positive questions?
My naive approach is below (in Delphi, but feel free to use whatever you like), but I think there’s a more elegant way.
value1Temp := MaxInt; value2Temp := MaxInt; value3Temp := MaxInt; if ( value1T > 0) then value1Temp := value1; if ( value2 > 0) then value2Temp := value2; if ( value3 > 0) then value3Temp := value3; Result := Min(value1Temp, Min(value2Temp, value3Temp)); if Result = MaxInt then Result := 0;
Edit: Sorry added what’s needed if there are no positive numbers. I thought I had it in there before, but must have missed it.
I’d do this:
If you want it in a loop with an arbitrary number of questions, then:
If you want the value array to be zero-based, change the for loop to be: 0 to N-1
I think this code makes it very clear exactly what is being done.
Putting the ‘then’ statements on the same line makes the code look cleaner in this simple case, but feel free to indent the ‘then’ statements onto the next line if you feel it’s necessary.