I am trying to implement the multiplication of string numbers with Stobics. The input is two numbers written in string. Then I translate them into an array and then try to multiply. Already analyzed the algorithm several times and can not find the error. Help to understand, please.

#include "stdafx.h" #include <string> #include <iostream> #include <climits> using namespace std; int main() { string a, n; int* A, *B, *C, length, l, cc; //cin>>a; //cin>>n; a = "1"; n = "1"; A = new int [a.size()]; B = new int [n.size()]; for (int i = 0; i < a.size(); i++) A[i] = a[a.size() - i - 1] - '0'; for (int i = 0; i < n.size(); i++) B[i] = n[n.size() - i - 1] - '0'; length = a.size() + n.size() + 1 ; l = length; C = new int [length]; for (int ix = 0; ix < a.size() - 1; ix++) for (int jx = 0; jx < n.size() - 1; jx++) { C[ix + jx - 1] += A[ix] * B[jx]; cc == A[ix] * B[jx]; } for (int ix = 0; ix < length; ix++) { C[ix + 1] += C[ix] / 10; C[ix] %= 10; } while (C[length] == 0) length-- ; for (int i = l - 1; i > -1; i--) cout << C[i]; system("PAUSE"); } 

Closed due to the fact that off-topic participants αλεχολυτ , aleksandr barakin , user194374, Alex , Denis Bubnov 5 Dec '16 at 10:39 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - αλεχολυτ, aleksandr barakin, Community Spirit, Alex, Denis Bubnov
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    Should be enough

     length = a.size() + n.size() - 1; 

    Another C array is uninitialized with random numbers. It should be reset after creation.

    And you deduce it not so - reduce length , and in a cycle use l .

    Also, your multiplication cycles are too short.

    I tried to correct your mistakes:

     int main() { string a,n; int *A, *B, *C, length, l,cc; cin>>a; cin>>n; //a="1"; //n="1"; A=new int [a.size()]; B=new int [n.size()]; for (int i=0; i<a.size(); i++) A[i]=a[a.size()-i-1]-'0'; for (int i=0; i<n.size(); i++) B[i]=n[n.size()-i-1]-'0'; length = a.size() + n.size() - 1 ; l=length; C=new int [length]; for (int ix = 0; ix < length; ix++) { C[ix] = 0; } for (int ix = 0; ix < a.size(); ix++) { for (int jx = 0; jx < n.size(); jx++) { C[ix + jx] += A[ix] * B[jx]; } } for (int ix = 0; ix < length-1; ix++) { C[ix + 1] += C[ix] / 10; C[ix] %= 10; } while (C[length] == 0) length-- ; for(int i=length; i>-1; i--) cout<<C[i]; system("PAUSE"); } 
    • Thank you very much. But the conclusion I had was made correctly: for (int i = length-1; i> -1; i--) cout << C [i] ;. Otherwise, it affects the empty value of the array and displays the wrong answer. - Maxitt
    • Look again at your source - you do not derive from length , but from l ! What I wrote is that you change the length , but output something, starting with l , which has not been changed! - Mikhailo Nov.

    Already analyzed the algorithm several times and can not find the error.

    At least here:

     cc==A[ix] * B[jx]; 

    should be = , not == .

    The rest is too lazy to read.