What about recursion?
You can implement an internal function that runs a loop for each entry of the given array and, performs the algorithm, when we call it after the last array's entry (in our case, when nCurrLen == 0
). Something like:
#include <list>
using namespace std;
void f3_internal(int * pOrgIntArr, int nOrgLen,
int * pCurrIntArr, int nCurrLen, list<int>& currIndices)
{
if (nCurrLen > 0)
{
int p = pCurrIntArr[0];
for (int i = 0; i < p; i++)
{
currIndices.push_back(i);
int * pNewIntArr = pCurrIntArr + 1;
f3_internal(pOrgIntArr, nOrgLen,
pNewIntArr, nCurrLen - 1, currIndices);
currIndices.pop_back();
}
}
else
{
int nLen = currIndices.size();
int * pIntArr = new int[nLen];
int currIndex = 0;
for (list<int>::const_iterator indexIt = currIndices.begin(); indexIt != currIndices.end(); indexIt++)
{
pIntArr[currIndex] = *indexIt;
currIndex++;
}
f1(pIntArr, nLen);
delete[] pIntArr;
}
}
void f3(int * pIntArr, int nLen)
{
list<int> currIndices;
f3_internal(pIntArr, nLen, pIntArr, nLen, currIndices);
}
Or, if you don't want to use STL containers, you can try something like this:
void f3_internal(int * pOrgIntArr, int nOrgLen,
int * pCurrIntArr, int nCurrLen, int* pCurrIndices)
{
if (nCurrLen > 0)
{
int p = pCurrIntArr[0];
for (int i = 0; i < p; i++)
{
pCurrIndices[nOrgLen - nCurrLen] = i;
f3_internal(pOrgIntArr, nOrgLen,
pCurrIntArr + 1, nCurrLen - 1, pCurrIndices);
}
}
else
{
int * pIntArr = new int[nOrgLen];
for (int indexInx = 0; indexInx < nOrgLen; indexInx++)
{
pIntArr[indexInx] = pCurrIndices[indexInx];
}
f1(pIntArr, nOrgLen);
delete[] pIntArr;
}
}
void f3(int * pIntArr, int nLen)
{
int* pCurrIndices = new int[nLen];
f3_internal(pIntArr, nLen, pIntArr, nLen, pCurrIndices);
delete[] pCurrIndices;
}