Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!im4u!ut-sally!seismo!nbires!hao!hplabs!sdcrdcf!ism780c!marty From: marty@ism780c.UUCP (Marty Smith) Newsgroups: net.lang.c,net.lang.pascal Subject: Re: Pascal vs C, again (was: Pascals Origins) Message-ID: <2943@ism780c.UUCP> Date: Mon, 21-Jul-86 12:05:42 EDT Article-I.D.: ism780c.2943 Posted: Mon Jul 21 12:05:42 1986 Date-Received: Wed, 23-Jul-86 09:27:10 EDT References: <2222@brl-smoke.ARPA> <7014@boring.mcvax.UUCP> <3130@utcsri.UUCP> Reply-To: marty@ism780c.UUCP (Marty Smith) Organization: Interactive Systems Corp., Santa Monica, CA Lines: 47 Xref: watmath net.lang.c:10013 net.lang.pascal:593 In article <3130@utcsri.UUCP> greg@utcsri.UUCP (Gregory Smith) writes: > >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'); > >weird,huh? Note that the condition 'x[i]=0' is evaluated twice ( once >in the negative sense ), which would be unacceptable if we were searching >an array of records and the test was expensive. > What's wrong with this one? label 1,2; var i: integer; ... i :=1 ; while i<=1000 do if x[i] = 0 then goto 1 else i := i+1; writeln('not found'); goto 2; 1: writeln('zero at location', i ) 2: ... Martin Smith