Advertisement
tvarbanov

CSharp2-03-05

Jan 4th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System;
  2.  
  3. class BiggerThanNeighborsMethod
  4. {
  5.     //Write a method that checks if the element at given position in given array of integers is bigger than its two neighbors (when such exist).
  6.  
  7.     static bool CheckNeighbors(int index,int[] array)
  8.     {
  9.         bool isBigger = false;
  10.         if (array[index] == 0) //If checks first element
  11.         {
  12.             return isBigger;
  13.         }
  14.         else if (array[index] == array.Length-1) //If checks last element
  15.         {
  16.             return isBigger;
  17.         }
  18.         else if (array[index] > array[index + 1] && array[index] > array[index - 1]) //Checks the two neighbors
  19.         {
  20.             isBigger = true;
  21.         }
  22.         return isBigger;
  23.     }
  24.  
  25.     static void Main()
  26.     {
  27.         //Get the index
  28.         Console.WriteLine("Enter the index you want to check: ");
  29.         int indexOfNum = int.Parse(Console.ReadLine());
  30.         //Predefined array
  31.         int[] arrayOfNums = { 1, 5, 4, 9, 5, 3, 1, 8, 6, 4, 6, 9, 2, 8, 3, 4, 9 };
  32.         Console.WriteLine("The number at position {0} is {1}.",indexOfNum,arrayOfNums[indexOfNum]);
  33.         Console.WriteLine("The array : "+string.Join(", ",arrayOfNums));
  34.         Console.WriteLine("Bigger than the two neighbours : "+CheckNeighbors(indexOfNum,arrayOfNums));
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement