How would you define integer cotantiation?

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;
}


P.S. How do you spell cotantiation?
Last edited on
cotantiation


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.
If you want to append a digit on the end of the number, times 1 by the position to set by the base and add the number - 1.

Example, added in unecessary parentheses for clarity:
number = 135;

(1 * 100) + 0 +
(1 * 10) + 2 +
(1 * 1) + 4
= 135

I suggest you use the power "^" operator rather than multiplier, easier to look at, same thing.

(10 ^ 2) + 0 = 100
(10 ^ 1) + 2 = 30
(10 ^ 0) + 4 = 5

100 + 30 + 5 = 135
Last edited on
Concatenation is not a mathematical operation


Yes, it is. Turbine and I have both provided algorithms for it. I'm only asking as to how *would* you define it for negative numbers?

And btw, just for the sake of knowing... how would you turn an int into a string anyway?
Last edited on
???
(10 ^ 2) + 2 = 12.

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.
I don't know how I made that mistake, fixed now.

Agreed, concatenation nor adding is the correct term. Appending is however an accepted term. ;)
Last edited on
Topic archived. No new replies allowed.