Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- const int SIZE_FIRST_ARR = 4;
- const int SIZE_SECOND_ARR = 2;
- using namespace std;
- int* addOneArrayToAnother (int arr1[], int arr2[], int size1,int size2)
- {
- int* newArr;
- if(SIZE_FIRST_ARR > SIZE_SECOND_ARR)
- {
- newArr = new int[1 + SIZE_FIRST_ARR];
- int carry = 0;
- for(int i = SIZE_FIRST_ARR - 1 , j = SIZE_SECOND_ARR - 1 , k = SIZE_FIRST_ARR ; j >= 0 || i >= 0 || k >= 0 ;i--, j-- ,k--)
- {
- //if((arr1[i] + arr2[j]) >= 10)
- //{
- newArr[k] = (arr1[i] + arr2[j] + carry) % 10;
- carry = (arr1[i] + arr2[j]) / 10;
- if( arr1[i] >= 10)
- {
- newArr[k] = (arr1[i] + arr2[i] + carry) % 10;
- carry = (arr1[i] + arr2[i]) / 10;
- }
- //arr1[i - 1] += carry;
- //if(arr1[i - 1] >= 10)
- //{
- // k--;
- // newArr[k] = (arr1[i - 1] + arr2[j - 1]) % 10;
- // carry = (arr1[i - 1] + arr2[j - 1]) / 10;
- // arr1[i - 1] = arr1[i - 1] + carry;
- //}
- //k--;
- //}
- //else if((arr1[i] + arr2[j]) < 10)
- //{
- //newArr[k] = (arr1[i] + arr2[i]);
- //}
- // k--;
- }
- }
- else if(SIZE_FIRST_ARR < SIZE_SECOND_ARR)
- {
- newArr = new int[1 + SIZE_SECOND_ARR];
- size2 = 1 + SIZE_SECOND_ARR;
- }
- return newArr;
- }
- void main()
- {
- int arr1[SIZE_FIRST_ARR] = {9,9,9,9};
- int arr2[SIZE_SECOND_ARR] = {9,7};
- int* newArr;
- int size1 = SIZE_FIRST_ARR;
- int size2 = SIZE_SECOND_ARR;
- int newSize;
- newArr = addOneArrayToAnother( arr1, arr2, size1,size2);
- for(int i = 0; i < SIZE_FIRST_ARR + 1;i++)
- {
- cout << newArr[i] <<" ";
- }
- cout << '\n';
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement