Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!think.com!snorkelwacker.mit.edu!ira.uka.de!fauern!faui43.informatik.uni-erlangen.de!prknoerr From: prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) Newsgroups: comp.lang.pascal Subject: Third TPascal Keywords: puzzle,pascal Message-ID: <1991Jun27.131018.26430@informatik.uni-erlangen.de> Date: 27 Jun 91 13:10:18 GMT Organization: CSD., University of Erlangen, Germany Lines: 82 Ok, here you are, the third one: This one needs some warnings, for it will for sure not run on any compiler. For instance, you need a word data type which is not implemented on most unix pc compilers (for some good reason, agreed). But I think most of you guys either use Borland's TurboPascal or perhaps some other pascal which does have unsigned data types, so you will perhaps be able to follow this one (I know I'm advertising, but at least it's for a reasonable product I do it for) program AdjustValue; procedure AdjustAndTrim(var value : word;change : integer); var temp : integer; begin temp := value + change; if (temp<0) then temp := 0 else if (temp>1000) then temp := 1000; value := temp; end; var given : word begin given := 0; AdjustAndTrim(given,-1); end. The "AdjustAndTrim"-procedure should add or subtract a shift to a word, cutting off too low and too high values to stay inside a constant range (0 .. 1000 here). As the programmer (that is me) anticipated there will be some negative values at first, he chose to put the sum into an integer first, checking whether it was legal, doing then some trimming and as the last operation copy the valid value back into the original word variable. What goes wrong is the following: Even the first assignment fails. Though an integer has been provided to catch up with those negative fellows, a range check error will occur when called with the given parameters. Now, who of you pascalaholics can guess what went wrong? And where do the other puzzles stay I keep scanning the group for? Does Arthur Dent after all find out what the question to 42 is? Drop a word! Bye, Peter PS: If I happen to type in junk, and the intended solution won't work (perhaps because the compiler yells at some typo error), please supply me not only with the solution but also with corrections/ better solutions/enhanced puzzles. This is called cooking and counts double.I will then take the thing home to my trusty 286 and figure out my errors. Seemingly one such thing has happened to the last puzzle, but as I were sure I did exactly such a thing myself some time ago, I have still to check it out. Keeping watching for my next post next week, which will also contain the solution to the first puzzle!) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PPS: Somebody suggested I should also post puzzles of the kind: Which variable declarations would be necessary to make the following source code compilable: [figure your own example (and in case of agreement, also post it)] ? But in my opinion this sounds a bit to much like theory, where I prefer the "will compile, but produce unexpected results"-kind of puzzles, as they have an air of praxis (BTW, do you use the verb praxis here?) Any opinions are welcome. --- sig? You must be kidding with all those SIGKILLs around on this machine! Peter Knoerrich, email: prknoerr@faui43.informatik.uni-erlangen.de