Search code examples
c++arraysmultiplication

Multiplication by Individual Place Value in C++


I have two non-negative numbers represented as arrays of digits that could be of different lengths and I want to multiply them without converting to string using stoll because that limits the input size.

I can get it to work for cases like 900 * 9, but not 11 * 11.

This is what I have so far:

  #include <iostream>
  int main() {

    int a[100], b[100], product[200]; // required to use
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
    }

    int temp2 = 0;
    for (int i = 0; i < n; i++) {
      int temp1 = temp2*10; // i think it might be this line that doesn't always work but i don't know what I should change it to
      for (int j = 0; j < m; j++) {
        temp2 = a[i] * b[j] + temp1;
        cout << temp2 << endl;
      }
    }
  }

Solution

  • I fixed your code in the solution below. The solution is stored in the product array and then moved to a vector<int> called solution with size equal to the exact number of digits as the final answer and is printed to console after the program finishes:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
    
        int a[100], b[100], product[200]; // required to use
        for (int i = 0; i < 200; ++i) {
            product[i] = 0;
        }
        int n, m;
        cin >> n >> m;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> b[i];
        }
    
        int lineDigit = 0;
        for (int i = n - 1; i >= 0; i--) {
            int lineSum = 0;
            for (int j = m - 1; j >= 0; j--) {
                int digit = (m - j) + 1;
    
                int digitProd = a[i] * b[j];
                int tens = digitProd / 10;
                int ones = digitProd % 10;
                product[digit + lineDigit] += ones;
                product[digit + lineDigit + 1] += tens;
    
                product[digit + lineDigit + 1] += product[digit + lineDigit] / 10;
                product[digit + lineDigit] %= 10;
            }
    
            ++lineDigit;
        }
    
        bool foundNonZero = false;
        vector<int> solution;
        for (int i = 199; i >= 2; --i) {
            if (!foundNonZero) {
                if (product[i] != 0) {
                    foundNonZero = true;
                }
                else {
                    continue;
                }
            }
            solution.push_back(product[i]);
        }
    
        for (int i = 0; i < solution.size(); ++i) {
            cout << solution[i] << ' ';
        }
        cout << endl;
    
        system("pause");
    }
    

    There were a couple of simple arithmetic errors in your code. It won't be very elucidating to go over it; just keep thinking about how multiplication is done by hand and try to get the logic right.