I’ve been attempting to convert a set of nested loops into a single equation and need some help.
The code to calculate my sequences is as follows:
`
int Astart = 1;
int Bstart = 1;
int Cstart = 1;
int Dstart = 1;
int Ai = 6;
int Bi = 2;
int Ci = 3;
int Di = 4;
int A,B,C,D = 0;
int i=0;
for(int a=Astart;a<Astart+Ai; a++)
{
for(int b=Bstart;b<Bstart+Bi; b++)
{
for(int c=Cstart;c<Cstart+Ci;c++)
{
for(int d=DStart;d<Dstart+Di;d++)
{
std::cout << "[" << i << "]" << a << " " << b << " " << c << " " << d << std::endl;
i++;
}
}
}
}
`
All I need to do is have a single for loop running the number of permutations to replicate the behavior of the nested loop. i.e.
for(int i=0; i<(Ai*Bi*Ci*Di); i++)
{
<Insert equations here!>
}
What I’m really after is a function that takes an integer in the sequence and calculates the values of A,B,C and D based on the variables Ai,Bi,Ci,Di and Astart,Bstart,Cstart,Dstart.
Also, this system relies on the order of the nested loops, is there a method of not relying on the order to get the values A,B,C,D?
Any help would be great,
Thanks,
Chris.
Use the divide (
/) and remainder (%) operators.…