Argh, to add some more. You don’t actually need three states for your example, since you are only keeping a running pair of max and min, and once you have one input everything is valid. So the switch can reduce to initial, and running - and thus can be an if. But overall the idea remains a core part of how any code processing an input stream should be written.
Any input processing code that turns into a massive set of cascading if statements is a prime candidate for revision.
Interesting, I think I get the idea, I’ll look at it again in a couple days. Is barf a highly technical term?
Unfortunately it’s the reader, not the poster whose skin setting matters. Even if you revert to the old v3.7.3, you still have to use PHP instead of CODE if you want Sultan skinheads to be able to read your post.
~ ~ ~ ~
As a general comment about coding, I like to avoid duplication where possible. For example, you invoke readInt() three places; I’d reduce that to two or even one(*). Although that will result in a shorter program, it may take you longer to write it! But the extra effort may pay for itself if the code ends up easier to read or modify. … Or … what Francis Vaughan said.
-
- Initializing min and max may become a problem if you reduce to a single readInt(). I solve this by pre-initializing min = 99999999, max = -99999999, or, more correctly, min = INT_MAX, max = INT_MIN.
I don’t think that’s a very accurate translation of what you wrote symbolically. First of all, p and q are statements that can be either true or false (i.e., Boolean expressions), not necessarily predicates applied to the same subject. In otherwords, there need not be a thing which we’re suggesting is p or q. They can just as easily be unrelated statements like “bananas are yellow” and “elephants are large”.
But even putting that aside, the symbols you wrote don’t say “neither p nor q” – in other words, they don’t say both p and q are false… Rather, they describe a case where at least one of p or q is false.
Let me try to give you a better translation:
!p || !q = “Either p is not true, or q is not true*”
!(p && q) = “It is not the case that both p and q are true”
See why these are equivalent? If at least one of p and q is false, then it is not the case that both are true. And, in the other direction, if it isn’t the case that both are true, then it must be the case that at least one of them is false.
- Or both – contrary to casual English where we sometimes use “or” to mean “exclusive or”.