Advertisement
stanevplamen

02.03.05.NeighborCheck

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