Recently I answered one of the many repeated questions of 'How to get all possible combinations of an array of insert data type here. My answer was:
#include <stdio.h>
int main()
{
/* String for combos to be enumerated */
char set[4] = { 'a', 'b', 'c', 'd' };
/* Length of the array */
int setLength = 4, a, b, c;
/* This will print all combos that have 1 value. E.g. A, B, C, D */
for (a = 0; a < setLength; a++)
{
printf("%c : ", set[a]);
}
/* This will give the 1st value of the combo */
for (a = 0; a < setLength; a++)
{
/* This will give the 2nd value. Resulting in combos with a length of 2 */
for (b = 0; b < setLength; b++)
{
printf("%c%c : ", set[a], set[b]);
}
}
/* 1st value */
for (a = 0; a < setLength; a++)
{
/* 2nd value */
for (b = 0; b < setLength; b++)
{
/* 3rd value */
for (c = 0; c < setLength; c++)
{
printf("%c%c%c : ", set[a], set[b], set[c]);
}
}
}
/* To continue with longer combos simply add more and more for loops */
printf("\n");
return 0;
}
However when I looked at other answers to this questions they all involved in-built functions from the language e.g. C#. Therefor my question is: is the way I answered correct, or reliable and if not what would be a more efficient way of doing this without in-built functions.