Advertisement
callumbinner22

pg 347 Task 6

Dec 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 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 ConsoleApplication6
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] myarray = { 1, 5, 8, 7, 8, 9, 1, 1, 7 };
  14.             Check(myarray);
  15.  
  16.         }
  17.         static void Check(int[] myarray)
  18.         {
  19.             int firstIndex = 0;
  20.             for (int index = 1; index < myarray.Length - 1; index++)
  21.             {
  22.                 int first = myarray[index];
  23.                 if ((first > myarray[index + 1]) && (first > myarray[index - 1]))
  24.                 {
  25.                     firstIndex = index;
  26.                     break;
  27.                 }
  28.                 else
  29.                 {
  30.                     firstIndex = -1;
  31.                 }
  32.             }
  33.             Console.WriteLine("The location of the first integer that is greater than both its neighbours to the left and right respectively is ... " + firstIndex + "(The array starts at zero)");
  34.         }
  35.     }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement