Re: a problem in my program in pascal



<gilevgi@xxxxxxxxx> wrote in message
news:1160569433.506935.220260@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I want to write the pattern " for i:=1 to a[i] do" n times (n is
variable), that is,

for b[1]:=1 to a[1] do
for b[2]:=1 to a[2] do
for b[3]:=1 to a[3] do
for b[4]:=1 to a[4] do
.........
..........
.........
for b[n]:=1 to a[n] do
something here.

in this writing n is a variable. therefore I can not write what i want.
can you say how can i write the above structure using "repeat-until" or
"while" patterns?

Last time I tried this, for solving a nine-tile puzzle[0], I gave up and
wrote out the loops. But it can be done; I've attended lectures where
it was. They were about backtracking and clearly I'd forgotten too much
about them.

First, let me outline a solution that does _not_ involve backtracking.
Assuming you have constant dimensions (a[2] does not vary for different
values of b[1]), multiply them out and have a single loop from 1 to
a[1]*a[2]*a[3]*...*a[n]. Using div and mod appropriately, all the b[]s
can be computed from the loop index.

That's neither very pretty nor very efficient, however. Div and mod may
be pretty when used correctly (and here, it feels like they're the right
solution to the wrong problem), but they're never particularly efficient.

My best guess for backtracking seems a little awkward, too, but as I've
explained that's probably because I'm no longer very good at it. Note also
that normally, backtracking is associated with _truncating_ a solution
tree whenever you find you can no longer produce a viable solution on
the current path. This element is not in your question; you might have
conditional Break statements in every loop to represent it. (For example,
there might be a requirement that all the b[]s added together don't
exceed a certain limit. A partial sum b[1]+b[2]+b[3] might disqualify
an entire branch without the need to loop past b[3].)

type TNumbers = array of Integer;

procedure Loop(const Dimension: Integer; const Indices, Counts: TNumbers);
begin
Assert(Length(Indices)=Length(Counts));

if (Dimension<Length(Indices))
then begin
for Indices[Dimension]:=1 to Counts[Dimension]
do Loop(Succ(Dimension), Indices, Counts);
end;
end
else Work(Indices);
end;

Call as follows:

SetLength(Counts, N);
{ Fill Counts }
SetLength(Indices, N);
Loop(0, Indices, Counts);

I tested it and it works. That is not to say that the above code should
compile - it should not. I've left an easily corrected mistake in it on
purpose, also because it's clearer this way. _And_ you have to implement
Work(). Note that it might be a parameter to Loop().

Groetjes,
Maarten Wiltink

[0] Pieces can end up in any of the positions, in any of four
orientations. It was a surprisingly difficult puzzle.


.



Relevant Pages

  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: what does "serialization" mean?
    ... >> resource reasons). ... >> other forms of writing saying what you really mean to say is a writing ... If you consider all loop forms of all languages, ... exposed "iteration" API. ...
    (comp.programming)
  • Re: How to write array of doubles to stream without using a loop?
    ... > without experimentation - that may not be the case here, ... as opposed to supplying many little hunks in a loop. ... > Writing in a single hunk ...
    (microsoft.public.dotnet.languages.csharp)