943,639 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1535
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 28th, 2008
0

problems with eigen digit code

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sonygamer is offline Offline
27 posts
since Feb 2008
Feb 28th, 2008
0

Re: problems with eigen digit code

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.
Reputation Points: 10
Solved Threads: 6
Junior Poster
plgriffith is offline Offline
100 posts
since Jan 2008
Feb 28th, 2008
0

Re: problems with eigen digit code

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 6
Junior Poster
plgriffith is offline Offline
100 posts
since Jan 2008
Feb 28th, 2008
0

Re: problems with eigen digit code

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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sonygamer is offline Offline
27 posts
since Feb 2008
Feb 28th, 2008
0

Re: problems with eigen digit code

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sonygamer is offline Offline
27 posts
since Feb 2008
Feb 28th, 2008
0

Re: problems with eigen digit code

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 28th, 2008
0

Re: problems with eigen digit code

Okay I made this code on Turbo C
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Feb 28th, 2008
0

Re: problems with eigen digit code

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sonygamer is offline Offline
27 posts
since Feb 2008
Feb 28th, 2008
1

Re: problems with eigen digit code

Okay here is another shorter alternative.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Feb 28th, 2008
0

Re: problems with eigen digit code

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Haven't programmed in C++ in a while....need help
Next Thread in C++ Forum Timeline: Loop problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC