recursive algorithm

Reply

Join Date: Jul 2004
Posts: 6
Reputation: vienne is an unknown quantity at this point 
Solved Threads: 0
vienne vienne is offline Offline
Newbie Poster

recursive algorithm

 
0
  #1
Jul 18th, 2004
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:
  1. void f(int x, int num)
  2. {
  3. int i;
  4. char a[4] = {'a', 'b', 'c', 'd'};
  5. char temp[16];
  6.  
  7. if(num == 0)
  8. return;
  9.  
  10. else
  11. for(i = 0; i < 4; i++)
  12. {
  13. temp[x] = a[i];
  14.  
  15. f(x + 1, num - 1);
  16.  
  17. temp[x + 1] = '\0';
  18.  
  19. printf("%s\n", temp);
  20. }
  21. }

where should i change to make it work?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC