FLISEN

Untitled

Jan 17th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2.  
  3. class ElementCheck
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("How long to be the array:");
  8.         int n = int.Parse(Console.ReadLine());
  9.         int[] array = new int[n];
  10.  
  11.         InputArray(n, array);
  12.  
  13.         Console.Write("Which index you want to check - from 0 to N: ");
  14.         int index = int.Parse(Console.ReadLine());
  15.  
  16.         CheckByIndex(n, array, index);
  17.  
  18.     }
  19.  
  20.     private static void InputArray(int n, int[] array)
  21.     {
  22.         Console.WriteLine("Enter the elements of the array:");
  23.         for (int i = 0; i < n; i++)
  24.         {
  25.             array[i] = int.Parse(Console.ReadLine());
  26.         }
  27.     }
  28.  
  29.     private static void CheckByIndex(int n, int[] array, int index)
  30.     {
  31.         if (index == 0 || index == n)
  32.         {
  33.             Console.WriteLine("Number {0} on index {1} has only one neighbor!", array[index], index);
  34.         }
  35.         else if (array[index] > array[index - 1] && array[index] > array[index + 1])
  36.         {
  37.             Console.WriteLine("Number {0} on index {1} is bigger than its two neighbors", array[index], index);
  38.         }
  39.         else if (array[index] < array[index - 1] || array[index] < array[index + 1])
  40.         {
  41.             Console.WriteLine("Number {0} on index {1} is smaller than at least one of its two neighbors", array[index], index);
  42.         }
  43.         else
  44.         {
  45.             Console.WriteLine("Number {0} on index {1} is equal to its two neighbors", array[index], index);
  46.         }
  47.     }  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment