//Write a method that returns the index of the first element in array that is larger than its neighbours, //or -1, if there’s no such element. //Use the method from the previous exercise. static void Main(string[] args) { Console.WriteLine("Enter size of array: "); int size = int.Parse(Console.ReadLine()); int[] myArr = new int[size]; for (int i = 0; i < myArr.Length; i++) { Console.Write("Array[{0}] = ", i); myArr[i] = int.Parse(Console.ReadLine()); } int index = -1; for (int i = 0; i < myArr.Length; i++) { if (LargerThanNeighbours.GreaterThanNeighbours(myArr, i)) { index = i; break; } } if (index == -1) { Console.WriteLine(index); } else { Console.WriteLine("The first index of element larger than his neighbours is {0}", index); } }
/* 6. First larger than neighbours Write a method that returns the index of the first element in array that is larger than its neighbours, or -1, if there’s no such element. Use the method from the previous exercise. */ static int FirstLager(int[] arr) { for(int i = 1; i < arr.Length -1; i++) { if (LargerThanNeighbours.LargerThanNeighboursMethod(arr, i)) return i; } return -1; }
static int FirstLargerNumber(params int[] arr) { for (int i = 1; i < arr.Length - 1; i++) { if (LargerThanNeighbours.LargerNumber(arr, i) == true) { return(i); } } return(-1); }
private static int IndexOfFirstElementLargerThanNeighbours(int[] seq) { for (int i = 0; i < seq.Length; i++) { if (LargerThanNeighbours.IsLargerThanNeighbors(seq, i)) // invoking the method from project 03. Larger-Than-Neighbours { return(i); } } return(-1); }
public static int FindFirstLargerThanNeighbours(int[] array) { for (int i = 0; i < array.Length; i++) { if (LargerThanNeighbours.IsLargerThanNeighbours(array, i)) { return(i); } } return(-1); }
static int IndexOfFirstElementLargerThanNeighbours(int[] seq) { for (int i = 0; i < seq.Length; i++) { if (LargerThanNeighbours.IsLargerThanNeighbours(seq, i)) { return(i); } } return(-1); }
private static void CheckFirstLargerThanNeighbours(int[] array) { for (int i = 0; i < array.Length; i++) { if ((i > 0) && (i < array.Length - 1) && (LargerThanNeighbours.IfLargerThanNeighbours(array, i))) { Console.Write("The index of the first element in array that is larger than its neighbours is: {0}", i); return; } } Console.WriteLine("-1 (There is no element that is larger than its neighbours.)"); }
private int CheckNeighbors(int[] inputArray) { var myLargerThanNeighbor = new LargerThanNeighbours(); bool result; for (int position = 0; position < inputArray.Length; position++) { result = myLargerThanNeighbor.CheckNeighbors(position, inputArray); if (result) { return(position); } } return(-1); }
private static int GetIndexOfFirstElementLargerThanNeighbours(int[] numbers) { int result = -1; for (int position = 0; position < numbers.Length; position++) { bool isBiggerThanNeighbours = LargerThanNeighbours.IsLargerThanNeighbours(numbers, position); if (isBiggerThanNeighbours) { result = position; return(result); } } return(result); }
public static void Main(string[] args) { var myClass = new LargerThanNeighbours(); var myClassHelper = new HelperClass(); Console.Write("Enter the number of elements in the array: "); int elementsCount = int.Parse(Console.ReadLine()); int[] array = myClassHelper.GenerateRandomArray <int>(elementsCount); Console.WriteLine("Random array: "); myClassHelper.PrintArray(array); Console.WriteLine(); Console.Write("Enter lookup position: "); int lookupPosition = int.Parse(Console.ReadLine()); bool bigger = myClass.CheckNeighbors(lookupPosition, array); if (bigger) { Console.WriteLine("Element at position {0} ({1}) is bigger than it's neighbors ({2} and {3})", lookupPosition, array[lookupPosition], array[lookupPosition - 1], array[lookupPosition + 1]); } else { switch (myClass.NeighborsCount(lookupPosition, array)) { case Neighbor.None: Console.WriteLine("This position is outside array!"); break; case Neighbor.One: Console.WriteLine("This element has only one neighbor!"); break; case Neighbor.Two: Console.WriteLine("Element at position {0} ({1}) is not bigger than it's neighbors ({2} and {3})", lookupPosition, array[lookupPosition], array[lookupPosition - 1], array[lookupPosition + 1]); break; default: break; } } }
private static void FirstLarger(int[] arr) { int count = 0; for (int i = 1; i < arr.Length - 1; i++) { if (LargerThanNeighbours.isLarger(arr, i)) { Console.WriteLine("\nThe index of the first element in array that is larger than its neighbours is: {0}", i); count++; return; } } if (count == 0) { Console.WriteLine("\nThe index of the first element in array that is larger than its neighbours is: {0}", -1); } }