In article <20**************************@posting.google.com >,
Hardrock <sh***********@yahoo.com> wrote:
I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example
void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}
}
Here n is an argument of funtion f(). The problem is that since n is a
variable, so I don't know how many for() loops I need to write in my
source code.
Any input to help me solve this problem will be deeply appreciated.
What are you really trying to do?
This looks just enough like homework that I'm going to just give you
hints instead of posting code.
Start by working out how you would loop through all possible values of
an N-digit decimal number.
(Hint: Add in the least significant digit, then normalize all the
digit values. Be sure to keep track of your termination condition.)
Once you've worked that out, it should be a simple exercise to dynamically
vary the digit values based on what you get in k[] instead of fixing
them all at 10.
dave
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
However, not liking dogs, eating one has a certain appeal for me.
I think that it would be better than using live terriers for skeet-shooting.
--David Evans in uw.general