Sum of digits

Pages: 12
because when I initially started this forum I did a recursion see above I posted this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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")
}

which is recursive.......
Last edited on
I don't really see the recursivity, does the function call itself? Also, where is the prototype defined?
http://www.cplusplus.com/doc/tutorial/functions2/ has a bit of info on recursivity.

Shame he asked for recursivity, this is extremely easy to do with a decending for loop, given the number of digits counted, which can be done with (int)floor(log10(number))+1... suppose the assignemetn is to test you in recursivity.
Last edited on
Topic archived. No new replies allowed.
Pages: 12