Advertisement
stefanpu

Bigger than neighbours

Feb 14th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 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 BiggerThanNabours
  8. {
  9.     class BiggerThanNeighbors
  10.     {
  11.         public static int n;
  12.         public static int[] myArray = { 1, 2, 5, 2, 6, 7, 3, 2, 7, 8, 2 };
  13.         public static void Main(string[] args)
  14.         {
  15.             //Методът от предната задача - ExistsInArray;          
  16.             PrintArray();
  17.  
  18.             Console.Write("Please ENTER the index to check for: ");
  19.             int n = int.Parse(Console.ReadLine());
  20.             //int[] myArray = { 1, 2, 5, 2, 6, 7, 3, 2, 7, 8, 2 };            
  21.  
  22.             Console.WriteLine("Is bigger than neighbors? {0}", GreaterNeighbors(myArray, n));
  23.         }
  24.  
  25.         public static bool GreaterNeighbors(int[] arrayToCheck, int position)
  26.         {
  27.             bool inIndex = (position > 0)&&(position < arrayToCheck.Length - 1);
  28.  
  29.             //Първо проверяваш дали индексът е вътрешен
  30.             if (inIndex)
  31.             {
  32.                 //После инициализираш тази променлива.
  33.                 bool neighborsBigger = (arrayToCheck[position] > arrayToCheck[position - 1]) &&
  34.                     (arrayToCheck[position] > arrayToCheck[position + 1]);
  35.  
  36.                 if (neighborsBigger == true)
  37.                 {
  38.                     return true;
  39.                 }
  40.                 else
  41.                 {
  42.                     return false;
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 return false;
  48.             }
  49.            
  50.            
  51.         }
  52.  
  53.         private static void PrintArray()
  54.         {
  55.             for (int index = 0; index < myArray.Length; index++)
  56.             {
  57.                 Console.Write(myArray[index] + " ");
  58.             }
  59.             Console.WriteLine();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement