I'm working on a simple recursion function that takes a number and then adds the digits together, say 12345 = 1+2+3.....=15 I have that part:

int DigitSum(int number, int sum){
    //if the remainder of the number / 10 is 0, return the sum
    if(number % 10 <= 0){
         return sum;
         }
    //else, sum adds to itself the decimal remainder, then shaves off the decimal by number/10 (int loses decimal)
    //when calling itself.
    else {
         sum = sum + number % 10;
         return DigitSum(number / 10, sum);
         }
   
}

but now I need to write a second function that used the first one to add THOSE digits together, so say I input 12345 again, it would use the first function to get 15, but then add those digits to get 6.

I know I am way over thinking this, but could I get a hint? I tried this:

int DigitSum2(int number, int sum){
    int sum2;//if the remainder of the number / 10 is 0, return the sum
    if(number % 10 <= 0){
         return DigitSum(sum, sum2);
         }
    //else, sum adds to itself the decimal remainder, then shaves off the decimal by number/10 (int loses decimal)
    //when calling itself.
    else {
         sum = sum + number % 10;
         return DigitSum(number / 10, sum);
         }
   
}

but of course did not work.

Recommended Answers

All 8 Replies

I know I am way over thinking this, but could I get a hint?

Why do you need a second function when the first does what you need (summing digits)? The program's driver would simply call DigitSum in a loop until the stopping case is met, unless you have a specific requirement that require DigitSum2...

I do, this first part of the assignment was to write the DigitSum,
the next part is this:
Write a value-returning recursive function that uses the DigitSum function of part 1 to compute the single digit to which the int arguments digits ultimately sum.
Example 999 in digit sum = 27, but the recursive digit sum would then be 2+7 = 9;

>>Write a value-returning recursive function that uses the DigitSum function of part 1 to compute the single digit to which the int arguments digits ultimately sum.
This is a weird wording. It sounds like you need another function, which I'll refer to as "findMinDigitSum()", that is recursive, but each recursion is required to call DigitSum().
Is that how you understand the problem as well? I believe so.

Do you understand what recursion is? Based on your posted code I don't think you do.

Recursion is when a function calls itself until a result is reached. Once the result is reached, it doesn't call itself anymore, and the "nested" functions start returning:

#include <iostream>

void recursiveCount(int);

int main() {
  int value = 10;

  recursiveCount(value);

  return 0;
}

void recursiveCount(int value) {

  if (value > 0) {
    recursiveCount(value-1);
    std::cout << " " << value;
  } else {
    std::cout << value;
  }
}

This example is a little contrived, but it should show you what I'm talking about.

Yes, I understand what recursion is. A function that calls itself until a base statement is met. As far as my first function goes thats a fine example of what recursion is, it calls itself until a base statement of n%10 <=0 is met. I know that in a working environment you would most likely use a normal loop for this and not recursion, but its the exercise in the book. I'm just having trouble understanding how I use the first function in a second function that is also recursive. The wording is copied straight from the book.

My apologies, yes you do, as evidenced by DigitSum(). Your second function (DigitSum2()) isn't recursive though, so I got a little confused.

DigitSum() determines the digit sum of a single set of digits. You would recursively call findMinDigitSum() until DigitSum() comes up with something that is less-than 10 (a single digit).

Would this be a suitable answer? I had to do it on paper a few times to get it, not sure its the most elegant way:

int DigitSum2(int number, int sum){
    
    int sum2 = 0;
    if(DigitSum(number, sum) < 10){
    sum2 = DigitSum(number, sum);
    return sum2;
    }
    else {
         number = DigitSum(number, sum);
         return DigitSum2(number, sum);
         }
}

Thanks for all your help by the way. I think my tone was off in my first reply, I in no way meant to sound angry, I'm sorry if I did. I appreciate any suggestions or criticisms. I use to be a graphic artist before going back to school, so you have to learn to adjust to those things, lol.

I'd suggest something more along the lines of

int DSum2 (int v) {
    if (v < 10)
        return v;
    return DSum2 (DSum (v, 0));
}

It's important, though, that you understand why.
Work through the solution (yours and this one) and record each step. The beginning of this one would look like:

DSum2(12345)
  12345 < 10 [NOPE, continue]
  DSum2(DSum(123245,0))
    DSum(12345,0)
      12345 < 10 [NOPE, continue]
      DSum(1234,5)
      1234 < 10 [NOPE, continue]
      ...
      return 15
  DSum2(15)   <- Result of the DSum(12345,0) recursion
  15 < 10 [NOPE, continue]
    DSum2(DSum(15,0))
    ...

Each time you call DSum you do the entire recursion step. You want to minimize that. Work through yours by hand and see if you notice the difference.

I haven't tested it. Does it produce the correct result? That's the ultimate determination of whether it's correct or not.

I would suggest you reverse Lines 4 and 5. This would allow you to condense Lines 3 and 4 into one line. You could then replace the second function call with your sum2 variable. That will save you a function call and potentially make your code faster/more efficient:

int DigitSum2(int number, int sum){
    
    //int sum2 = 0;
    //if(DigitSum(number, sum) < 10){
    //sum2 = DigitSum(number, sum);

    int sum2 = DigitSum(number, sum);
    if (sum2 < 10) {
     return sum2;
    }
    else {
         number = DigitSum(number, sum);
         return DigitSum2(number, sum);
         }
}

I would have to look more closely, but I don't think it changes the dynamic/intent of your code negatively.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.