Home > Software design >  My code is incapable of computing really large sums and I'm not sure why
My code is incapable of computing really large sums and I'm not sure why

Time:10-10

Here is my code. The point of this exercise was to create code that can compute very large sums. I am reversing both strings, adding, and then reversing the resultant string back.

// Basic mechanism: 
//reverse both strings
//reversing the strings works because ex. 12 12 is the same as 21 21=42->reverse->24
//add digits one by one to the end of the smaller string
//dividing each sum by 10 and attaching the remainder to the end of the result-> gets us the carry over value 
// reverse the result.
//ex. 45 45 ->reverse = 54 54 -> do the tens place -> 0->carry over -> 4 4 1 -> result= 09 -> reverse -> 90.
#include <iostream>
#include <cstring>
using namespace std;

    string strA;
    string strB;
    string ResStr = ""; // empty result string for storing the result 
    int carry =0;
    int  sum; //intermediary sum
    int n1; //length of string 1
    int n2; // length of string 2
    int rem; // remainder
int main()
{
    cout << "enter" << endl;
    cin >> strA;
    cout << "enter" << endl; // I didn't know how to write this program to use argv[1] and argv[2] so this was my solution 
    cin >> strB;
  
    // turning the length of each string into an integer 
    int n1 = strA.length(), n2 = strB.length();
 
    // Reversing both of the strings so that the ones, tens, etc. positions line up (the computer reads from left to right but we want it to read from right to left)
    reverse(strA.begin(), strA.end());
    reverse(strB.begin(), strB.end());
 
  
    for (int i=0; i<n1; i  )//start at 0, perform this operation until the amount of times reaches the value of n1
    {
        //get the sum of current digits 
        int sum = ((strA[i]-'0') (strB[i]-'0') carry);
        int rem=(sum);
        ResStr =(rem '0'); //this gets the remainder and adds it to the next row

        // Calculate carry for next step. Thus works because carry is an integer type, so it will truncate the quotient to an integer, which is what we want
        carry = sum/10;
    }
 
    // Add the remaining digits of the larger number 
    for (int i=n1; i<n2; i  ) //start at n1, perform this operation until the amount of times reaches the value of n2
    {
        int sum = ((strA[i]-'0') carry);
        int rem=(sum);
        ResStr =(rem '0');
        carry = sum/10;
    }
 
    // Add remaining carry over value
    if (carry)
        ResStr =(carry '0');
    // reverse the resulting string back, because we reversed it at the beginning 
    reverse(ResStr.begin(), ResStr.end());
    
    cout << "The result is " << ResStr << endl;
    return 0;
}

It's capable of carrying over because it can do smaller numbers, but it can't do 9,999,999,999 1 for example.

CodePudding user response:

The problem is that you've mixed up the conditions in the two loops.

The algorithm requires the longest number to be in strA and the shorter to be in strB, and since:

int n1 = strA.length(), n2 = strB.length();

The correct loops will be:

for (int i = 0; i < n2; i  )  // first loop, loop short
for (int i = n2; i < n1; i  ) // second loop, do the rest in strA

Demo

I also suggest that you make the program less sensitive to in which order the user enters the numbers. After the numbers have been entered, you could swap the order if strB is longer than strA:

if(strB.length() > strA.length()) std::swap(strA, strB);

Demo

CodePudding user response:

You can use long instead of int

  •  Tags:  
  • c
  • Related