#include <iostream>
#include <vector>
using namespace std;
void addArrays(int a[], int aSize, int b[], int bSize, int sum[], int &sumSize) {
int carry = 0;
int i = 1, j = 1, k = 1;
// Loop through both arrays from least significant to most significant digit
while (i <= aSize || j <= bSize || carry) {
int digitA = (i <= aSize) ? a[i] : 0;
int digitB = (j <= bSize) ? b[j] : 0;
int total = digitA + digitB + carry;
carry = total / 10;
sum[k++] = total % 10;
i++;
j++;
}
sumSize = k - 1; // Size of the resulting sum array
}
int main() {
int v[100], x[100], rez[200];
int n, m, rezSize;
cout << "Enter size and elements for the first array:" << endl;
cin >> n;
for (int i = n; i >= 1; --i) {
cin >> v[i];
}
v[0] = n; // Store the size at the beginning of the array
cout << "Enter size and elements for the second array:" << endl;
cin >> m;
for (int j = m; j >= 1; --j) {
cin >> x[j];
}
x[0] = m; // Store the size at the beginning of the array
// Call the addArrays function
addArrays(v, v[0], x, x[0], rez, rezSize);
// Print the result
cout << "sum[] = {";
for (int i = rezSize; i >= 1; --i) {
cout << rez[i];
if (i > 1) {
cout << ", ";
}
}
cout << "}" << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBhZGRBcnJheXMoaW50IGFbXSwgaW50IGFTaXplLCBpbnQgYltdLCBpbnQgYlNpemUsIGludCBzdW1bXSwgaW50ICZzdW1TaXplKSB7CiAgICBpbnQgY2FycnkgPSAwOwogICAgaW50IGkgPSAxLCBqID0gMSwgayA9IDE7CgogICAgLy8gTG9vcCB0aHJvdWdoIGJvdGggYXJyYXlzIGZyb20gbGVhc3Qgc2lnbmlmaWNhbnQgdG8gbW9zdCBzaWduaWZpY2FudCBkaWdpdAogICAgd2hpbGUgKGkgPD0gYVNpemUgfHwgaiA8PSBiU2l6ZSB8fCBjYXJyeSkgewogICAgICAgIGludCBkaWdpdEEgPSAoaSA8PSBhU2l6ZSkgPyBhW2ldIDogMDsKICAgICAgICBpb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1bHQKICAgIGNvdXQgPDwgInN1bVtdID0geyI7CiAgICBmb3IgKGludCBpID0gcmV6U2l6ZTsgaSA+PSAxOyAtLWkpIHsKICAgICAgICBjb3V0IDw8IHJleltpXTsKICAgICAgICBpZiAoaSA+IDEpIHsKICAgICAgICAgICAgY291dCA8PCAiLCAiOwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgIn0iIDw8IGVuZGw7CiAgICAKICAgIHJldHVybiAwOwp9
Enter size and elements for the first array:
Enter size and elements for the second array:
sum[] = {-1, -6, -7, -8, -2, -7, -1, -7, -6, -9, 4}