Guest User

Untitled

a guest
Dec 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 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}
  20.  
  21. int[] arrOne = { 1, 2, 3 };
  22. int[] arrTwo = { 1, 2, 3, 4, 5, 6 };
  23.  
  24. static IEnumerable<T> Looped<T>(T[] array)
  25. {
  26. for (int i = 0; ; i = (i + 1) % array.Length)
  27. yield return array[i];
  28. }
  29.  
  30. int[] ar1 = { 1, 2, 3, 4, 5 };
  31. int[] ar2 = { 1, 2 };
  32. var ar3 = ar1.Zip(Looped(ar2), (x, y) => x + y).ToArray();
Add Comment
Please, Sign In to add comment