stkirov

06.FirstElementBiggerThanNeighbours

Jan 16th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. //Write a method that returns the index of the first element in array that is bigger than its neighbors, or -1, if there’s no such element.
  2. //Use the method from the previous exercise.
  3. using System;
  4. class FirstElementBiggerThanNeighbours
  5. {
  6.     //I've added reference to exercise 5, in order fo this class to know about the methods in exercise 5
  7.     static void Main()
  8.     {
  9.         int[] array = { 1, 2, 3, 4, 5, 5, 6, 7 };
  10.         bool numberIsFound = false;
  11.  
  12.         for (int i = 0; i < array.Length; i++)
  13.         {
  14.             bool checkNumber = ElementBiggerThanNeighboursInArray.IsBiggerThanNeighbours(array, i);
  15.             if (checkNumber)
  16.             {
  17.                 //Print the array and color the position which we're currently on
  18.                 for (int j = 0; j < array.Length; j++)
  19.                 {
  20.                     if (j == i)//make the number that we search green
  21.                     {
  22.                         Console.ForegroundColor = ConsoleColor.Green;
  23.                     }
  24.                     Console.Write("{0} ", array[j]);
  25.                     Console.ResetColor();
  26.                 }
  27.                 Console.WriteLine();
  28.                 Console.WriteLine("The index is {0}", i);
  29.                 numberIsFound = true;
  30.                 break;
  31.             }
  32.         }
  33.         if (numberIsFound==false)
  34.         {
  35.             Console.WriteLine(-1);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment