![]() |
|
#1
|
|||
|
|||
looped loopsHi 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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
you don't loop 2 times, you loop n*n times in this case. __________________
spasms!!! |
|
#4
|
||||
|
||||
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:
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:
CPP / C++ / C Code:
__________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. |
|
#5
|
|||
|
|||
|
Quote:
Yes, but I wonder if there is some way to loop pow(n,m) times... |
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
Well, you could do what you are trying to do just by using a conditional statement like the following: CPP / C++ / C Code:
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
|
|||
|
|||
|
Thanks! I think I can work with this.
I am trying to use it for going through a search tree of variable length... |
|
#9
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
Sorry..Quote:
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:
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:
Now suppose you wanted some specific constraint for each loop: CPP / C++ / C Code:
CPP / C++ / C Code:
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 GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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