when 1 of the integers (primarily the one to be 'added') is negative?
The following code works for positive intergers but not for negative integers.
(Not only does it not work, I can't really think of how it should be defined. )
1 2 3 4 5 6 7 8 9 10 11 12 13
int cot(int a, int b)
{
int newnum = 0;
int ap = num_of_digits(a), bp = num_of_digits(b);
int j = 1;
newnum = b; int fp = a; int multiplier = 1;
for (int i = 0; i < bp; ++i)
{
multiplier = 10 * multiplier;
}
newnum = (multiplier * a) + b;
return newnum;
}
That's a new one on me and very impressively it scores zero hits on google at the time of typing. Do you mean concatenation? If that's what you mean, turn them both to std::Strings and then just add the two strings together.
Concatenation is not a mathematical operation, so there's really no way that it "should" work with a negative integer.
I think you meant
(10 ^ 3) * 1 + (10 ^ 2) * 3, etc.
And I would disagree about concatenation. It is something you do on strings, lists, etc. not integers, unless you are doing what Moschops said (with conversion to strings). That said, you are trying to do basically string concatenation on two integers, without converting them from integer types, then, by taking each digit and its place in the number?
One way I see would be to concatenate them as though they were both positive, then negate the entire result. But as I said, concatenation is really a string operation and so this sort of idea doesn't really apply.