I don't think using strings is that bad of an idea -- it breaks a number into easy-to-access digits.
I'm not sure OP understands the depth of his HW, though.
You are being asked to do a number of things:
First, you must implement S. This is where you would use a string, or, as
kbw suggests, no strings (use division
/
and remainder
%
to get individual digits).
1 2 3 4 5 6 7 8 9 10
|
unsigned S( unsigned n )
{
unsigned result = 0;
for each digit in n
{
if (n is odd) result += the digit;
else (n is even) result += 2*(the digit);
}
return result;
}
|
Again, I don't find any reason why you cannot just convert n to a string and iterate over that to get the individual digits (remembering, as in your OP, to subtract '0' from the character representation of each digit in the string).
Next, you need to implement D.
1 2 3 4
|
unsigned D( unsigned n )
{
return the last digit in S(n);
}
|
Again, I don't care how you get it. A simple remainder operation will do. (Hint hint).
Finally, you need to implement the algorithm to sum all the D(n) for n in [A,B].
1 2 3 4 5 6 7 8 9 10
|
int main()
{
unsigned A; cout << "A? "; cin >> A;
unsigned B; cout << "B? "; cin >> B;
unsigned sum = 0;
for every N in [A,B]: sum += D(N);
cout << N << "\n";
}
|
Hope this helps.