| | |
problems with eigen digit code
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 27
Reputation:
Solved Threads: 0
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:
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.
c++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int calcDigit1(int,int); int main(){ int n; cout<<"input a number greater than zero:"<<endl; cin>>n; cout<<"The eigen digit of this numberis:"<<calcDigit1(n,1)<<endl; } int calcDigit1(int n,int a){ if(n<=0) return a; return calcDigit1(n%10,a+n); }
Last edited by sonygamer; Feb 28th, 2008 at 12:34 am.
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
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.
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.
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
C++ Syntax (Toggle Plain Text)
int main() { int n; cout << "input number" << endl; cin >> n; Eigen(n); return 0; } void Eigen(int n) { int array[]; //dynamically allocate array here, i don't feel like writing the code for that int var; //holds array size int length, temp; temp = n; length = 0; if(n < 10) { cout << "Eigen value is " << n << endl; exit(1); } else { //find the length of n while(temp > 1) { temp = temp/10; length += 1; } for(int i=1; i <= length; i++) { array[i-1] = n % (10 ^ i); } for(i = 0; i < var; i++) { n+=array[i]; } } free(array); Eigen(n); }
Last edited by plgriffith; Feb 28th, 2008 at 1:25 am.
•
•
Join Date: Feb 2008
Posts: 27
Reputation:
Solved Threads: 0
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:
I forgot to say this at first, but someone managed to finish this in many,many less lines than the ones I have.
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)
#include <iostream> using namespace std; int calcDigit(int,int); void main(){ int n; cout<<"input a number greater than zero:"<<endl; cin>>n; cout<<"The eigen digit of this number is:"; cout<<calcDigit(n,0)<<endl; } int calcDigit(int n,int a){ if(n<10) return a; return calcDigit(n%10,a+n); }
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.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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.
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.
Okay I made this code on Turbo C
Its not totally recursive though. I will post if can make a totally recursive one.
C++ Syntax (Toggle Plain Text)
#include<iostream.h> int eig(int num) { if(num<10) { return num; } else { return eig(num/10)+(num%10); } } void main() { int n,ans=0; cin>>n; ans=eig(n); while(1) { if(ans<10) { break; } else { ans=eig(ans); } } cout<<"Eigen number is "<<ans<<endl; }
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.
All generalizations are wrong. Even this one.
Okay here is another shorter alternative.
C++ Syntax (Toggle Plain Text)
#include<iostream.h> int eig(int num) { if(num<10) { return num; } else { return eig(eig(num/10)+(num%10)); } } void main() { int n,ans=0; cin>>n; ans=eig(n); cout<<"Eigen number is "<<ans<<endl; }
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.
All generalizations are wrong. Even this one.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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.
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.
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)
calcEigD(n) if n < 10 output n as Eigen digit else temp while n > 0 temp += n % 10 n /= 10 calcEigD(temp)
![]() |
Other Threads in the C++ Forum
- Previous Thread: Haven't programmed in C++ in a while....need help
- Next Thread: Loop problem
Views: 1251 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






