Advertisement
Teodor92

06. MethodsNeigbors

Jan 15th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1. /* Write a method that returns the index
  2.  * of the first element in array that is
  3.  * bigger than its neighbors, or -1, if
  4.  * thereโ€™s no such element.
  5.  */
  6.  
  7. using System;
  8.  
  9. class FirstBiggerThenNeighbors
  10. {
  11.     static int BiggerNeighborFinder(int[] array)
  12.     {
  13.         int index = 0;
  14.         for (int i = 1; i < array.Length - 1; i++)
  15.         {
  16.             if ((array[i] > array[i + 1]) && (array[i] > array[i - 1]))
  17.             {
  18.                 index = i;
  19.                 break;
  20.             }
  21.             else
  22.             {
  23.                 index = -1;
  24.             }
  25.         }
  26.         return index;
  27.        
  28.     }
  29.     static void Main()
  30.     {
  31.         int[] myArray = { 1,2,3,4,5,4,7,8 };
  32.         Console.WriteLine(BiggerNeighborFinder(myArray));
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement