Re: algorithms + data structures = programs
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Sun, 23 Jul 2006 20:33:49 +0000
arnuld said:
Richard Heathfield wrote:
arnuld said:
<snip>
r := 10*r + d[i]
d[i] := r div 2
r := r-2*d[i]
more *shocking* is its implmentation in PASCAL.
Okay, let's take a look:
program power(output);
{ decimal representation of negative powers of 2 }
const n = 10;
type digit = 0..9;
var i,k,r: integer;
d: array[1..n] of digit;
begin
for k := 1 to n do
begin
write('.');
r := 0;
for i := 1 to k - 1 do
begin
r := 10 * r + d[i];
d[i] := r div 2;
r := r - 2 * d[i];
write(chr(d[i] + ord('0')))
end ;
d[k] := 5;
writeln('5');
end
end .
Yes, okay, I agree. I'm appalled. :-)
hey Rich, i need help.
Um, okay, let's see what we can do with this mess.
We start off with the usual Pascal furniture, and then get on to the meat of
the program:
for k := 1 to n do
So clearly Wirth is only interested in the first ten results.
begin
write('.');
He eschews a leading 0, and gets right into the decimal point.
r := 0;
for i := 1 to k - 1 do
Wirth ex machina - he presumes that each value will take one more decimal
place to represent than the previous value.
First time in, k is 1, so the inner loop doesn't execute. d[1] gets the
value 5, and '.5' is written.
Second time in, k is 2, so i ranges from 1 to 1 (i.e. one iteration
happens).
begin
r := 10 * r + d[i];
i is 1, and d[1] is 5, and r is 0, so r becomes 10 * 0 + 5, which is 5.
d[i] := r div 2;
d[1] becomes half of whatever it is at the moment, so it goes from 5 to 2
(because div is an integer division, so fractions are ignored).
r := r - 2 * d[i];
r becomes its current value less twice d[1]. It was 5, but now it takes the
new value 5 - (2 * 2), which is 1. In other words, it becomes the
*remainder* of the division of d[i] by 2. It would have been better named
'remainder' rather than r. And d[i] is the digit that will be displayed at
position i.
write(chr(d[i] + ord('0')))
This is the equivalent of C's putchar(d[i] + '0') - in other words, it
writes the character equivalent of a particular digit.
end ;
d[k] := 5;
Wirth ex machina again - he presumes the last digit of the representation
will be 5. (He's right, of course, but it would have been nice for the
program to work this out for itself.)
writeln('5');
end
The program does make sense, kind of. It's just written with a whole bunch
of unstated assumptions and is very poorly explained. His explanation
precedes the program:
"Assume that a fraction f is represented by the array d such that
f = sum(i ranges from 1 to k - 1 inclusive) of d[i] * pow(10, -i)
i.e. by its decimal form with k - 1 digits. Now f is to be divided by 2.
This is done by repeating the familiar division operation for all k - 1
digits d[i], starting with i = 1. It consists of dividing the digit by 2,
taking into account a possible carry from the previous position, and of
retaining a remainder r for the previous step."
This could have been expressed somewhat more clearly! And indeed it could
have been avoided completely. I would not have chosen this particular
problem as an illustration of array handling.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- Follow-Ups:
- Re: algorithms + data structures = programs
- From: arnuld
- Re: algorithms + data structures = programs
- References:
- algorithms + data structures = programs
- From: arnuld
- Re: algorithms + data structures = programs
- From: Richard Heathfield
- Re: algorithms + data structures = programs
- From: arnuld
- Re: algorithms + data structures = programs
- From: Richard Heathfield
- Re: algorithms + data structures = programs
- From: arnuld
- algorithms + data structures = programs
- Prev by Date: 5000 RapidShare Ebooks
- Next by Date: Everything is a...
- Previous by thread: Re: algorithms + data structures = programs
- Next by thread: Re: algorithms + data structures = programs
- Index(es):
Relevant Pages
|