GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Jul-2004, 16:54
vienne vienne is offline
New Member
 
Join Date: Jul 2004
Posts: 1
vienne is on a distinguished road

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:
CPP / C++ / C Code:
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?
  #2  
Old 18-Jul-2004, 17:56
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Welcome to GID and Kudo's for a first post and using CODE tags! Thank you!

Quote:
Originally Posted by vienne
i'm having a problem with below recursive code.
You neglected to tell us what the problem actually is. Even though telling us what the program should do is important, it's even more important to tell us what it does do because it gives us a starting point to look for.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 21-Jul-2004, 12:54
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
I think your problem occurs because everytime you call f, your redeclareing the array temp[]. So basically you place a into temp[x], then call f again. But since f declares a variable called temp[] a is then erased from temp[x] and garbage is in its place. just declare temp in main (or whatever function f is called from) and add it as a peramiter to f so f reads void f(int x, int num, char temp[]) That should help you some.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
shortest path algorithm and file saving Pandiani C++ Forum 10 17-Jul-2006 11:46
help.... SLR * algorithm tay C Programming Language 4 10-Sep-2004 12:48
algorithm for display complex graph ctai010 C++ Forum 0 20-Mar-2004 06:24
Speed up compare algorithm captnsaj C++ Forum 4 04-Feb-2004 15:50
Color scale algorithm Ilya C++ Forum 0 28-Oct-2003 12:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.