GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Jun-2004, 08:39
Marjolein Marjolein is offline
New Member
 
Join Date: Jun 2004
Posts: 25
Marjolein is on a distinguished road

looped loops


Hi everybody,


Does anybody know some way to loop loops?
I mean, I can loop 2 times by using

for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
statement;

How can I loop say m times without using m integer variables?


Thanks for any replies!

Regards, Marjolein
  #2  
Old 21-Jun-2004, 11:35
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
I'm not sure what your asking. Are you trying to say is there a way to do a 5 nested loops with only 1 variable? If so then I think the answer is no. Are you trying to say how can you run the same loop X amount of times? In that case you'd only need the two loops like you have. Try and be a bit more specific
  #3  
Old 21-Jun-2004, 12:24
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by Marjolein
Hi everybody,


Does anybody know some way to loop loops?
I mean, I can loop 2 times by using

for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
statement;

How can I loop say m times without using m integer variables?


Thanks for any replies!

Regards, Marjolein

you don't loop 2 times, you loop n*n times in this case.
__________________
spasms!!!
  #4  
Old 21-Jun-2004, 16:42
mithunjacob's Avatar
mithunjacob mithunjacob is offline
Junior Member
 
Join Date: Jun 2004
Location: Vellore, India
Posts: 65
mithunjacob will become famous soon enough

Look at it this way....


What is a nested FOR loop? It's basically a series of nos! Check this out:
CPP / C++ / C Code:
for(int i=0;i<10;++i)
 for(int j=0;j<10;++j)
   for(int k=0;k<10;++k)
      cout<<"\n"<<i<<j<<k;
The output would be:
000
001
002
...


Well, what is this actually? Isn't it more like a counter of sorts? So, if I could make a counter of specifiable digits with constraints, if you like, you'd really be having a nested FOR loop..I've used this concept and put it in a tutorial awaiting approval in the Tutorials section...I've added the header file which defines the counter class, anyway...
CPP / C++ / C Code:
//Counter.h
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>


class dig
{
public:
int val;
int min;
int max;

dig( int mi=0, int ma=9)
{
val=mi,max=ma,min=mi;
}

int hitMax()
{
return (val==max);
}

};

class counter
{
dig *digs;

public:
int upd;
int digNo;
int beg;

int continuous;

void init(int n,int c=0)
{
digNo=n;
digs=new dig[digNo];
continuous=c;
upd=1;
beg=1;
}

counter(){}

counter(int n, int c=0) {init(n,c);}

void set()
{
upd=1;
}

void reset()
{
for(int i=0;i<digNo;++i) digs[i].val=digs[i].min;
if(continuous) upd=1;
else upd=0;
}

int val(int no)
{
if(no>=0 && no<digNo) return digs[no].val;
else return -1;
}

void digSet(int no, int ma, int mi, int va)
{
digs[no].max=ma,digs[no].min=mi,digs[no].val=va;
}

~counter()
{
delete digs;
}

int update()
{

if(upd)
{
	for(int i=digNo-1;i>=0;--i)
		{
		if(digs[i].hitMax())
			{
			if(i==0)
				{
				reset();
				break;
				}
			else digs[i].val=digs[i].min;
			}
		else
			{
			++digs[i].val;
			break;
			}
		}
if(beg==1)
	{
	for(int i=0;i<digNo;++i) digs[i].val=digs[i].min;
	beg=0;
	}
}

return upd;
}
};
A small example:
CPP / C++ / C Code:
#include <fstream.h>
#include <conio.h>
#include "counter.h"

void main()
{
counter number(2,1);                                      

/*First argument:no.of digits
->Suppose you wanted all nos. 
   less than 50, then all of   
   you have to do is set the 
   constraints with number.digSet(0,4,0,0);
                       1)0 => First digit
                       2)4 => Max val
                       3)0 => Min val
                       4)0 => Initial val*/


for(;;number.update())
{
        for(int i=0;i<2;++i) cout<<number.val(i);
        if(getch()==27) break;         //27 is ASCII for the Escape key
        clrscr();                                                  
}

}
__________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times.
  #5  
Old 22-Jun-2004, 00:56
Marjolein Marjolein is offline
New Member
 
Join Date: Jun 2004
Posts: 25
Marjolein is on a distinguished road
Quote:
Originally Posted by machinated
you don't loop 2 times, you loop n*n times in this case.


Yes, but I wonder if there is some way to loop pow(n,m) times...
  #6  
Old 22-Jun-2004, 01:07
Marjolein Marjolein is offline
New Member
 
Join Date: Jun 2004
Posts: 25
Marjolein is on a distinguished road
I am afraid that my programming level is not that high that I can understand what exactly your counter class is doing and how it is related to my question?
  #7  
Old 22-Jun-2004, 03:57
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
Quote:
Originally Posted by Marjolein
Hi everybody,


Does anybody know some way to loop loops?
I mean, I can loop 2 times by using

for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
statement;

How can I loop say m times without using m integer variables?


Thanks for any replies!

Regards, Marjolein

