Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Xml.Schema;
  4.  
  5. namespace ConsoleApp1
  6. {
  7. public class Question3
  8. {
  9. /*
  10. A function that build an array y according to maximum between two cells.
  11. Input: arrX - the array we wish to rebuild into arrayY.
  12. Output: arrY.
  13. */
  14.  
  15. public static int[] BuildArrY(int[] arrX)
  16. {
  17. int[] arrY = new int[arrX.Length];
  18. int i = 0, totalMax = arrX[0], currentMax = 0;
  19.  
  20. for (i = 0; i < arrX.Length - 1; i++)
  21. {
  22. if (arrX[i] > totalMax)
  23. {
  24. totalMax = arrX[i];
  25. }
  26.  
  27. if (arrX[i + 1] > totalMax) // Won't miss the last index.
  28. {
  29. totalMax = arrX[i + 1];
  30. }
  31.  
  32. currentMax = (int) Math.Max(arrX[i], arrX[i + 1]);
  33. arrY[i] = currentMax;
  34. }
  35.  
  36. arrY[arrY.Length - 1] = totalMax;
  37.  
  38. return arrY;
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement