Generalized Loops

  • Thread starter Abstract Dissonance
  • Start date
A

Abstract Dissonance

Its been a while since I've programmed in C++ but I need to write a small
program.

The idea is that I need to do several nested for loops.

Since I don't think I can easily handle doing the loops directly, except
maybe through recursion but at a cost, I was thinking I could use templates
to generate the code. The problem is that I have completely forgot just
about everything I learned about templates.

What I want to do is have a template that will basicaly generate a nested
set of N for loops and supply me with an array like structure to access the
ith for loops current value.

Basicaly I need to do the code

for(i1 = 0; i1<= m; i1++)
for(i2 = 0; i2<= m; i2++)
.............
for(in = 0; in<= m; in++)
{
x = F[k]; // return's ik's current value...
// i.e., if k = 4, then x = i4
// code here
}

Technically I can't have ik = 1 but i think I could handle figuring that
out.

I work on trying to figure it out but right no my brain is fried and I'd
like to know if this is even possible before I try.

Thanks,
Jon
 
A

Alf P. Steinbach

* Abstract Dissonance:
Its been a while since I've programmed in C++ but I need to write a small
program.

The idea is that I need to do several nested for loops.

Since I don't think I can easily handle doing the loops directly, except
maybe through recursion but at a cost, I was thinking I could use templates
to generate the code. The problem is that I have completely forgot just
about everything I learned about templates.

What I want to do is have a template that will basicaly generate a nested
set of N for loops and supply me with an array like structure to access the
ith for loops current value.

Basicaly I need to do the code

for(i1 = 0; i1<= m; i1++)
for(i2 = 0; i2<= m; i2++)
.............
for(in = 0; in<= m; in++)
{
x = F[k]; // return's ik's current value...
// i.e., if k = 4, then x = i4
// code here
}

Technically I can't have ik = 1 but i think I could handle figuring that
out.

I work on trying to figure it out but right no my brain is fried and I'd
like to know if this is even possible before I try.

You don't need a template.

Use a vector of n numbers; define a function to increment the vector in
the same way you'd increment a base m+1 number.

But first, think about whether you really want your loop body executed
(m+1)^(n+1) times: plug in a few typical values for m and n in your
calculator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top