Could some one help me how to implement below version of nested_loops with recursive_loops? I was able to generated the exact output what I want using nested_loops, but it is always limited to 3 loops. I want to make the number of loops dynamic, let's assume 10 loops. Hence I want to implement it using recursive_loops. Below is the working code of nested_loops but unable to generate similar output using recursive_loops. Thanks in advance.
Driver code:
#include <iostream>
#include <vector>
using namespace std;
void nested_loops(unsigned int loop, const unsigned int &maxloop,
unsigned int no, const unsigned int &maxno,
unsigned int sum, const unsigned int &matchsum,
vector<unsigned int> &pos, vector<vector<unsigned int>> &positions);
void recursive_loops(unsigned int loop, const unsigned int &maxloop,
unsigned int no, const unsigned int &maxno,
unsigned int sum, const unsigned int &matchsum,
vector<unsigned int> &pos, vector<vector<unsigned int>> &positions);
void show_positions(const vector<vector<unsigned int>> &positions);
int main()
{
vector<vector<unsigned int>> positions;
vector<unsigned int> pos;
nested_loops(1, 3, 1, 20, 1, 15, pos, positions);
cout << "\nPositions size after nested_loops:" << positions.size() << endl;
show_positions(positions);
positions.clear();
pos.clear();
recursive_loops(1, 3, 1, 20, 1, 15, pos, positions);
cout << "\nPositions size after recursive_loops:" << positions.size() << endl;
show_positions(positions);
return 0;
}
void show_positions(const vector<vector<unsigned int>> &positions)
{
vector<unsigned int> pos;
for(int i=0; i<positions.size(); ++i)
{
cout << endl;
pos = positions[i];
for(int j=0; j<pos.size(); ++j)
{
cout << " " << pos[j];
}
}
cout << endl << endl;
}
nested_loops:
void nested_loops(unsigned int loop, const unsigned int &maxloop,
unsigned int no, const unsigned int &maxno,
unsigned int sum, const unsigned int &matchsum,
vector<unsigned int> &pos, vector<vector<unsigned int>> &positions)
{
for(int i1=no; i1<maxno-2; ++i1)
{
for(int i2=i1+1; i2<maxno-1; ++i2)
{
for(int i3=i2+1; i3<maxno; ++i3)
{
sum=i1+i2+i3;
if(sum==matchsum)
{
pos.clear();
pos.push_back(i1);
pos.push_back(i2);
pos.push_back(i3);
positions.push_back(pos);
break;
}
} } }
}
recursive_loops:
void recursive_loops(unsigned int loop, const unsigned int &maxloop,
unsigned int no, const unsigned int &maxno,
unsigned int sum, const unsigned int &matchsum,
vector<unsigned int> &pos, vector<vector<unsigned int>> &positions)
{
pos.push_back(no);
if(loop == maxloop)
{
if(sum+no == matchsum)
{
positions.push_back(pos);
}
return;
}
else
{
recursive_loops(loop+1, maxloop, no+1, maxno, sum+no+1, matchsum, pos, positions);
if(no < maxno)
{
if(sum+no < matchsum)
{
pos.pop_back();
for(int i=no; no+i<maxno && sum+no+i<=matchsum; ++i)
{
recursive_loops(loop+1, maxloop, no+i, maxno, sum+no+i, matchsum, pos, positions);
pos.pop_back();
}
}
}
}
//pos.pop_back();
}
Output: (expecting same output for both function calls) Sum of numbers matches to 15 for every line and numbers are always incremental.
Positions size after nested_loops:12
1 2 12
1 3 11
1 4 10
1 5 9
1 6 8
2 3 10
2 4 9
2 5 8
2 6 7
3 4 8
3 5 7
4 5 6
Positions size after recursive_loops:3
1 2 6
1 2 6
1 4 5
recursive_loops()has exactly that signature (in particular the types of arguments)? I ask because, by doing it as you have, you have made the problem significantly harder than it needs to be (which probably contributes to your failure to get that function working as you require).