problems with eigen digit code

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 27
Reputation: sonygamer is an unknown quantity at this point 
Solved Threads: 0
sonygamer sonygamer is offline Offline
Light Poster

problems with eigen digit code

 
0
  #1
Feb 28th, 2008
Hello again everyone. I've been trying to finish this code that calculates the eigen digit (i.e. 2349 is 2+3+4+9=18=1+8=9. It wont give me a single answer though. And we are supposed to do it through a recursive function. The code looks like this:
  1. #include <iostream>
  2. using namespace std;
  3. int calcDigit1(int,int);
  4. int main(){
  5. int n;
  6. cout<<"input a number greater than zero:"<<endl;
  7. cin>>n;
  8. cout<<"The eigen digit of this numberis:"<<calcDigit1(n,1)<<endl;
  9. }
  10. int calcDigit1(int n,int a){
  11. if(n<=0)
  12. return a;
  13. return calcDigit1(n%10,a+n);
  14. }
I dont understand what is wrong with it, so your help is greatly appreciated. And please keep in mind that i am a complete beginner at this.
Last edited by sonygamer; Feb 28th, 2008 at 12:34 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: problems with eigen digit code

 
0
  #2
Feb 28th, 2008
This is the way I would approach it:
Call a function Eigen from main.
Have an array of ints in eigen. Store the first number in array[0], second in array[1] and so on.

For example, with the number 2349, store 2 in array[0], 3 in array[1], 4 in array[2], 9 in array[3]. Then, write a loop to add each element of the array, and store that in a variable eigen_Val. After you do this, call Eigen(eigen_val).

I will work on some code in a few minutes to put up here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: problems with eigen digit code

 
0
  #3
Feb 28th, 2008
  1. int main()
  2. {
  3. int n;
  4. cout << "input number" << endl;
  5. cin >> n;
  6. Eigen(n);
  7.  
  8. return 0;
  9. }
  10.  
  11. void Eigen(int n)
  12. {
  13. int array[];
  14. //dynamically allocate array here, i don't feel like writing the code for that
  15. int var; //holds array size
  16. int length, temp;
  17. temp = n;
  18. length = 0;
  19. if(n < 10)
  20. {
  21. cout << "Eigen value is " << n << endl;
  22. exit(1);
  23. }
  24. else
  25. {
  26. //find the length of n
  27. while(temp > 1)
  28. {
  29. temp = temp/10;
  30. length += 1;
  31. }
  32.  
  33. for(int i=1; i <= length; i++)
  34. {
  35. array[i-1] = n % (10 ^ i);
  36. }
  37.  
  38. for(i = 0; i < var; i++)
  39. {
  40. n+=array[i];
  41. }
  42. }
  43. free(array);
  44. Eigen(n);
  45. }
Last edited by plgriffith; Feb 28th, 2008 at 1:25 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 27
Reputation: sonygamer is an unknown quantity at this point 
Solved Threads: 0
sonygamer sonygamer is offline Offline
Light Poster

Re: problems with eigen digit code

 
0
  #4
Feb 28th, 2008
ok it gives me an answer now, however, the answer is always whatever was inputed to n.
Basically, i put it n as 2349, it gives back 2349, if n=23, then the answer comes as 23.
It looks like this now:
  1. #include <iostream>
  2. using namespace std;
  3. int calcDigit(int,int);
  4. void main(){
  5. int n;
  6. cout<<"input a number greater than zero:"<<endl;
  7. cin>>n;
  8. cout<<"The eigen digit of this number is:";
  9. cout<<calcDigit(n,0)<<endl;
  10. }
  11. int calcDigit(int n,int a){
  12. if(n<10)
  13. return a;
  14. return calcDigit(n%10,a+n);
  15. }

I forgot to say this at first, but someone managed to finish this in many,many less lines than the ones I have.
Last edited by sonygamer; Feb 28th, 2008 at 1:32 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 27
Reputation: sonygamer is an unknown quantity at this point 
Solved Threads: 0
sonygamer sonygamer is offline Offline
Light Poster

Re: problems with eigen digit code

 
0
  #5
Feb 28th, 2008
I still cannot figure out what is wrong with my code, could i get some help please? I really need to finish this today by 10 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: problems with eigen digit code

 
0
  #6
Feb 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: problems with eigen digit code

 
0
  #7
Feb 28th, 2008
Okay I made this code on Turbo C
  1. #include<iostream.h>
  2.  
  3. int eig(int num)
  4. {
  5.  
  6. if(num<10)
  7. {
  8. return num;
  9. }
  10. else
  11. {
  12. return eig(num/10)+(num%10);
  13. }
  14.  
  15. }
  16.  
  17. void main()
  18. {
  19. int n,ans=0;
  20. cin>>n;
  21. ans=eig(n);
  22. while(1)
  23. {
  24. if(ans<10)
  25. {
  26. break;
  27. }
  28. else
  29. {
  30. ans=eig(ans);
  31. }
  32. }
  33. cout<<"Eigen number is "<<ans<<endl;
  34.  
  35. }

Its not totally recursive though. I will post if can make a totally recursive one.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 27
Reputation: sonygamer is an unknown quantity at this point 
Solved Threads: 0
sonygamer sonygamer is offline Offline
Light Poster

Re: problems with eigen digit code

 
0
  #8
Feb 28th, 2008
Thank you so much for your help. I didn't think it was allowed to put the calculations for the eigen number as a prototype. I was taught that all calculations had to go below MAIN.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: problems with eigen digit code

 
1
  #9
Feb 28th, 2008
Okay here is another shorter alternative.
  1. #include<iostream.h>
  2.  
  3. int eig(int num)
  4. {
  5.  
  6. if(num<10)
  7. {
  8. return num;
  9. }
  10. else
  11. {
  12. return eig(eig(num/10)+(num%10));
  13. }
  14.  
  15. }
  16.  
  17. void main()
  18. {
  19. int n,ans=0;
  20. cin>>n;
  21. ans=eig(n);
  22. cout<<"Eigen number is "<<ans<<endl;
  23.  
  24. }
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: problems with eigen digit code

 
0
  #10
Feb 28th, 2008
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.
  1. calcEigD(n)
  2. if n < 10
  3. output n as Eigen digit
  4. else
  5. temp
  6. while n > 0
  7. temp += n % 10
  8. n /= 10
  9. 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1251 | Replies: 16
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC