Advertisement
stanevplamen

02.03.06.FindFirstBiggerNeighbor

Jul 15th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using BiggerNeighbor;
  3.  
  4. class FindFirstBiggerNeighbor
  5. {
  6.     public static int sign = 0;
  7.  
  8.     public static int ValidPosition(int[] elements, int position)
  9.     {
  10.         if (position + 1 < elements.Length && position - 1 >= 0)
  11.         {
  12.             return 1;
  13.         }
  14.         else if (position + 1 == elements.Length)
  15.         {
  16.             return 2;
  17.         }
  18.         else
  19.         {
  20.             return 3;
  21.         }
  22.     }
  23.  
  24.     public static bool BiggerCheck(int[] elements, int position)
  25.     {
  26.         sign = ValidPosition(elements, position);
  27.         if (sign == 1 && elements[position] > elements[position - 1] && elements[position] > elements[position + 1])
  28.         {
  29.             return true;
  30.         }
  31.         else if (sign == 2 && elements[position] > elements[position - 1])
  32.         {
  33.             return true;
  34.         }
  35.         else if (sign == 3 && elements[position] > elements[position + 1])
  36.         {
  37.             return true;
  38.         }
  39.         else
  40.         {
  41.             return false;
  42.         }
  43.     }
  44.  
  45.     static void Main()
  46.     {
  47.         int[] array = { 10, 2, 3, 4, 5, 6, 7, 8, 6 };
  48.  
  49.         for (int i = 0; i < array.Length; i++)
  50.         {
  51.             int index = i;
  52.  
  53.             bool checkBool = BiggerCheck(array, index);
  54.             if (sign == 1 && checkBool == true)
  55.             {
  56.                 Console.WriteLine("{0} is bigger than {1} and {2}", array[index], array[index - 1], array[index + 1]);
  57.                 Environment.Exit(0);
  58.             }
  59.             else if (sign == 2 && checkBool == true)
  60.             {
  61.                 Console.WriteLine("{0} is bigger than {1} and (empty)", array[index], array[index - 1]);
  62.                 Environment.Exit(0);
  63.             }
  64.             else if (sign == 3 && checkBool == true)
  65.             {
  66.                 Console.WriteLine("{0} is bigger than (empty) and {1}", array[index], array[index + 1]);
  67.                 Environment.Exit(0);          
  68.             }
  69.         }
  70.         Console.WriteLine("-1, no such element");
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement