Lets say if the input is 4 then the output should be all possible 4 letter words with letters a to f. All the way from aaaa to ffff. How would I do this through the use of recursion?
My apologies for not including my attempt at the problem in my initial question. And some for you are wondering why I am using recursion instead of using a simpler method (such as a for loop for example) and the reason for this is that my prof wants us to use a for loop to solve this problem.
Here is my attempt at doing this:
void allPossiblilities(int n)
{
char*result;
if(Done(result))/*since the last possibility will be all f I am using that as my base case*/
{
printf("%s",result);
return;
}
/*This is where the recursive part should go but I am totally lost as to what it should be*/
}
bool Done(result)/*This function just returns true if all the array's index are f*/
{
int i;
bool a=true;
for(i=0;i<=n-1;i++)
if(result[i]!='f')
a=false;
}
return a;
}
I will give you a hint, to make you think:
How many possibilities are for 4 digits and 10 possible numbers (0-9) base^digits = 10^4 = 10000 possible outputs 0000-9999, in your case they will base = 6 (A-F) and exp = 4 (4 positions) 6^4 = 1296 combinations.
How are recursive functions made?
They have 2 steps:
Basic Step: it’s the criteria or the condition when the function doesn’t call itself ( the final condition).
Recursive Step: It’s the criteria or the condition when the function calls itself, and the result of it should be nearer to the Basic Step.
Example the famous factorial function, the basic step is the return of 1, and the recursive step is the second one.
PD: I am trying to make you analyze the problem and get the solution by yourself, and giving you some tools.
The code:
Part of the output:
