Sum of digits

Pages: 12




Trying to Create program definition for sumDigits Function. Function will receive and return an integer.

Main Method
Ask for input for numerous non-negative integers.
Using pass-by-reference, pass integer received into recursive function called sumDigits.
Receive result back from sumDigits and output result to screen.
Include: system("PAUSE"); after your output to pause the screen.
See example below.

sumDigits Function (Receives integer)
Recursively call sumDigits adding up the integers received.
Return result to main method for output.
If a single digit is received, just return it to the main method.

Ensure you include ALL files required to make your program compile and run. I would like to see your .cpp file and the .exe file that is inside your debug directory.
Upload your page to the Dropbox.
NOTE: Complete your activity and submit it to the Dropbox by clicking on the Dropbox tab at the top of the course frame and choosing the correct Weekly Activity.
If needed, click on the Help button above for more information on Course Tools/Using the Dropbox.

Total Possible Points

Example output of your program
Run 1:
Enter a nonnegative integer: 23
The sum of the digits of 23 is 5

Run 2:
Enter a nonnegative integer: 1234
The sum of the digits of 1234 is 10

Run 3:
Enter a nonnegative integer: 90513
The sum of the digits of 90513 is 18

Run 4:
Enter a nonnegative integer: 2147483647
The sum of the digits of 2147483647 is 46


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// this is what I got so far...
#include <iostream>

using namespace std;

 // prototype
int sumDigits(const int first, const int last, const int array[], const int array_size);

int main()
{
    assert(( first > 0) && ( first < array_size));
    assert((last > 0 )&& (last < array_size));
    
    if (first == last) 
       return (array[first]);
       
       return (array[first] + sum(first + 1, last, array));
       
       system("PAUSE")
}
Do you know how to get an individual digit? It only involves division and modulus. Also, if you only need to deal with non-negatives, use unsigned types instead of signed types.
Last edited on
not sure on how to get individual digits is there a reference or site I can look at to know how?

I understand the the modulus is going to give you the remainder of the numbers being devided but where does that come into play when you are adding lets say 123456 to equal 21?
Last edited on
Well, let's do an example. Firstly, remember that the integer division operator will cut off decimal places, and that the modulus operator gives us the remainder of a division (the part that was cut off). Say we have the number 1342, and you want to get digit number 2 (from the right). If we use modulus 10 on it, that'd give us 2, but that's not what we want. If we divide it by (10^1), then it gives us 134, but that's also not what we want. if we put them together, however, first diving by 10 and then performing modulus with 10, we get 4, which is the value we want.
Last edited on
not understanding your math how do you get 10^2 when this is the same as saying 10 times 10 which is 100 not 13
You're right, I messed up. Since we wanted the second digit, it should be 10 to the power of 1, and 1342 divided by 10 is 134.2, but the decimal place gets cut off and we're left with 134. I fixed the post, it see if you understand it now.
Last edited on
Ok let me see if I understand so everytime you divide by ten basically it mover the decimal over giving you the modulus or remainder of what ever the number is. Am I correct?

so for instance going back to your example.

1342 if I divide by ten it would give me a modulus of 2

1342 if I divide by 100 it would give me a modulus of 42

1342 if I divide by 1000 it would give me a modulus of 342

Last edited on
Dividing by 10 chops off the first digit (the digit on the right). Performing modulus 10 on it gives you just that first digit (right-most digit). Consider the first digit as digit #0.
Last edited on
Ok so....

so if you would do 1342%10 this would give me 2
if you do 1342%100 this would give me 42? or 4?
if you do 1342%1000 this would give me 342? or 3
You won't need to use anything other than 10 for the modulo % part of this problem. The powers of 10 only need to be used for division.

But yes, 1342%100 is 42 and 1342%1000 is 342. As I said, this isn't what you want.
ok so I guess I would have to do something in the form of

1342%10= a
1342/10 = b
b%10 =c
b/10 = b
b%10 = d


is this correct is the only way I can think of doing it.
or am I wrong?
No, there is a much simpler way of doing it.

First, you would divide by (ten to the power of the digit you want).
Then, you would take modulo ten of the resulting number.
You now have your digit.

This code demonstrates this:
1
2
3
4
5
6
7
unsigned int number;
unsigned int digit;
cin >> number; //type in 1342 or any other number
cin >> digit; //type in 0 for digit 1, 1 for digit 2, 2 for digit 3, etc
number /= 10 ^ digit;
number %= 10;
cout << number;
Actually Ernest's way is easier, more efficient and avoids overflow. xP
1
2
3
4
5
while( has_digits(n) ){
  digit = number % 10; //last digit
  number /= 10; //chop
  //here you do the operations with that digit
}
Also, ^ is not the power operator but xor

Include: system("PAUSE"); after your output to pause the screen.
Please read these articles
http://www.cplusplus.com/forum/articles/11153/
http://www.cplusplus.com/forum/articles/7312/
ok I am getting an error with this here is my code ne55

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;
int main()


{
    int digit, number, n ;
    
   cout << "Please enter an number";
   cin >> digit;
       while( has_digits(n) )
       {
         digit = number % 10; //last digit
          number /= 10; //chop
           //here you do the operations with that digit
       }
       system("Puase");
}
And the error is...
There is no has_digit function, I suppose that you will know when to end the loop. ;)
so this is what I gather

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;
int main()

int has_digits();

{
    int digit, number, n ;
    
   cout << "Please enter an number";
   cin >> digit;
       while( has_digits(n) )
       {
         digit = number % 10; //last digit
          number /= 10; //chop
           //here you do the operations with that digit
       }
       system("Puase");
}

as far as ending the loop I am thinking when it hits 0 right?
Last edited on
Nope. If you're looking for 0, and say your number is 10001, you would get 1 and then stop. You would want to keep going (over three zeroes) and get the final 1 in this case.

EDIT: Actually I misunderstood. If you divide by 10 and get zero, you know you're at the last digit.
Last edited on
closed account (D80DSL3A)
@Ernest. Even if you figure out how to do it this way (it can be done) it is an ITERATIVE method (using a while loop). Note that your assignment requires a RECURSIVE method (function calling itself). There are other requirements that the function must meet also such as: 1) pass number by reference, not value. 2) Return a 1digit # directly. Read assignment carefully.
You're on the wrong track here.

EDIT: You will still need the methods shown in previous posts for using / and % to get the individual digits.
Last edited on
so what your saying is I should have
1
2
 int has_digits(int& first)
 
or something like this?
Last edited on
This is what I had earlier................
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// this is what I got so far...
#include <iostream>

using namespace std;

 // prototype
int sumDigits(const int first, const int last, const int array[], const int array_size);

int main()
{
    assert(( first > 0) && ( first < array_size));
    assert((last > 0 )&& (last < array_size));
    
    if (first == last) 
       return (array[first]);
       
       return (array[first] + sum(first + 1, last, array));
       
       system("PAUSE")
}
Last edited on
Pages: 12