fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. try
  8. {
  9. int[] array = new int[] { 1, 5, 3, 4, 2 };
  10.  
  11. //Reverse copy of Array
  12. var newarr = arrayReverseNewArray(array);
  13.  
  14. //Display New Array Elements
  15. foreach (int x in newarr)
  16. Console.Write(x + ",");
  17.  
  18. Console.WriteLine("");
  19.  
  20. //Reverse original Array
  21. arrayReverseinPlace(array);
  22.  
  23. //Display original Array Elements
  24. foreach (int x in array)
  25. Console.Write(x + ",");
  26.  
  27. }
  28. catch (Exception ex)
  29. {
  30. Console.Write( ex.Message);
  31. }
  32. }
  33. private static void arrayReverseinPlace(int[] array)
  34. {
  35. for (int i = 0; i < array.Length / 2; i++)
  36. {
  37. int tempvar = array[i];
  38. array[i] = array[array.Length - i - 1];
  39. array[array.Length - i - 1] = tempvar;
  40. }
  41. }
  42.  
  43. private static int[] arrayReverseNewArray(int[] array)
  44. {
  45. int[] arr = new int[array.Length];
  46. int j = 0;
  47. for (int i = array.Length - 1; i >=0 ; i--)
  48. {
  49. arr[j] = array[i];
  50. j++;
  51. }
  52. return arr;
  53. }
  54. }
Success #stdin #stdout 0.01s 131136KB
stdin
Standard input is empty
stdout
2,4,3,5,1,
2,4,3,5,1,