Guest User

Untitled

a guest
Dec 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. int[] arr1 = {1, 2, 3, 4, 5}; // {1, 2, 3, 4, 5}
  2. int[] arr2 = {1, 2}; // {1, 2}
  3. int[] sumArr12 = arr1.Select((value, index) => value + arr2[index % arr2.Length]).ToArray(); // {2, 4, 4, 6, 6}
  4.  
  5. int[] arrOne = { 1, 2, 3, 4, 5 };
  6. int[] arrTwo = { 1, 2 };
  7.  
  8. int[] result = new int [arrOne.Length > arrTwo.Length ? arrOne.Length : arrTwo.Length];
  9. var firstIndex = 0;
  10. var secondIndex = 0;
  11.  
  12. for(int index = 0; index < result.Length; index++)
  13. {
  14. firstIndex = arrOne.Length > firstIndex ? firstIndex : 0;
  15. secondIndex = arrTwo.Length > secondIndex ? secondIndex : 0;
  16. result[index] = arrOne[firstIndex++] + arrTwo[secondIndex++];
  17. }
  18.  
  19. //result = {2, 4, 4, 6, 6}
Add Comment
Please, Sign In to add comment