Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. const int SIZE_FIRST_ARR = 4;
  5. const int SIZE_SECOND_ARR = 2;
  6. using namespace std;
  7.  
  8. int* addOneArrayToAnother (int arr1[], int arr2[], int size1,int size2)
  9. {
  10. int* newArr;
  11.  
  12. if(SIZE_FIRST_ARR > SIZE_SECOND_ARR)
  13. {
  14. newArr = new int[1 + SIZE_FIRST_ARR];
  15.  
  16. int carry = 0;
  17. 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--)
  18. {
  19.  
  20. //if((arr1[i] + arr2[j]) >= 10)
  21. //{
  22. newArr[k] = (arr1[i] + arr2[j] + carry) % 10;
  23. carry = (arr1[i] + arr2[j]) / 10;
  24.  
  25. if( arr1[i] >= 10)
  26. {
  27. newArr[k] = (arr1[i] + arr2[i] + carry) % 10;
  28. carry = (arr1[i] + arr2[i]) / 10;
  29. }
  30.  
  31.  
  32. //arr1[i - 1] += carry;
  33. //if(arr1[i - 1] >= 10)
  34. //{
  35. // k--;
  36. // newArr[k] = (arr1[i - 1] + arr2[j - 1]) % 10;
  37. // carry = (arr1[i - 1] + arr2[j - 1]) / 10;
  38. // arr1[i - 1] = arr1[i - 1] + carry;
  39. //}
  40. //k--;
  41. //}
  42. //else if((arr1[i] + arr2[j]) < 10)
  43. //{
  44. //newArr[k] = (arr1[i] + arr2[i]);
  45. //}
  46. // k--;
  47. }
  48. }
  49. else if(SIZE_FIRST_ARR < SIZE_SECOND_ARR)
  50. {
  51. newArr = new int[1 + SIZE_SECOND_ARR];
  52. size2 = 1 + SIZE_SECOND_ARR;
  53. }
  54.  
  55. return newArr;
  56.  
  57. }
  58.  
  59. void main()
  60. {
  61. int arr1[SIZE_FIRST_ARR] = {9,9,9,9};
  62. int arr2[SIZE_SECOND_ARR] = {9,7};
  63. int* newArr;
  64. int size1 = SIZE_FIRST_ARR;
  65. int size2 = SIZE_SECOND_ARR;
  66. int newSize;
  67.  
  68. newArr = addOneArrayToAnother( arr1, arr2, size1,size2);
  69.  
  70. for(int i = 0; i < SIZE_FIRST_ARR + 1;i++)
  71. {
  72. cout << newArr[i] <<" ";
  73. }
  74.  
  75. cout << '\n';
  76. system("pause");
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement