I'd declare a function accepting an int and returning void.
The stopping value would be when the int passed in is less than 10, since that would be
Eigen digit
If the int passed in wasn't the Eigen digit, then I'd declare an int called temp to pass to the next call of the funciton and I'd determine the individual digits of the int passed in by sequentially passing the int passed in through a loop, decreasing the value of the int passed in by dividing by 10 each time through the loop and continuing as long as the modified value of the int passed in was greater than zero. Within the loop I'd get the right hand digit of the current value of the int passed in by using modulo operator (% 10) and adding it to temp, before dividing the current value by 10. When the loop finished I'd recursively call the function, passing it temp.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
hammerhead's use of the function definition before main() is valid, though using a function prototype before main() and a function definition after main() is valid as well.
However, don't adopt his use of void as the return type for main().
In addition, this line is suspect in my opinion.
return eig(eig(num/10)+(num%10));
though I don't have compiler to run it at this time.
Here's pseudocode for my initial post.
calcEigD(n)
if n < 10
output n as Eigen digit
else
temp
while n > 0
temp += n % 10
n /= 10
calcEigD(temp)
This needs fleshing out. Copy and pasting won't work. Be sure you understand what each line is doing so you can flesh it out appropriately and defend it if asked.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
I follow your mathematical analysis. What I'm not so sure of is your C++ code. My concerns, perhaps unfounded, are 1) the use of two, nested calls to the same function from within the function and 2) passing a function as an arguement to a function (I've seen function pointers and function objects (functors) passed as arguements, but i don't remember seeing functions themselves passed as arguments).
If you've compiled and run the program and it works with a variety of input, then I've learned something today, which is never a bad thing!
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
int eig( int num )
{
if( num<10 ) return num;
else return eig( eig( num/10 ) + (num%10) ) ;
}
cool!
though you spoiled it by void main() and #include<iostream.h>
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Functions accepting functions as parameters. Worked on my compiler with several different inputs. My hat is off to ya.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Thank you for that clarification. Gee, I've learned something new today, too!
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396