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.