Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!cuae2!ihnp4!ihlpg!tainter From: tainter@ihlpg.UUCP (Tainter) Newsgroups: net.lang.c,net.lang.pascal Subject: Re: Pascal vs C, again (was: Pascals Origins) Message-ID: <2248@ihlpg.UUCP> Date: Wed, 23-Jul-86 18:12:50 EDT Article-I.D.: ihlpg.2248 Posted: Wed Jul 23 18:12:50 1986 Date-Received: Thu, 24-Jul-86 23:56:39 EDT References: <2222@brl-smoke.ARPA> <7014@boring.mcvax.UUCP> <3130@utcsri.UUCP> Organization: AT&T Bell Laboratories Lines: 40 Xref: mnetor net.lang.c:5435 net.lang.pascal:344 > Challenge: given > var x: array [1..1000] of integer; > write a code fragment to find the location of the first 0 in x. The > condition that no zeroes exist must be distinguished. > Further rules: > - Standard Jensen & Wirth Pascal ( no break from loop ) > - *** x[i] must not be evaluated for i<1 or i>1000 *** > - The search loop must be terminated as soon as a zero is found. > - 'a or b', 'a and b' always evaluate both a and b ( I think > this is a rule in Jensen & Wirth ) > This is the best I can find: > var i: integer; > ... > i :=1 ; > while i<1000 and x[i] <> 0 do > i := i+1; > if x[i] = 0 then writeln('zero at location', i ) > else writeln('not found'); > Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg This is a better solution: var i : integer; notfound : boolean; x : array [1..1000] of integer; . . . i := 1; notfound := true; while (i<=1000) and notfound do if x[i] = 0 then notfound := false else i := i+1; if notfound then writeln('not found') else writeln('zero at location', i ); --j.a.tainter