Originally Posted by
Elysia
I assume you are able to make a flowchart--a general one, at least--for this task? If so, do it. It should be a good start.
It doesn't have to be in "computer language," but merely how you would logically do it in your head or on a piece of paper.
Yea I did and the following code was what came from it lol.. It works but its definitely inefficient especially for something like this.
Originally Posted by
oogabooga
It doesn't look like a good candidate for recursion, but if you must you must. But can you do it non-recursively? That would be a good start.
Yea it would have been a bunch of nested for loops but I was told unacceptable haha.
Heres the code I came up
Code:
//allPoss will calcualte all possible strings for string pass
//pWalker, curLength, curChar are local recursion variables
//starting from startChar to endChar with the numLength
int allPoss(char * pass, int pWalker, int curLength, int numLength, int startChar, int endChar, int curChar)
{
if (pass[curLength] < endChar)
{
pass[curLength] = curChar;
cout << pass << endl;
return allPoss(pass,pWalker,curLength,numLength,startChar,endChar,curChar+1);
}
else if(pass[curLength] == endChar && pass[pWalker] == endChar)
{
if(curLength == 0)
{
pass[pWalker] = startChar;
return allPoss(pass,curLength,curLength+1,numLength,startChar,endChar,startChar);
}
else if(pWalker == 0)
{
if(curLength<numLength-1)
{
int i;
for(i=0;i<=curLength;i++)
{
pass[i] = startChar;
}
return allPoss(pass,curLength,curLength+1,numLength,startChar,endChar,startChar);
}
}
else
{
return allPoss(pass,pWalker-1,curLength,numLength,startChar,endChar,pass[pWalker-1]+1);
}
}
else if (pass[pWalker] < endChar)
{
pass[pWalker] +=1;
int i;
for(i=pWalker+1;i<=curLength;i++)
{
pass[i] = startChar;
}
return allPoss(pass,curLength-1,curLength,numLength,startChar,endChar,startChar);
}
else
return -1;
}