# Thread: what is wrong with my function?

1. ## what is wrong with my function?

I am using this function with my ADT Natural number in large precision
(My DT is
Code:
```struct NatNo{ unsigned n; //number of digits
unsigned V[30];  // array of digits in order of introduction ( starting from 0)
}```
)

Code:
```void add(NatNo a, NatNo b, NatNo &sum)
{
unsigned i;

unsigned maxlength= a.n>b.n? a.n: b.n;

init(sum, maxlenght); /* initializes sum with maxlength elements of 0 (s.n=maxlength and the array(s.V) with zero values ) */

for(i=a.n;i<maxlength;i++)
a.V[i]=0;

for(i=b.n;i<maxlength;i++)
b.V[i]=0;

for(i=maxlength-1;i>=0;i--);
{
sum.V[i+1] += (a.V[i] + b.V[i]) % 10;  /* the sum will have an additional
digit(pozition 0) for overflow */
sum.V[i] += (a.V[i] + b.V[i]) / 10;  // transportation
}

}```

2. Seems that you want to add two numbers. But why are you resetting the operands a and b to zero before adding ?
Kurt

3. I am only adding zeroes in the number with less digits...

4. instead of init(sum,maxlenght) i use init(sum,maxlength+1) (as i said, the sum will have one digit more than the biggest number) but still does not solve the problem

5. Sorry, I overlooked that.
But
Code:
```sum.V[i+1] += (a.V[i] + b.V[i]) % 10;
sum.V[i] += (a.V[i] + b.V[i]) / 10;```
guess you have to do something like this
Code:
```sum.V[i+1] = (sum.V[i+1] + a.V[i] + b.V[i]) % 10;
sum.V[i] = (sum.V[i] + a.V[i] + b.V[i]) / 10;```
Kurt

6. thanks, Kurt...
to make sure that addition works right, i introduced an auxilliary variable, which calculates the partial sums on every position(aux),and one for the transportation digit(t)
Code:
```unsigned aux=0,t=0;
for(i =maxlength-1;i>=0;i--);
{
aux=t+a.V[i]+b.V[i];
sum.V[i+1]= aux % 10;
t=aux/10;
}
sum.V[0]=t;```
unfortunately, this STILL doesn't solve the problem...

7. Code:
`for(i =maxlength-1;i>=0;i--);  // remove the semicolon`
BTW I'm too lazy to create a compilable program from your code by myself.
You'd get much more help if you would create such a program. Also it's quite possible that the real error is somewhere else.
Kurt

8. the semicolon was embarrassing but the problem was indeed somewhere else...i eventually managed to solve it