-1

I am a BEGINNER. I wrote a function to multiply big integers. When the function multiplybig(string a,string b) is called it goes directly to the return statement of the same function. I dont know where exactly the bug is. can someone please help me with this?

#include<iostream>
#include<string>
using namespace std;
string multiplybig(string a,string b)
{
    string c;
    int ar1[a.length()] , ar2[b.length()] ; int ans[a.length()+b.length()]={0};

    for(int i=0 ; i<a.length(); i++){
        ar1[i]=a[i]-'0';
    }

    for(int i = 0 ; i<a.length(); i++){
        ar2[i]=b[i]-'0';
    }

    int x = 0 ; int y = 0;

    for(int i=a.length()-1;i>=0;i--) {
        for(int j=b.length()-1;j>=0;j--){
            ans[x]+=ar1[i]*ar2[j];
            x++;
        }
        y++; x=y;
    }

    for(int i=0,j=a.length()+b.length()-1;i<a.length()+b.length();i++,j--){
        c[i]=ans[j]+'0';
    }

    return c;
}
int main()
{
    string a( "123" );
    string b( "111" );
    cout<< multiplybig( a, b );
    return 0;
}
  • 1
    Looks like a typo there: `for(int i = 0 ; i `b.length()`? Probably you go out of bounds there. – Lukas-T Jul 05 '20 at 09:49
  • Secondly some hints: `int ar1[a.length()]` is a VLA, which [are not part of the standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard), use `std::vector ar1(a.length())` instead and [`using namespace std;` is bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Lukas-T Jul 05 '20 at 09:51
  • @churill yeah thank you, i will edit that ( but problem exists even after correcting that) – Cams Boyfriend Jul 05 '20 at 09:54
  • Second problem I just spotted is `c[i]=ans[j]+'0';`. `c[i]` is out of bounds, since `c` is an empty string. Guess it's a good time to [learn how to use a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Lukas-T Jul 05 '20 at 09:56
  • [You don't need to tell us that you are new to `X`. It is just noise here](https://meta.stackoverflow.com/q/296391/11107541). – starball Dec 18 '22 at 23:38

1 Answers1

0

To sum up the comments:

Don't use VLAs

int ar1[a.length()] , ar2[b.length()] ; int ans[a.length()+b.length()]={0};

are VLAa. Those are not part of the standard, but a compiler specific extension. The right way would be to use std::vector:

std::vector<int> ar1(a.length());
std::vector<int> ar2(b.length()); 
std::vector<int> ans(a.length() + b.length());

(one statement per line for good readability)

Typo in the loop

for(int i = 0 ; i<a.length(); i++){
    ar2[i]=b[i]-'0';
}

Since you read from b the condition must be i < b.length();

Out-of-bounds-access on c[i]

Before the line

c[i]=ans[j]+'0';

c get's never assigned anything (it's an empty string), so any access using the subscript operator is an out-of-bounds-access. Either resize it first:

c.resize(ans.size());

or use push_back instead:

c.push_back(ans[j]+'0');

The last error (that the returned value was ignored) you already fixed in your last edit. With all those changed I got the expected result

013653
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • HUGE THANKS!!! I did everything you said and learn something new today. Thanks but will using dynamic array in heap instead of vector be fine ? What's the difference? – Cams Boyfriend Jul 06 '20 at 12:08
  • A vector is basically just a nice, shiny wrapper around a dynamic array. It can be used in just the same way, has almost 0 overhead (at least with compiler optimizations turned on) but provides many useful features. A `vector` should always be preferred over manual memory management. – Lukas-T Jul 06 '20 at 12:20
  • No problem, consider [accepting](https://stackoverflow.com/help/someone-answers) the answer if it was helpful to you :) – Lukas-T Jul 06 '20 at 16:27