3 ms·
The followup question was "Under what circumstances does this program fail?" so it seems they didn't want fancy input-checking, just a simple algorithm that ass
by jessaustin 12y ago
The followup question was "Under what circumstances does this program fail?" so it seems they didn't want fancy input-checking, just a simple algorithm that assumed positive integers. After all, your algorithm doesn't handle fractional numbers, does it? "6.2" is displayed in the example!
- compiler-guy 12y agoGiven the supplied instructions, it is not possible to write a program that handles floating point correctly, so my guess is that the failure conditions are non-integer numbers in locations 0 or 1. My code also doesn't handle negative integers correctly, but I'm pretty sure there is a way to do it. However, given that it is possible to write a program that works for all integers, and that this is an entry question for Oxford, "As correct as possible given the constraints of the problem" would be the right answer.
- thaumasiotes 12y ago> My code also doesn't handle negative integers correctly, but I'm pretty sure there is a way to do it. It's pretty easy to show by induction that you can't handle negative numbers in the general case. Consider: if memory locations 0 and 1 are both negative, so is their sum. But the only operations available to you are setting a value to 0, and incrementing it. You can't produce a negative number that way.
- btilly 12y agoThis is true. But if you are clever, you can handle the case where you have 2 arbitrary integers whose sum is non-negative! The trick is to zero out a counter, then increment it until it is one of the two starting values. Then start a second counter and increment it together with the other starting value until the second counter reaches the first and the other contains the sum of the two. Finally start a counter at location 2 and increment it until it reaches the value of the register that has the sum.