-
March 26th, 2006, 06:03 PM
#1
C++ newbie question
One thing I simply cannot figure out how to do is to calculate the sum of the digits of a number and to count the digits in a number.
How would one go about creating two directly recursive functions to calculate these two problems respectively? Assume that it is a positive integer.
Thanks!
-
March 26th, 2006, 06:14 PM
#2
Re: C++ newbie question
Originally Posted by Ekranoplan
One thing I simply cannot figure out how to do is to calculate the sum of the digits of a number and to count the digits in a number.
How would one go about creating two directly recursive functions to calculate these two problems respectively? Assume that it is a positive integer.
Thanks!
Why recursive? This sounds like homework, and if so, homework isn't even contemplated here unless you show us what you've done.
Regards,
Paul McKenzie
-
March 26th, 2006, 06:48 PM
#3
Re: C++ newbie question
Originally Posted by Paul McKenzie
Why recursive? This sounds like homework, and if so, homework isn't even contemplated here unless you show us what you've done.
Regards,
Paul McKenzie
It is indeed homework. I have done everything except get the functions correct. I figured modulus could be used but when I tried to do it I became stumped. I tried various other ways but with no other success.
This is the code thus far. newValue is used to generate five different numbers which are then used in the following functions. They are all then displayed in a table.
#include <iostream>
#include "newValue.h"
using namespace std;
// functions used
int sumDigits(int);
int countDigits(int);
int newValue(int);
int main()
{
int a = newValue();
int b = newValue();
int c = newValue();
int d = newValue();
int e = newValue();
cout<< "num\t\tsum\t\tcount\n";
cout<<"---\t\t---\t\t---\n";
cout<< a << "\t\t" << sumDigits(a) << "\t\t" << countDigits(a) << "\n";
cout<< b << "\t\t" << sumDigits(b) << "\t\t" << countDigits(b) << "\n";
cout<< c << "\t\t" << sumDigits(c) << "\t\t" << countDigits(c) << "\n";
cout<< d << "\t\t" << sumDigits(d) << "\t\t" << countDigits(d) << "\n";
cout<< e << "\t\t" << sumDigits(e) << "\t\t" << countDigits(e) << "\n";
}
int sumDigits(int x)
{
return x;
}
int countDigits(int x)
{
return x;
}
-
March 26th, 2006, 11:27 PM
#4
Re: C++ newbie question
You didn't show any real work. It is just a glorified int main() { } with calls to functions that contain nothing but a return statement.
When we say show your attempt, we mean a real attempt. Please read the homework FAQ:
http://www.codeguru.com/forum/showthread.php?t=366302
Separating digits can be done by converting the integer to a string, and then writing a loop converting each character of the string to a digit and adding it to a total. With this method, you also can get the total number of digits.
Then convert the loop to a recursive call.
Regards,
Paul McKenzie
-
March 27th, 2006, 02:01 AM
#5
Re: C++ newbie question
You can take some help from a very similar problem discussed quite long ago.. but it does not include recursion - you will have to do that yourself - http://www.codeguru.com/forum/showth...ht=count+digit
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|