i'm trying to add two arrays and i can't seem to get my carry to work in my add function.

for example, if array a has 1,2,3, and array b has 5,6,7, i should get 690. but since there is a flaw here, i get 680. why?

can anyone point out the flaw in my logic?

``````bool add (int a[], int b[], int sum[])
{

int x, temp;
for(int x=MAX_DIGITS-1; x>=0; x--)  // first index of array is 0 in C/C++
{

sum[x]=a[x]+b[x];

if(temp==1)
sum[x]=sum[x]+1;

if(sum[x]>=10)
{
temp=1;
sum[x]=sum[x]-10;
}

else
{
temp=0;
}

cout<<sum[x]<<" ";
}

if(( x<0 )&&(temp==1)) // = is assignment == is comparison
return false;
else
return true;

}``````

for example, if array a has 1,2,3, and array b has 5,6,7, i should get 690. but since there is a flaw here, i get 680. why?

I am not sure do I understand you correctly, you said you have two array: array a contain 1, 2, 3, array b contain 5, 6, 7 ... are you trying to add 123 + 567? or you are adding them just by number with number, like 1+2, 2+6, 3+7? Do you mind explaining it a little bit more?

I am not sure do I understand you correctly, you said you have two array: array a contain 1, 2, 3, array b contain 5, 6, 7 ... are you trying to add 123 + 567? .

Yes, I want to add 123 + 567 so I get 690.

my arrays are size 20, and they are entered as chars. then i read the char into array a in reverse order so my ones, tens, hundreds, etc. columns line up for nice easy math. except i cant get my nice easy math to work. ha

but right now i'm not carrying right and i get 680 instead of 690. doh! i can supply the whole program if that helps?

First your temp variable is not initialized but you use it.

Second check this and see if it help, look particularly at the logic inside the loop.

Third you have the x declared before the for loop and inside it. So that

Doing this works so far :

``````bool add (int a[], int b[], int sum[])
{

int x = 0, temp = 0;
for(x = MAX_DIGITS-1; x>=0; x--)  // first index of array is 0 in C/C++
{

sum[x]=a[x]+b[x];

if(temp==1)
sum[x]=sum[x]+1;

if(sum[x]>=10)
{
temp=1;
sum[x]=sum[x]-10;
}

else
{
temp=0;
}

}

if(( x<0 )&&(temp==1)) // = is assignment == is comparison
return false;
else
return true;

}``````

the link you provided, i saw that, but it kind of went over my head. i thought i was getting kind of ok at c++ but this is making me feel retarded. its a first semester c++ class.

so i changed it from a type bool to type void so i didnt have the int x initialized outside of the for loop. i still dont get why it's not carrying the 1 right. here is my whole program. i know there are other issues with it, but i think i need to get the addition working before i iron out some of the other 'housekeeping' issues.

``````#include <iostream>
#include <cctype>

using namespace std;

const int MAX_DIGITS = 20;

void inputBigIntA (const char theNumA[], int a[], int& sizeA, int& qty);
void inputBigIntB (const char theNumB[], int b[], int& sizeB, int& qty);
void add (int a[], int b[], int sum[]);

int main()
{

int qty=0;
int sizeA, sizeB;        // number of digits in the integer
int a[MAX_DIGITS]={0}, b[MAX_DIGITS]={0}, sum[MAX_DIGITS];
int digit[MAX_DIGITS]; // the digits stored in reverse order
char theNumA[MAX_DIGITS + 1];
char theNumB[MAX_DIGITS + 1];

// code below COUNTS the number of digits entered
cin >> theNumA;

for ( ; (qty <= MAX_DIGITS) && (theNumA [qty] != '\0'); qty ++);

if (qty == MAX_DIGITS+1)
{
sizeA = -1;
}

// loop to get proper input?? if qty > max_digits

cout << "you entered: " << endl;
cout << theNumA << endl;
cout << "the number you entered is " << qty << " digits long" <<endl;

// if 22 digits entered, stack overflow, need to fix

inputBigIntA (theNumA, a, sizeA, qty);

// input theNumB

cin >> theNumB;

qty=0;

for ( ; (qty <= MAX_DIGITS) && (theNumB [qty] != '\0'); qty ++);

if (qty == MAX_DIGITS+1)
{
sizeB = -1;

}

cout << "you entered: " << endl;
cout << theNumB << endl;
cout << "the number you entered is " << qty << " digits long" <<endl;

inputBigIntB (theNumB, b, sizeB, qty);

return 0;

}

void inputBigIntA (const char theNumA[], int a[], int& sizeA, int& qty)
{

sizeA = qty;

qty--;

for (int x=0; qty>=0; x++)
{
if (isdigit (theNumA[qty]))
{  a[x] = theNumA[qty] - 48;

cout << a[x] << endl;
}
else
{
sizeA = -1;
return;
}
qty--;

}

cout << endl << theNumA << endl;
cout << sizeA << endl << endl;

}

void inputBigIntB (const char theNumB[], int b[], int& sizeB, int& qty)
{
sizeB = qty;

qty--;

for (int x=0; qty>=0; x++)
{
if (isdigit (theNumB[qty]))
{
b[x] = theNumB[qty] - 48;
cout << b[x] << endl;
}

else
{
sizeB = -1;
return;
}

qty--;

}

}

void add (int a[], int b[], int sum[])
{

int temp = 0;
for(int x=MAX_DIGITS-1; x>=0; x--)
{

sum[x]=a[x]+b[x];

if(temp==1)
sum[x]=sum[x]+1;

if(sum[x]>=10)
{
temp=1;
sum[x]=sum[x]-10;
}

else
{
temp=0;
}

cout<<sum[x]<<" ";
}

}``````

WAIT. hold the phone.

i changed:

for(int x=MAX_DIGITS-1; x>=0; x--)

to

for(int x=0; x<=MAX_DIGITS-1; x++)

because it was reading from the wrong direction. now i get the right math. i just have to change the order and get it back out again in the proper order......if that makes sense?