# How do I add two long intergers of different lengths using STL?

Printable View

• 04-03-2003
doampofo
How do I add two long intergers of different lengths using STL?
This funtion adds two long intergers of the same length, this one seems to be fine.
Code:

```. void add_integers(INTDEQUE &sum, INTDEQUE X, INTDEQUE Y) {         int x,y;         int carry =0;         int result =0;         while (!X.empty() && !Y.empty())         {                 x = X.back ();                 y = Y.back ();                 cout <<"\n  " << x << ", " << y;                 X.pop_back ();                 Y.pop_back ();                 result = x+y + carry;                 {                         carry = result / 10;                         result = result % 10;                 }                 sum.push_front (result);         } }```
.

However I changed the code to this, so that deques of different lenghts can be added. But it doesn't work. Could you please show me where I'm going wrong?

Code:

```. void sum_accumulate (INTDEQUE &sum,  INTDEQUE X, INTDEQUE Y) {         int x,y;         int carry = 0;         int result = 0;         while (!X.empty () && !Y.empty ())         {                 x = X.back ();                 y = Y.back ();                 cout <<"\n  " << x << ", " << y;                 X.pop_back ();                 Y.pop_back ();                                 result = x + y + carry;                 carry = result / 10;                 result = result % 10;                 sum.push_front (result);                 sum.insert(sum.end(), '-1');         }           INTDEQUE::iterator pdeque;                  cout << "\nThe sum of the two numbers is :";           for (pdeque = sum.begin();  sum.end != '-1'; pdeque++)                     cout <<  *pdeque;           cout << '\n'; }```
.
Thanks.
• 04-03-2003
JasonD
I'm not sure what the problems are, since I have no idea what INTDEQUE is, but it appears that you are creating your own addition function for your own number class. From a quick view I can give you an optimization tip: Don't divide or use modulus, since they are the slowest operations for a CPU. You know x+y+carry will never be over 19, so all you really need to do is a check to see if it is over 10. If so, carry is 1, and subtract 10 from that sum. A compare and a potential subtract is FAR quicker than a divide and a modulus (if you were programming ASM, you could at least combine the divide/modulus together, since its the same command - perhaps C optimizes this for you).
• 04-03-2003
doampofo
Thanks JasonD,
I didn't think about it that way. I just changed my code. And it works the same way.
However, my problem still persists.
lets say i add 456 to639 ( i'm using small integers for the sake of an example).
Instead of printing out 1095.... it prints out 095. It leaves out the last 1.
secondly if I add numbers like 45 and 456, it only adds 45 and 45 giving a result of 90.
:confused:
• 04-04-2003
JasonD
You will need to show me what INTDEQUE is before I can answer that question.
• 04-04-2003
elad
while (!X.empty () && !Y.empty ())

this means as soon as either X or Y is empty stop the loop. That's why its only adding a total number of digits equal to the shortest number and there's no way to handle the carry, if any. So analyze why the loop stopped and handle each posibility:

if(X.empty() && Y.empty())
what do you do if there is still a carry from the last addition?

else if(X.empty() && !Y.empty())
what do you do with stuff still in Y?

else if(!X.empty() && Y.empty())
what do you do with stuff still in X?