Multipling Integers in Arrays

This is a discussion on Multipling Integers in Arrays within the C++ Programming forums, part of the General Programming Boards category; I am having a problem trying to multiply arrays. Everything works fine until the second(bottom) number is more then 6 ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Question 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. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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.
    Last edited by ripper079; 10-13-2002 at 04:53 PM.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    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. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Maybe should you also "reset" the cTemp-array in the beginning

    Code:
    for (int k = 0; k <= 29; k++)
    		{
    			cTemp.m_nInt[k] = 0;
    		}
    Last edited by ripper079; 10-13-2002 at 06:32 PM.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    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. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    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.
    Last edited by ripper079; 10-14-2002 at 08:26 AM.

  8. #8
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    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.
    Here is my addition code:
    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. #10
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Thank you very much, everything works now. I'll post again when division doesn't work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing an arrays of integers
    By caduardo21 in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2005, 11:43 AM
  2. arrays of random integers & time
    By meeloff in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2004, 03:35 PM
  3. Storing integers from file into arrays???? please help
    By adzza69 in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2004, 12:28 AM
  4. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21