Thread: Adding strings of integers

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Adding strings of integers

    I'm trying to write an algorithm for a larger project that will take two strings which are both large integers (only using 10 digit numbers for the sake of this demo) and add them together to produce a final string that accurately represents the sum of the two original strings. I realize there are potentially better ways to have gone about this from the beginning but I am supposed to specifically use strings of large integers as opposed to a long integer.

    My thinking was to take the two original strings, reverse them so their ones position, tens position, and so on all line up properly for adding. Then one position at a time, convert the characters from the strings to single integers and add them together and then use that sum as the ones position or otherwise for the final string, which once completed will also be reversed back to the correct order of characters.

    Where I'm running into trouble I think is in preparing for the event in which the two integers from the corresponding positions in their strings add to a sum greater than 9, and I would then have carry over some remainder to the next position. For example, if I had 7 and 5 in my ones positions that would add to 12, so I would keep the 2 and add 1 to the tens position once it looped back around for the tens position operation.

    I'm not getting results that are in any way accurate and after spending a large amount of time stumbling over myself trying to rectify my algorithm, I am not sure what I need to do to fix this.

    Hopefully my intended process is clear and someone will be able to point me in the right direction or correct some mistake I may have in my program.

    Thanks in advance.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    
    using namespace std;
    
    
    int main ()
    {
        string str1 = "1234567890", str2 = "2345678901"; //Two original strings of large integers
        string rev_str1, rev_str2;
        int int1 = 0, int2 = 0;
        string final; //Final product string, sum of two original strings
        int temp_int = 0, buffer_int, remainder = 0;
        string temp_str = "", buffer_str;
        char buffer[100] = {0};
    
    
        cout << "str1 = " << str1 << endl;
        cout << endl;
        cout << "str2 = " << str2 << endl;
        cout << endl;
    
    
        rev_str1 = string(str1.rbegin(), str1.rend());
    
    
        rev_str2 = string(str2.rbegin(), str2.rend());
    
    
        for (int i = 0; i < 10; i++)
        {
            buffer_str = rev_str1.at(i);
    
    
            int1 = atoi(buffer_str.c_str());
    
    
            buffer_str = rev_str2.at(i);
    
    
            int2 = atoi(buffer_str.c_str());
    
    
            buffer_int += (int1 + int2 + remainder);
    
    
            remainder = 0;
    
    
            while (buffer_int > 9)
            {
                buffer_int -= 10;
    
    
                remainder += 10;
            }
    
    
            temp_str = itoa(buffer_int, buffer, 10);
    
    
            final += temp_str;
        }
    
    
        final = string(final.rbegin(), final.rend());
    
    
        cout << "final = " << final << endl;
    
    
        cout << endl;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > remainder += 10;
    But it's only worth 1 in the next column isn't it.

    If you do
    456
    789 +
    on paper, 6+9 is 17, so it's 7 and carry 1 (not 10)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    In the gradeschool math algorithm, the carry and the number which is currently to be added (buffer_int) are each less than 10. If both your strings are len characters, I would use something like this

    Code:
    int carry = 0;
    for (int i = 0; i < len; i++)
    {
        int1 = toInt(rev_str1.at(i));
        int2 = toInt(rev_str2.at(i));
        buffer_int = (int1 + int2 + carry);
        carry = buffer_int / 10;
        buffer_int %= 10;
        final += toString(buffer_int);
    }
    if (carry > 0) {
        final += toString(carry);
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There was another guy in here last week asking about this. I'm guessing he's in the same class. Don't worry, he wasn't doing any better than you are.

    What are you using to debug this? Are you following the same calculation on paper while you step through the code?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Was just using Code::Blocks. Yeah, I thought I was on track with it but after giving it a day I went back and figured it out. Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding group of integers to string
    By InicDominus in forum C Programming
    Replies: 2
    Last Post: 10-10-2012, 10:14 AM
  2. Adding Long Integers!
    By alireza beygi in forum C Programming
    Replies: 1
    Last Post: 12-17-2011, 07:11 AM
  3. adding 16bit and 32 bit integers
    By kris_perry2k in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 09:49 AM
  4. Adding integers to a string
    By Kyoto Oshiro in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 08:01 AM
  5. C++ help!!!!! Adding integers
    By PR1MO in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 05:40 PM

Tags for this Thread