Pascal vs C, again (was: Pascals Origins)

Naoto Kimura cc at pegasus.cs.ucla.edu
Sat Jul 26 16:54:46 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.
>

to avoid the evaluation of  ``x[i]=0'' you can do the following:

	var
	   i: integer;
	   f: boolean;
	...
	i := 0;
	repeat
	   i := i + 1;
	   f := x[i]=0;
	until f or i=1000
	if f then
	   writeln('zero at location', i )
	else
	   writeln('not found');

   The only thing that really looks weird in the above is the ``i := 0'',
which can cause trouble if ``i'' was to be defined as the subrange
1..1000 .


*********************************************************************
*** you've probably already heard the following at least a millon ***
*** times                                                         ***
*********************************************************************

    A difficult thing to write in Pascal is an interactive program.
Checking eof and eoln before reading anything can cause some problems,
since they are undefined.  Performing a read or readln before checking
the status of eoln or eof can be a problem if you use an empty file.
Either the read would fail since there isn't anything to read, or the
read or readln will set the eoln and eof, but will return some bogus
value in the variable.  These problems get to be pretty bad, since
different compiler handle eoln and eof differently.

Let's say we have the following:

	var
	   i : integer;
	...
	while not eof do
	   begin
	      writeln ('enter an integer : ');
	      readln (i);
	   end;

The prompt never appears until something is entered, since these
conditions cannot be checked before an attempt to read is made.
To solve this problem we might do the following:

	var
	   i : integer;
	...
	writeln ('enter an integer : ');
	while not eof do
	   begin
	      readln (i);
	      writeln ('enter an integer : ');
	   end;

   This solves the problem of getting no prompt before entering data,
but creates the problem of one extra prompt at the end.

   But then, you can't really complain about these problems because
Pascal wasn't designed to be used for interactive programming anyway.



More information about the Comp.lang.c mailing list