Solved

Optimizing a nested loop with recursion

Posted on 2004-03-29
4
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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