1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void add(int a[], int b[], int result[]);
void print(int a[]);
int main()
{
int op1[5] = {0,2,7,8,9};
int op2[5] = {0,5,1,3,4};
int result[5]= {0};
add(op1,op2,result);
//print(op1);
//print(op2);
print(result);
//for (int i = 0; i < 5; i++)
//cout << result[i] << " " << endl;
}
void add(int arr[], int b[], int result[]){
int carry = 0;
int sum = 0;
for (int i = 4; i >= 0; --i){
sum = arr[i] + b[i] + carry;
cout << "sum == " << sum << endl;
if (sum >= 10) {
sum = arr[i]/10;
carry = 1;
}
else {
carry = 0;
}
result[i] = sum + arr[i-1];
}
}
void print(int arr[])
{
for (int i = 4; i >= 0; --i)
cout << arr[i] << endl;
}
|