Pascal vs C, again (was: Pascals Origins)

Tainter tainter at ihlpg.UUCP
Thu Jul 24 08:12:50 AEST 1986


> 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



More information about the Comp.lang.c mailing list