1

I am trying to multiply two numbers kept in two 1d arrays. After I kept them in a 2d array(like we do in standard multiplication), the output isn't what I am expecting. Here's how far I have done so far

int main()
{
    int n, m, i, j;
    printf("Enter multiplicand(n) size: ");
    scanf("%d", &n);
    printf("Enter multiplier(m) size: ");
    scanf("%d", &m);
    int a[n], b[m];
    printf("Enter multiplicands: ");
    for(i = 0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("Enter multipliers: ");
    for(i = 0; i<m; i++)
    {
        scanf("%d", &b[i]);
    }

    int c[m][n+m];
    int k = 0 , l = m+n-1 , p = 2;
    int ans = 0, carry = 0;
    for(i = 0; i<m; i++)
    {
        for(j = 0; j<m+n; j++)
        {
            c[i][j] = 0;
        }
    }
    for(i = m-1; i>=0; i--)
    {
        for(j = n-1; j>=0; j--)
        {
            if(a[j]*b[i] < 10)
            {
                c[k][l] = a[j]*b[i];
                l--;
            }
            else if(carry > 0 && a[j]*b[i] < 10)
            {
                ans = a[j]*b[i] + carry;
                c[k][l] = ans;
                carry--;
                l--;
            }
            else if(carry > 0 && a[j]*b[i] > 10)
            {
                ans = a[j]*b[i]%10 + carry;
                c[k][l] = ans;
                l--;
            }
            else if(carry > 0 && a[j]*b[i] == 10)
            {
                ans = (a[j]*b[i])%10 + carry;
                c[k][l] = ans;
                carry++;
                l--;
            }
            else
            {
                ans = a[j]*b[i]%10;
                c[k][l] = ans;
                carry++;
                l--;
            }
        }
        l = m+n-p;
        p++;
        k++;
        carry = 0;
        ans = 0;
    }
    for(i = 0; i<m; i++)
    {
        for(j = 0; j<m+n; j++)
        {
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }
}

If I multiply 594 with 232 I should get:

0 0 1 1 8 8

0 1 7 8 2 0

1 1 8 8 0 0

but the program is returning:

0 0 0 1 8 8

0 0 6 8 2 0

0 1 8 8 0 0

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Rock
  • 13
  • 3
  • Looks like `carry` is carrying over more than needed. Maybe you need to reset `carry` (to `0`?) (*I didn't study your code attentively*). – pmg May 11 '20 at 15:27
  • 1
    You may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Andreas Wenzel May 11 '20 at 15:28
  • 1
    You may want to read this: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 11 '20 at 15:31
  • not your answer but, you can intialize your array `int c[m][n+m]={ 0 };` like this, instead of for loop – Muhammedogz May 11 '20 at 15:32
  • 1
    @muhammed VLAs cannot be initialized, OP could do `memset(c, 0, sizeof c);` which is likely what the `for` loops translate to. – pmg May 11 '20 at 15:32

1 Answers1

1

You were not handling the case when carry exist but the internal loop finishes using this

    if(carry>0){
        c[k][l] = carry;
    }

at the end and with small modification on your code I've only tested it with 3 digits nums, this may help.

for(i = m-1; i>=0; i--)
    {
        for(j = n-1; j>=0; j--)
        {
            if(a[j]*b[i] < 10 && carry==0)
            {
                c[k][l] = a[j]*b[i];
                l--;
            }
            else if(carry > 0)
            {
                ans = a[j]*b[i] + carry;
                c[k][l] = ans%10;
                carry=ans/10;
                l--;
            }
            else
            {
                ans = a[j]*b[i]%10;
                c[k][l] = ans;
                carry=a[j]*b[i]/10;
                l--;
            }
        }
        if(carry>0){
            c[k][l] = carry;
        }
        l = m+n-p;
        p++;
        k++;
        carry = 0;
        ans = 0;
    }
sachco
  • 274
  • 2
  • 9