ellapt

T9.6.FirstBiggerThanNeighbors

Jan 19th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. using System;
  2.  
  3. class FirstBiggerThanNeighbors
  4. {
  5. static int FrstBigNeighb(int[] arr)
  6. {
  7. int result=-1;
  8. for (int i = 1; i < arr.Length-1; i++)
  9. {
  10. if ((arr[i - 1] < arr[i]) && (arr[i] > arr[i + 1]))
  11. {
  12. result = i;
  13. return result;
  14. }
  15. }
  16. return result;
  17. }
  18.  
  19. static void Main()
  20. {
  21. Console.WriteLine("Index of the first element in array that is bigger than its neighbors");
  22. int n;
  23. int indexPos;
  24.  
  25. Console.Write("Enter array length n > 2: ");
  26. n = int.Parse(Console.ReadLine());
  27.  
  28. Console.WriteLine("Enter array elements:");
  29. int[] intArray = new int[n];
  30. for (int i = 0; i < n; i++)
  31. {
  32. intArray[i] = int.Parse(Console.ReadLine());
  33. }
  34. indexPos = FrstBigNeighb(intArray);
  35. if (indexPos == -1)
  36. {
  37. Console.WriteLine("There are no elements bigger than their nejghbors.");
  38. }
  39. else
  40. {
  41. Console.WriteLine("Position of the first element that is bigger than its neighbors is: {0}", indexPos);
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment