Advertisement
geniusvil

Methods-06

Dec 17th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _06.IndexOfElementBiggerNeighbors
  8. {
  9. class Program
  10. {
  11. /*Write a method that returns the index of the first
  12. * element in array that is bigger than its neighbors,
  13. * or -1, if thereโ€™s no such element. */
  14.  
  15.  
  16. static void CreatingArray(int[] arrayNum)
  17. {
  18. Random numbers = new Random();
  19. for (int i = 0; i < arrayNum.Length; i++)
  20. {
  21. arrayNum[i] = numbers.Next(1, 20);
  22. }
  23. }
  24.  
  25. // if index = 0
  26. static int PrintIndex(int[] array)
  27. {
  28. int index = -1;
  29. for (int i = 0; i < array.Length; i++)
  30. {
  31. if (i == 0 && array[i] > array[i + 1])
  32. {
  33. index = i;
  34. break;
  35. }
  36. else if ((i != 0 && i != array.Length - 1) && (array[i] > array[i + 1] && array[i] > array[i - 1]))
  37. {
  38. index = i;
  39. break;
  40. }
  41. else if (i == array.Length - 1 && array[i] > array[i - 1])
  42. {
  43. index = i;
  44. break;
  45. }
  46. }
  47. return index;
  48. }
  49.  
  50. static void PrintArray(int[] array)
  51. {
  52. Console.Write("The given array is: ");
  53. for (int i = 0; i < array.Length; i++)
  54. {
  55. Console.Write(array[i] + " ");
  56. }
  57. Console.WriteLine();
  58. }
  59.  
  60. static void Main()
  61. {
  62. Console.Write("Please enter length of array: ");
  63. int length = int.Parse(Console.ReadLine());
  64.  
  65. int[] arrayNum = new int[length];
  66. CreatingArray(arrayNum);
  67. PrintArray(arrayNum);
  68.  
  69. Console.Write("The index of first element bigger than its neighbors is ");
  70. Console.Write(PrintIndex(arrayNum));
  71. Console.WriteLine(".");
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement