Thread: How do I add two long intergers of different lengths using STL?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    How do I add two long intergers of different lengths using STL?

    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.

  2. #2
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    I'm not sure what the problems are, since I have no idea what INTDEQUE is, but it appears that you are creating your own addition function for your own number class. From a quick view I can give you an optimization tip: Don't divide or use modulus, since they are the slowest operations for a CPU. You know x+y+carry will never be over 19, so all you really need to do is a check to see if it is over 10. If so, carry is 1, and subtract 10 from that sum. A compare and a potential subtract is FAR quicker than a divide and a modulus (if you were programming ASM, you could at least combine the divide/modulus together, since its the same command - perhaps C optimizes this for you).

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Lightbulb

    Thanks JasonD,
    I didn't think about it that way. I just changed my code. And it works the same way.
    However, my problem still persists.
    lets say i add 456 to639 ( i'm using small integers for the sake of an example).
    Instead of printing out 1095.... it prints out 095. It leaves out the last 1.
    secondly if I add numbers like 45 and 456, it only adds 45 and 45 giving a result of 90.

  4. #4

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    while (!X.empty () && !Y.empty ())

    this means as soon as either X or Y is empty stop the loop. That's why its only adding a total number of digits equal to the shortest number and there's no way to handle the carry, if any. So analyze why the loop stopped and handle each posibility:

    if(X.empty() && Y.empty())
    what do you do if there is still a carry from the last addition?

    else if(X.empty() && !Y.empty())
    what do you do with stuff still in Y?

    else if(!X.empty() && Y.empty())
    what do you do with stuff still in X?
    Last edited by elad; 04-04-2003 at 01:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. hton long long
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 06-01-2008, 08:26 PM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 12:26 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM