-4

I am a beginner in C++. I am trying this long integer multiplication. I am not understanding that how the value ofsum[3][0] is changing in the subsequent loop cycle.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string num1, num2;
    int i,j,l1, l2,temp,k;
    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;
    l1= num1.size();
    l2= num2.size();

    cout << l1 << "  " << l2 << endl;

    int sum[l2-1][l2];        //         5 6 7 8   ---> num1
                              //         1 2 3 4   ---> num2
    for(i=0; i<l1; i++)       //       ---------
        num1[i]-='0';          //       2 2 7 1 2    sum[3][4]---->sum[3][0]
                               //     1 7 0 3 4         i.e sum[3][0] should be 2
    for(i=0; i<l2; i++)        //   1 1 3 5 6
        num2[i]-='0';          //   5 6 7 8
                              //   -------------
    for(i=l2-1; i>=0; i--)    //   7 0 0 6 6 5 2
    {
        k=0;
        temp=0;
        for(j=l1-1; j>=0; j--)
        {
            temp+=(num2[i]*num1[j]);
            sum[i][k]= temp%10;
            temp=temp/10;
            k++;
        }
        sum[i][k]=temp;
        cout << sum[3][0] << endl;
    }

    for(i=l2-1; i>=0; i--)      // output is 2 2 7 1 1     Here value of sum[3][0] is 1 but the desired output is 2.
    {                           //           1 7 0 3 1
        for(k=l2; k>=0; k--)    //           1 1 3 5 0
            cout << sum[i][k];  //           0 5 6 7 8 
        cout << endl;
    }


    return 0;
}

I tried this code for the case num1=5678 and num2=1234. So sum[3][0] should be 2 in that case.

Ankit Gupta
  • 757
  • 7
  • 13

2 Answers2

1

You did not make sum large enough. You use sum[0..l2-1][0..l1] so the size would need to be sum[l2][l1+1].

When you exceed the second dim of sum that typically makes part of one row of sum share storage with part of another, so the place where you stored sum[2][0] is the same place you later stored sum[1][4]

When you exceed the first dim of sum (or the second dim in the last row) that makes sum share storage with other things, such as(but not necessarily) the other variables local to that function.

Also, your loop for displaying sum is incorrect. You use rows of sum from l2-1 down to 0 and columns from 0 up to L1. But you display columns l2 down to 0. The columns are computed based on l1, so should be displayed based on l1. That error would have symptoms if you tried an example with l1 not equal l2.

JSF
  • 5,281
  • 1
  • 13
  • 20
0

The sum array should be create like this:

int sum[l2][max(l1,l2)+1];

It is too small to store results of you calculations now.

Why program doesn't crash while you write out of bounds of the array? Because C++ has not any array bounds checking.

To be honest, you should declare the array by new and remove it by delete when it is not needed anymore. C++ standard doesn't provide to create non-constant size array without new. Only some compilers supports it as an extension and many of them prints warnings when you do that. More informations here: How do I declare a 2d array in C++ using new?

Community
  • 1
  • 1
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65