?
Solved

Optimizing a nested loop with recursion

Posted on 2004-03-29
4
Medium Priority
?
210 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 375 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

800 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