Pascal vs C, again (was: Pascals Origins)
Jack Jansen
jack at mcvax.uucp
Mon Jul 21 20:53:11 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.
>...
>This is the best I can find:
> var i: integer;
> ...
> i :=1 ;
> while i<1000 and x[i] <> 0 do
^ Type of operands conflict :-)
> 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.
>
Sigh, I shouldn't go into this, but I can't resist.
First, what you do here is take a feature that is missing from pascal,
and show a use for it. I *know* break is nice, and I *know* that it is missing
from pascal. However, I could also come up with some nice question that would
be ideally suited to use set's for, and you would have to come up with quite
an ugly solution in C.
Moreover, your statement about 'x[i]=0' being evaluated twice is not true.
Don't forget that x[i]=0 is also evaluated for every time through the loop,
so you just save one test in C....
--
Jack Jansen, jack at mcvax.UUCP
The shell is my oyster.
More information about the Comp.lang.c
mailing list