Untitled
unknown
plain_text
a year ago
1.0 kB
3
Indexable
#include <iostream> #include <vector> using namespace std; vector<int> multwoint(vector<int>& n1, vector<int>& n2) { int m = n1.size(); int n = n2.size(); vector<int> result(m + n, 0); for (int i = m - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { int mul = n1[i] * n2[j]; int sum = result[i + j + 1] + mul; result[i + j + 1] = sum % 10; // Store the last digit result[i + j] += sum / 10; // Carry over to the next digit } } return result; } int main() { int n, m; cout << "Enter size of array1: "; cin >> n; cout << "Enter size of array2: "; cin >> m; vector<int> v1(n); vector<int> v2(m); cout << "Enter elements of array1: "; for (int i = 0; i < n; i++) { cin >> v1[i]; } cout << "Enter elements of array2: "; for (int j = 0; j < m; j++) { cin >> v2[j]; } vector<int> result = multwoint(v1, v2); cout << "Answer: "; for (int digit : result) { cout << digit; } cout << endl; return 0; }
Editor is loading...
Leave a Comment