0

We got an assignment to write a function multiplying big numbers, which prints the result on screen.

Here is my code so far:

void mnozenie(string a, string b)
{
    if(a=="0"||b=="0")
        cout<<0;
  else 
  {

    bool minus=false;

    if((a[0]=='-'&&b[0]!='-')||(b[0]=='-'&&a[0]!='-'))
        minus=true;

    if(a[0]=='-')
        a.erase(a.begin());

    if(b[0]=='-')
        b.erase(b.begin());    

    if(a.size()<b.size())
        swap(a,b);

    vector<int> C;
    for(int i=0; i<(int)a.size()+(int)b.size(); i++)
        C.push_back(0);

    for(int i=(int)b.size()-1; i>=0; i--)
    {
        for(int j=(int)a.size()-1; j>=0; j--)
        {
            C[i+j+1]+=((a[j]-'0')*(b[i]-'0'));
            int k=i+j+1;
            while(C[k]>9&&k>0)
            {
               C[k-1]+=C[k]/10;
               C[k]=C[k]%10;
               k--; 
            }

        }



    }

    while(C[0]==0)
        C.erase(C.begin());

    if(minus)
        cout<<"-";

    for(int i=0; i<(int)C.size(); i++)
        cout<<C[i];

    cout<<endl;
   }     



}

I haven't found a case when it's incorrect, but when I try to submit it I get ANSWER mistake. So probably there is such a case.

Geek
  • 19
  • 2
  • 1
    what result do you get? what result do you expect? – SeanC Dec 04 '17 at 20:40
  • 2
    Please format your code better so it's easier for us to read. Example: `int k = i + j + 1;` – Almo Dec 04 '17 at 20:41
  • 2
    What is the input? What is the output? What output do you expect? What does your debugger tell you? – Kevin Dec 04 '17 at 20:41
  • *I can't see where is my mistake though - spent quite a long time on this code.* -- I hate to say this, but saying things like that should never be an option **if** you wrote the code yourself. Every time you write a program, you must know for sure what every line, loop, variable declaration, assignment, is *supposed* to do. If you then run your program and it doesn't behave the way it's supposed to behave, you should know exactly what to look for to see where the issue is. – PaulMcKenzie Dec 04 '17 at 21:18
  • now it's almost correct - I can't find any case when it gives incorrect results but when I submit my code I get ANSWER error. – Geek Dec 04 '17 at 21:19
  • *We got an assignment to write a function multiplying big numbers* -- So did you test this on *small* numbers? Maybe you put all of your effort with big numbers, and it might fail on something simple like 2 x 2, or 3 x 2. – PaulMcKenzie Dec 04 '17 at 21:22
  • @PaulMcKenzie I would never think that everybody who has problem with their code had copied it. I wrote this code myself, and I know what each line is supposed to do, but when I try to submit my code I get an answer error. I very very often have troubles with code I wrote, even though I write it myself. And I find it still difficult to know exactly where I made a mistake. The fact that somebody has just started to learn programming doesn't mean they should understand everything or be a thief. – Geek Dec 04 '17 at 21:22
  • Yes, I've tried, and it seems to work just fine. – Geek Dec 04 '17 at 21:25
  • No, what I'm saying is that you don't write a program hoping it works, and if it doesn't, be thrown so far off as to what may be wrong. As to your program, again, did you test this against simple numbers that don't require "big integers"? A lot of times these assignments are written with the programmer having tunnel vision trying to make "big integers" work, and the simple integers get ignored and fail to work as a result. – PaulMcKenzie Dec 04 '17 at 21:27
  • Also, as the other comment suggested, do you know if the numbers will always be trimmed of leading zeros, spaces, etc? You didn't specify what, if any, special cases may arise (and didn't provide any input as to what fails). – PaulMcKenzie Dec 04 '17 at 21:32
  • All other things aside, I see bad things happen if either of the strings is empty. (Unchecked access of `[0]`, and `size() - 1` isn't a good idea either.) – DevSolar Dec 04 '17 at 21:33
  • @PaulMcKenzie Apologies then. I think I know my mistake now - I forgot about printing endl when the result is 0. – Geek Dec 04 '17 at 21:34
  • @Natalia -- As the other comment mentioned, you didn't check for an empty string being passed to your function. Also, `vector C(a.size(), b.size());` instead of that `for` loop. – PaulMcKenzie Dec 04 '17 at 21:45

1 Answers1

1

Not sure it's the only problem but is a problem...

When you write

  if ( a[0] = '-')
     a.erase(a.begin());

  if ( b[0] = '-')
     b.erase(b.begin());  

you, in the tests, assign '-' to a[0] and b[0].

I presume your intention was to test if a[0] is '-' (and the same with b[0]

So I suggest to modify in

  // .......vv
  if ( a[0] == '-')
     a.erase(a.begin());

  // .......vv
  if ( b[0] == '-')
     b.erase(b.begin());   
max66
  • 65,235
  • 10
  • 71
  • 111
  • THANK YOU SO MUCH! omg, I still make such silly mistakes. //update Unfortunately it still produces uncorrect result :( But it's better than it was – Geek Dec 04 '17 at 20:54
  • 3
    @Natalia - additional suggestion: activate the compilers configuration options to get **all** warnings and consider all warnings as errors. The compilers usually signal problems like assign/test as warnings. IMHO, a good code **must** be error free. – max66 Dec 04 '17 at 20:59