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

Loop recursion

Hi, im having some problem while doing my assignment,
i cant make my recursive function to loop as to get each digit from the number generated randomly.

int random (int num)
{

srand (time (NULL));
num = rand();
cout << num << endl;

if (num != 0)
return num % 10;
num = num / 10;
}

Wat im trying to do here is to get each digit from the number, for example, number show is 3859, i should get the result of 9 5 8 3.
But from my coding i'll only get the last digit, that's 9.

Thanks

stunt_man33
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Hi, im having some problem while doing my assignment, i cant make my recursive function to loop as to get each digit from the number generated randomly.

int random (int num) {

srand (time (NULL)); num = rand(); cout << num << endl; if (num != 0) return num % 10; num = num / 10; }

Wat im trying to do here is to get each digit from the number, for example, number show is 3859, i should get the result of 9 5 8 3. But from my coding i'll only get the last digit, that's 9.

Thanks

One, you're going to get really lousy random numbers since you keep seeding the random number generator. Just seed it once at the top of the program. Two, there's no reason to pass this function anything. num is immediately overwritten at the top of the function before you do anything with it, so its value is lost. Three, this isn't a recursive function. Four, please use code tags:
[code]
// paste code here
[/code]

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
int random (int num)
{

srand (time (NULL));
num = rand();
cout << num << endl;

if (num != 0)
return random(num % 10);
num = num / 10;
}
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

Ok..
where exactly i should put the random generator?
Then, can u help me how to use recursive loop here?

int random (int num)
{
	srand (time (NULL));
	num = rand();
	cout << rand() << endl;
	  int n = m;
	cout << n << endl;

 	if (num != 0)
		return num % 10;
		num = num / 10;


Thanks.

stunt_man33
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Ok.. where exactly i should put the random generator? Then, can u help me how to use recursive loop here?

int random (int num)
{
	srand (time (NULL));
	num = rand();
	cout << rand() << endl;
	  int n = m;
	cout << n << endl;

 	if (num != 0)
		return num % 10;
		num = num / 10;

Thanks.

1. Not all paths in your function returns an integer. You would encounter a warning if you try to compile your code.

2. What's the use of "n" and "m" ?

3. Seed your random generator once in your main function before calling the random function.

4. If what you want is to generate random number, you don't have to use loop or recursive function at all. But it's kind of unclear what you actually wants, so it's better if you can elaborate your requirements.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You