Pascal vs C, again (was: Pascals Origins)
Marty Smith
marty at ism780c.UUCP
Tue Jul 22 02:05:42 AEST 1986
In article <3130 at utcsri.UUCP> greg at 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
More information about the Comp.lang.c
mailing list