Home > Enterprise >  My code for computing large sums fails this very specific test case and I'm not sure why
My code for computing large sums fails this very specific test case and I'm not sure why

Time:10-11

The point of this exercise was to create code that can compute very large sums by using an algorithm close to how you would do it by hand. My code seems to work just fine, except for one specific test case, which is 9223372036854775808 486127654835486515383218192. I'm not sure what is special about this specific test case for it to fail, because the code is totally capable of carrying over, as well as adding numbers that have two different amounts of digits...Help would be greatly appreciated.

Edit: The output for this case is '2 -0 *.4058858552237994000

// 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();
 
if (n1<n2){
    swap(strA,strB);
}//for this part I have no idea why this has to be the case but it only works if this statement is here 

    // 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<n2; i  )//start at 0, perform this operation until the amount of times reaches the value of n2
    {
        //get the sum of current digits 
        int sum = ((strA[i]-'0') (strB[i]-'0') carry);
        int quotient=(sum/10);
        int rem=(sum-10*quotient);
        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=n2; i<n1; i  ) //start at n1, perform this operation until the amount of times reaches the value of n1
    {
        int sum = ((strA[i]-'0') carry);
        int quotient=(sum/10);
        int rem=(sum-10*quotient);
        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;
}

CodePudding user response:

After a lot of experimentation, what made the difference for me was replacing n1 and n2 entirely with strA.length() and strB.length() respectively in the for loop declarations. I'm not entirely sure why this happens, so if anyone has an explanation- please forward it.

Anyway, here's my version of the code:

//Basic mechanism: reverse both strings
//reversing the strings works because ex. 12 12 is the same as 21 21=42->reverse->24 (for single-digit sums smaller than 10)
//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 (mod 10 arithetic)
//ex. 45 45 ->reverse = 54 54 -> do the tens place -> 0->carry over -> 4 4 1 -> result= 09 -> reverse -> 90.
//ex. 90 90 ->reverse = 09 09 -> do the tens place -> 0->carry over ->0  9 9 ->carry over->1  result= 081 -> reverse -> 180.

#include <iostream>
using std::cin; using std::cout;
#include <cstring>
using std::string;
#include <algorithm>
using std::reverse;

string strA, strB, ResStr = "";
int carry = 0, sum;

int main() {

    //get user input
    cout << "Enter first number:  "; cin >> strA;
    cout << "Enter second number: "; cin >> strB; cout << "\n";
 
    if (length_strA < length_strB){ swap(strA,strB); } //force stringA to be larger (or equal) in size

    //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()); cout << strA << "\n";
    reverse(strB.begin(), strB.end()); cout << strB << "\n";
 
  
   for (int i = 0; i < strB.length(); i  ) {
      sum = int(strA[i])-48   int(strB[i])-48   carry/10;
      ResStr  = char(sum % 10) 48;
      carry = sum - sum;
   }
 
    // Add the remaining digits of the larger number
    //start at strB.length, perform this operation until the amount of times reaches the value of strA.length 
    for (int i = strB.length(); i < strA.length(); i  ) {
        sum = int(strA[i])-48   carry/10;;
        ResStr  = char(sum % 10) 48;
        carry = sum - sum;
    }
 
    // Add remaining carry over value
    if (carry != 0) { ResStr  = char(carry) 48; } 
    
    // reverse the resulting string back, because we reversed it at the beginning 
    reverse(ResStr.begin(), ResStr.end());
    
    //return user result
    cout << "The result is: " << ResStr << "\n";
    
return 0; }

To answer why, it's because even once strB ended, it kept iterating through making a new sum in the first loop until strA ended. This meant it was reading null values for strB[i] and adding that to strA[i] carry- which effectively shifted all the characters into the noisy-looking mess it was.

  •  Tags:  
  • c
  • Related