Write a method that checks if the element at given position in given array of integers is bigger than its two neighbors (when such exist).

Write a method that checks if the element at given position in given array of  integers is bigger than its two neighbors (when such exist).



using System;

class CheckElement
{
    static void Check(int[] array, int position)
    {
        if (array.Length == 1)
        {
            Console.WriteLine("The element has no neighbors");
        }
        else
        {
            if (position == 0)
            {
                Console.WriteLine("The comparable numbers are {0} and {1}", array[position], array[position + 1]);
                Console.WriteLine("The bigger number is: " + Math.Max(array[position], array[position + 1]));
            }
            else if (position == array.Length - 1)
            {
                Console.WriteLine("The comparable numbers are {0} and {1}", array[position], array[position - 1]);
                Console.WriteLine("The bigger number is: " + Math.Max(array[position], array[position - 1]));
            }
            else if ((position > 0) && (position < array.Length - 1))
            {
                Console.WriteLine("The comparable numbers are {0}, {1} and {2}", array[position], array[position + 1], array[position - 1]);
                Console.WriteLine("The bigger number is: " + Math.Max(Math.Max(array[position], array[position + 1]), array[position - 1]));
            }
            else
            {
                Console.WriteLine("There is no such number in this position. Try again... ");
                Main();
            }
        }
    }

    static void Main()
    {
        Console.Write("Enter array length: ");
        int arrayLength = int.Parse(Console.ReadLine());

        int[] array = new int[arrayLength];

        Console.WriteLine("Enter array elements ...");
        for (int i = 0; i < arrayLength; i++)
        {
            Console.Write("Position [{0}] -> ", i);
            array[i] = int.Parse(Console.ReadLine());
        }

        Console.Write("Enter element position you want to check: ");
        int position = int.Parse(Console.ReadLine());

        Check(array, position);
    }
}

1 Comments

  1. Play Free Slots at JTM Casino UK - Casino & Slot Machines
    Play the 창원 출장마사지 latest 전라남도 출장안마 slots and games 경기도 출장마사지 at JTM Casino 부천 출장마사지 UK including Jumbo 거제 출장안마 Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo Jumbo

    ReplyDelete
Previous Post Next Post