Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BiggerThanNabours
- {
- class BiggerThanNeighbors
- {
- public static int n;
- public static int[] myArray = { 1, 2, 5, 2, 6, 7, 3, 2, 7, 8, 2 };
- public static void Main(string[] args)
- {
- //Методът от предната задача - ExistsInArray;
- PrintArray();
- Console.Write("Please ENTER the index to check for: ");
- int n = int.Parse(Console.ReadLine());
- //int[] myArray = { 1, 2, 5, 2, 6, 7, 3, 2, 7, 8, 2 };
- Console.WriteLine("Is bigger than neighbors? {0}", GreaterNeighbors(myArray, n));
- }
- public static bool GreaterNeighbors(int[] arrayToCheck, int position)
- {
- bool inIndex = (position > 0)&&(position < arrayToCheck.Length - 1);
- //Първо проверяваш дали индексът е вътрешен
- if (inIndex)
- {
- //После инициализираш тази променлива.
- bool neighborsBigger = (arrayToCheck[position] > arrayToCheck[position - 1]) &&
- (arrayToCheck[position] > arrayToCheck[position + 1]);
- if (neighborsBigger == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- private static void PrintArray()
- {
- for (int index = 0; index < myArray.Length; index++)
- {
- Console.Write(myArray[index] + " ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement