Solved

Optimizing a nested loop with recursion

Posted on 2004-03-29
4
189 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now