Well, you could do what you are trying to do just by using a conditional statement like the following:

CPP / C++ / C Code:
int counter = 0;
for (int i = 1; i <= n; i++) {
          if (counter == m) break;
          for (int j = 1; j <= n; j++) {
                    if (i == n) {
                              i = 0;
                              counter++;
                    }
                    statement;
          }
}

that way you could loop n^m times... you can easily modify this to use more variables if you want them as indices for arrays or somethin like that...

BTW what are you going to use it for?? am curious...
  #8  
Old 22-Jun-2004, 05:10
Marjolein Marjolein is offline
New Member
 
Join Date: Jun 2004
Posts: 25
Marjolein is on a distinguished road
Thanks! I think I can work with this.
I am trying to use it for going through a search tree of variable length...
  #9  
Old 22-Jun-2004, 09:16
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by sho
Well, you could do what you are trying to do just by using a conditional statement like the following:

CPP / C++ / C Code:
int counter = 0;
for (int i = 1; i <= n; i++) {
          if (counter == m) break;
          for (int j = 1; j <= n; j++) {
                    if (i == n) {
                              i = 0;
                              counter++;
                    }
                    statement;
          }
}

that way you could loop n^m times... you can easily modify this to use more variables if you want them as indices for arrays or somethin like that...

BTW what are you going to use it for?? am curious...

this doesn't run n^m times. it runs n*n*m. If you want it to run n^m, you will have to type it m times.
__________________
spasms!!!
  #10  
Old 22-Jun-2004, 13:00
mithunjacob's Avatar
mithunjacob mithunjacob is offline
Junior Member
 
Join Date: Jun 2004
Location: Vellore, India
Posts: 65
mithunjacob will become famous soon enough
Unhappy

Sorry..


Quote:
Originally Posted by Marjolein
Hi everybody,


Does anybody know some way to loop loops?
I mean, I can loop 2 times by using

for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
statement;

How can I loop say m times without using m integer variables?


Thanks for any replies!

Regards, Marjolein
I'm sorry you didn't understand my reply, but I'll elaborate..

As I said, by using nested FOR loops, you're really simulating a counter. So, suppose you wanted m loops, then just declare:

counter Cnt(m,1);

How would you use it?

CPP / C++ / C Code:
for(int i=4;i<n;++i)
 for(int j=4;j<n;++j)
  for(int k=4;k<n;++k)
    .....
Well, as you can see the common constraint is n being the maximum & 4 as minimum, so you use:
count Cnt(m,1);
for(int i=0;i<m;++i) Cnt.digSet(i,n,4,4);

where:
i = designated the loop no.
n = maximum no
4 = minimum no
4 = initial no


Now, suppose I wanted to implement this:

for(int i=3;i<4;++i)
for(int j=3;j<4;++j)//Obviously the <4 makes it useless, but you get the point?
for(int k=3;k<4;++k)
for(int l=0;l<10;++l);
cout<<"\n"<<i<<j<<k<<l;

I would do this (let's assume there are 4 loops)
CPP / C++ / C Code:
#include "counter.cpp"
void main()
{
int m=4,n=3;
counter Cnt(m);
for(int i=0;i<3;++i) Cnt.digSet(i,n,n,n);
Cnt.digSet(3,9,0,0);
Cnt.set();

while(Cnt.update())
{
cout<<"\n";
        for(i=0;i<m;++i) cout<<Cnt.val(i);
}
}

Now suppose you wanted some specific constraint for each loop:
CPP / C++ / C Code:
#include <iostream.h>
void main()
{
int matrix[2][2][2]={1,2,
                      3,4,

                      5,6,
                      7,8};

for(int i=0;i<2;++i)
{
cout<<"\nPage "<<i+1<<": ";
        for(int j=0;j<2;++j)
        {
        cout<<"\n";
                for(int k=0;k<2;++k) cout<<"\t"<<matrix[i][j][k];
        }
}
}
This, using the counter class would be implemented like this:
CPP / C++ / C Code:
#include <iostream.h>
#include "counter.h"
void main()
{
int matrix[2][2][2]={1,2,
                      3,4,

                      5,6,
                      7,8};
counter Cnt(3);

for(int i=0;i<3;++i) Cnt.digSet(i,1,0,0);
Cnt.set();
while(Cnt.update())
        {
        if(Cnt.val(2)==0 && Cnt.val(1)==0) cout<<"\nPage "<<Cnt.val(0)+1<<":";
        if(Cnt.val(2)==0)                  cout<<"\n";
        cout<<"\t"<<matrix[ Cnt.val(0) ][ Cnt.val(1) ][ Cnt.val(2) ];
        }
}
Just in case you're wondering I've set the maximum value to be 1 as it never crosses 1 (you know, (0,0,1), (0,1,0), (0,1,1), (1,0,0) ...... )

Well, I hope you understood my class now!
__________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Program] Nested loops.. C++ Rebus C++ Forum 49 16-Dec-2006 19:19
Help with IF-ELSE conditional loops hellhammer C Programming Language 15 22-Apr-2004 19:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.