# Thread: Multipling Integers in Arrays

1. ## Multipling Integers in Arrays

I am having a problem trying to multiply arrays. Everything works fine until the second(bottom) number is more then 6 digits. Any help would be great.
Here is what I have:

Code:
```CHugeInt CHugeInt::operator*(const CHugeInt &n)
{
CHugeInt cTemp, cWork;
int nCarry = 0;

for (int y = 29; y >= 0; y--)
{
for (int i = 0; i <= 29; i++)
{
cWork.m_nInt[i] = 0;
}

for (int x = 29, a = y; x >= 0; x--, a--)
{
cWork.m_nInt[a] = ((m_nInt[x] * n.m_nInt[y]) % 10) + nCarry;
nCarry = (m_nInt[x] * n.m_nInt[y]) / 10;
}

cTemp = cTemp + cWork;
}

return (cTemp);
}```

2. My guess would be that your array m_nInt stores a value that is to big. Is m_nInt of type int?? It would be easier if you posted more code.

Also the code
Code:
`nCarry = (m_nInt[x] * n.m_nInt[y]) / 10;`
If m_nInt[x] and n.m_nInt[y]) stores large values nCarry wouldnīt hold such big numbers.

3. the variable m_nInt[x] only holds numbers between 0-9.
ex:

the number 12345 is stored like this.

m_nInt[29] = 5
m_nInt[28] = 4
m_nInt[27] = 3
m_nInt[26] = 2
m_nInt[25] = 1
m_nInt[24] = 0
...
...
...
...
m_nInt[0] = 0

4. Hmm, this is tricky. I have double-checked it many times and it should work. You say that when the second number is more than 6 digits something goes wrong. What number are you using in that function(some example just). Can it be that the number is larger than 10^29??? And are you absolutly sure that the overloading of + is correct implemented??

5. Maybe should you also "reset" the cTemp-array in the beginning

Code:
```for (int k = 0; k <= 29; k++)
{
cTemp.m_nInt[k] = 0;
}```

6. I have a for loop their for cWork. cTemp is set to 0's when it is declared, default constructor sets these to 0. When adding large numbers, ex. 999999999999999999 + 7654321 it displays the correct answer. So I am pretty sure the + operator is working correctly. The example that isn't working is:
7654321 * 78912345

m_nInt[29] = 1 n.m_nInt[29] = 5
m_nInt[28] = 2 n.m_nInt[28] = 4
m_nInt[27] = 3 n.m_nInt[27] = 3
m_nInt[26] = 4 n.m_nInt[26] = 2
m_nInt[25] = 4 n.m_nInt[25] = 1
m_nInt[24] = 5 n.m_nInt[24] = 9
m_nInt[23] = 6 n.m_nInt[29] = 8
m_nInt[22] = 7 n.m_nInt[29] = 7

I added my own test examples and determined that after the bottom number hits the n.m_nInt[24] spot it doesn't work correctly.

7. cTemp is set to 0's when it is declared, default constructor sets these to 0

In other words you "resets" cTemp only once. Is it possible that cTemp is used before you invoke operator*. In that case there might be garbage values which can give unexpected results.

8. Ahhh got it . Check in this code-snippet

Code:
```HugeInt HugeInt::operator *(const HugeInt &ref)
{
HugeInt Result; //Constructor should set all elements in array to 0
for (int y = 29; y >= 0; y--)
{
HugeInt tempResult;
int nCarry=0;
for (int x = 29,a = y; x >= 0; x--,a--)
{
tempResult.number[a] = ((number[x] * ref.number[y]) % 10) + nCarry;
nCarry = ((number[x] * ref.number[y]) / 10);
}

Result = Result + tempResult;
}
return Result;
}```
Iīve made my own version of yours CHugeInt. Iīve notised one that you donīt reset yours nCarry after a "whole" multiplication and every time you have garbage values for cCarray it results that you "sometimes" get strange results. Be sure that you have overloaded the operators right (operator + and =)

Iīve have testet it and it should work.

9. Thank you very much, it works better but there is still a little problem.
Example: The first is from the program, the second is from a calculator.

123456789 * 123456789 = 15241578550090521

123456789 * 123456789 = 15241578750190521

So everything is working good, but somewhere there is a problem with the math.
Code:
```CHugeInt CHugeInt::operator+(const CHugeInt &n)
{
CHugeInt cTemp;
int nCarry = 0;

for (int x = 29; x >= 0; x--)
{
cTemp.m_nInt[x] = m_nInt[x] + n.m_nInt[x] + nCarry;

if (cTemp.m_nInt[x] > 9)
{
cTemp.m_nInt[x] %= 10;
nCarry = 1;
}
else
{
nCarry = 0;
}
}

return (cTemp);
}```

10. Here is a easier version

Code:
```HugeInt HugeInt::operator +(const HugeInt &n)
{
int nCarry=0;
HugeInt cTemp;
for (int i = 29; i >=0; i--)
{
cTemp.m_nInt[i] = ((m_nInt[i] + n.m_nInt[i]) % 10) + nCarry;
nCarry = (m_nInt[i] + n.m_nInt[i]) / 10;
}
return cTemp;
}```
Hopefully it works

11. Thank you very much, everything works now. I'll post again when division doesn't work