0

I have written code for difference between two large numbers (Code is pasted below). I am getting result (result = N1 - N2) correct if N1>N2 but not getting correct results in case N1<=N2. I am not sure what whether this is a syntax error or any logical error. Any help would be appreciated!

P.S.- I am not a professional coder, so please forgive if there is any basic mistake. Thanks in advance!

#include<bits/stdc++.h> 
using namespace std; 
#include <string>

int sgn(int x){
    if (x>=0){
        return 1;
    }
    else{
        return -1;
    }
}


int SignValue(int x){
    if (x>=0){
        return 0;
    }
    else{
        return -1;
    }
}


string SubtractBigNumbers(string s1, string s2){
    int i1=0, i2=0, i3, i4;
    i3=s1.length(), i4=s2.length();
        if (i3>i4){
            // cout << "here";
            int A[i3], B[i3], C[i3];
                for (int i2=0; i2<i3; i2++){
                    A[i2]=(int) s1[i2] - 48;
                }

                for (int i2=0; i2<i3-i4; i2++){
                    B[i2]=0;
                }

                for (int i2=i3-i4; i2<i3; i2++){
                    B[i2]=(int) s2[i2-i3+i4] - 48;
                }

                for (int i2=0; i2<i3; i2++){
                    C[i2]=A[i2]-B[i2];
                }

                for (i2=i3-1; i2>0; i2--){
                    C[i2-1]=C[i2-1]+SignValue(C[i2]);
                    C[i2]=C[i2]-10*SignValue(C[i2]);
                }

                for (i1=0; i1<i3; i1++){
                    if (C[i1]!=0){
                        break;
                    }
                }

            int id=i3-i1+1;
            string N="";
                for (i2=1; i2<id; i2++){
                    N.push_back(C[i1+i2-1] + 48);
                }

            return N;
        }
        else if (i3=i4){
            int A[i3], B[i3], C[i3];
            int j=0,k;
                for (i2=0; i2<i3; i2++){
                    A[i2]=(int) s1[i2] - 48;
                    B[i2]=(int) s2[i2] - 48;
                        if (A[i2]-B[i2]==0){
                            j=j+1;
                        }
                }

                for (i1=0; i1<i3; i1++){
                    if (A[i1]-B[i1]!=0){
                        break;
                    }
                }
                k=i1;
                if (j==i3){
                    return 0;
                }
                else{
                    i1=0;
                        for (i2=0; i2<i3; i2++){
                            C[i2]=sgn(A[k]-B[k])*(A[i2]-B[i2]);
                        }

                        for (i1=0; i1<i3; i1++){
                            if (C[i1]!=0){
                                break;
                            }
                        }
                        int i_cmod=i3-i1+1;
                        int CMod[i_cmod];
                        for (i2=0; i2<i_cmod; i2++){
                            CMod[i2]=C[i1+i2-1];
                        }

                        for (i2=i_cmod-1; i2>0; i2--){
                            CMod[i2-1]=CMod[i2-1]+SignValue(CMod[i2]);
                            CMod[i2]=CMod[i2]-10*SignValue(CMod[i2]);
                        }

                        for (i1=0; i1<i_cmod; i1++){
                            if (CMod[i1]!=0){
                                break;
                            }
                        }
                        int i_d=i_cmod-i1+1;
                        int D[i_d];
                        for (i2=0; i2<i_d; i2++){
                            D[i2]=CMod[i1+i2-1];
                        }

                        string N="";

                        if (A[k]-B[k]>0){
                            for (i2=1; i2<i_d; i2++){
                                N.push_back(D[i2] + 48);
                            }
                            return N;
                        }

                        if (A[k]-B[k]<0){
                            int E[i_d];
                            E[0]=0;
                            E[1]=-1*D[1];
                            for (i2=2; i2<i_d; i2++){
                                E[i2]=D[i2];
                            }
                            for (i2=1; i2<i_d; i2++){
                                N.push_back(E[i2] + 48);
                            }
                            return N;
                        }
                }
        }
}
// Driver code 
int main() 
{ 
    string str1 = "1222"; 
    string str2 = "121"; 
    cout << SubtractBigNumbers(str1, str2); 
    return 0; 
} 
  • 3
    Most compilers will give you a warning about `else if (i3=i4)`. – aschepler Mar 29 '20 at 15:14
  • 2
    `#include using namespace std; ` <-- Don't *ever* do that. – Jesper Juhl Mar 29 '20 at 15:16
  • I am running it on online compiler (onlinegdb.com) & it's not showing error. But problem is its not giving answer of 19-23 as -4. – udit mangal Mar 29 '20 at 15:16
  • 1
    I haven't tried to figure out the attempted logic here, but it's obviously wrong. The correct implementation for something like this will be at least one tenth the size of the shown code, will not require the use of a bunch of arrays, and strings, and is trivially simple. It would be a waste of time trying to figure out what the problem here. The right approach is to get rid of the whole thing and rewrite it from scratch, the right way. When we learn subtraction, by hand, in grade school, this is ***not*** how we do it. – Sam Varshavchik Mar 29 '20 at 15:16
  • @JesperJuhl What is the correct way of writing that? – udit mangal Mar 29 '20 at 15:17
  • The correct way would be to include, explicitly, the headers you need. Not some unportable internal header from a specific standard library implementation. And don't drag in any more names into the global namespace than you need. Start by reading [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058). – Jesper Juhl Mar 29 '20 at 15:19
  • @SamVarshavchik Any suggestions what could be more better way to do this? However, my logic is to store large numbers in string format & then do the operation on individual numbers & then combine the whole array. I have done that in excel & it is working in excel but taking a lot of time! – udit mangal Mar 29 '20 at 15:19
  • @JesperJuhl Ok i will read that. But can you please tell me my mistake in this current code? – udit mangal Mar 29 '20 at 15:22
  • You do it exactly the same way you would do subtraction by hand. Write down on a blank sheet of paper two numbers, one below the other. Now do the subtraction by hand. Make note of each, exact, step you execute, in this process. Then translate what you did ***directly*** into code. Does the shown code do the same thing? Of course not. Because, after having written down the two numbers, what is the first thing you do? Do you add up the number of digits in each number and compare them, just like the very first statement in the function, the `if` statement? Of course not. Why do you do it here? – Sam Varshavchik Mar 29 '20 at 15:22
  • @udit how do you subtract a long string of numbers on paper? Do it that way. – JohnFilleau Mar 29 '20 at 15:23
  • Check out existing bignum libraries like, for example, [GMP](https://gmplib.org/). – Jesper Juhl Mar 29 '20 at 15:23
  • I am still not able to make it smaller. Can someone at least figure out error in this code (and it is in some last steps) or can post the code in by making it smaller! – udit mangal Mar 29 '20 at 15:41
  • @udit don't try to make this current code smaller. Discard this current code. Start over from scratch. You're going to spend FAR MORE TIME trying to make this code smaller than it would take to start from scratch. I know you feel like you don't have time to start from scratch, but you DEFINITELY don't have time to try to get this code to work. – JohnFilleau Mar 29 '20 at 16:11

0 Answers0