954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

recursive algorithm

i'm having a problem with below recursive code.
there are 4 chars stored in a array, 'a', 'b', 'c', 'd'. input is between 1 and 16, and output should look like this:

when input is 1:
a
b
c
d

when iput is 2:
aa
ab
ac
ad
ba
bb
bc
bd
ca
cb
cc
cd
da
db
dc
dd

and so on...

here is the code i'v been working with:

void f(int x, int num)
{
	int i;
        char a[4] = {'a', 'b', 'c', 'd'};
	char temp[16];

	if(num == 0)
		return;

	else
		for(i = 0; i < 4; i++)
		{
			temp[x] = a[i];

			f(x + 1, num - 1);

			temp[x + 1] = '\0';

			printf("%s\n", temp);
		}
}


where should i change to make it work?

vienne
Newbie Poster
6 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You