CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2006
    Posts
    4

    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!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ newbie question

    Quote 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

  3. #3
    Join Date
    Mar 2006
    Posts
    4

    Re: C++ newbie question

    Quote 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;
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    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

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured