Solved

Optimizing a nested loop with recursion

Posted on 2004-03-29
4
199 Views
Last Modified: 2010-04-05
Hi,

I have the following code below that iterates all the possible combinations of a three state switch for three swithes. Using recursion I know this is possible to work with N number of switches but can't work out how to do it.
The code should loop though and give you 3^n combinations, if you can do this - using recursion or not, i don't mind - recursion was just the way I tried.

procedure TForm1.Button1Click(Sender: TObject);
Var
states,i,l,n,j,count,k : integer;
switch : array[0..2] of integer;
begin
  switch[0] := 0;
  switch[1] := 0;
  switch[2] := 0;

    For k := -1 to 1 do
    begin
      switch[2] := k;
      For l := -1 to 1 do
      begin
         switch[1] := l;
         For i := -1 to 1 do
         Begin
           switch[0] := i;
           memo1.lines.add(InttoStr(switch[0])+','+InttoStr(switch[1])+','+InttoStr(switch[2]))
        end;
      end;
  end;

end;



Thanks in advance. 125 points on offer.

Richard
0
Comment
Question by:wildey100
  • 3
4 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 125 total points
ID: 10703497
maybe something like this?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure combi_switch(switches : array of integer; level : integer; outList : TStrings);
var
  i,j : integer;
  s : string;
begin
  for i := -1 to 1 do
  begin
    switches[level] := i;
    if level = high(switches) then
    begin
      s := '';
      for j := 0 to level-1 do
        s := s + inttostr(switches[j])+',';
      s := s + inttostr(switches[level]);
      outlist.add(s);
      application.ProcessMessages; //time to show
    end
    else
      combi_switch(switches,level+1,outlist);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  switches : array of integer;
begin
  setlength(switches,strtoint(edit1.text));
  combi_switch(switches,0,memo1.Lines);
end;

end.


meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10703745
just to ask about the b-grade?
0
 

Author Comment

by:wildey100
ID: 10703915
Sorry, it was my first post and answer grading. Maybe I was being too harsh. Your answer has solved my problem perfectly so I guess I should have marked it as "excellent". Sorry if I have caused offence. I'd be more than happy to regrade it if I could.

Richard.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10704035
well,
i could do it (because i'm the page editor)
but it would not be fair against other experts.

so i say let it as it is

for future read

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/help.jsp#hi73

and btw. welcome to ex-ex :-))

glad to helped you

meikl ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question