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).

SHARE:

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);
    }
}

COMMENTS

Name

ADOBE ILLUSTRATOR,5,Advantages of it,14,Android,3,Android App,1,Annoucements,5,Apple,27,Apple TV 2,1,Apps,3,AutoDraw,1,Basic It,23,best rct game,4,best rct version,2,best roller coaster tycoon game,2,Black Desert Tamer,1,Blogger,1,c# tutorials,25,Cloud Computing,1,Color Schemes,1,console,1,Crowdfunding,2,Devices,1,Dock,2,elcapitan,1,Enterprise,1,Excel,1,FaceTime,1,Far Cry Primal,1,Farmville 2,5,Featured,2,Games,13,games for ios,1,Glossary,2,Google,1,google docs,1,google sheets,1,Html,3,iDevice,5,ios,1,iOS Apps,5,iOS Tweaks,1,iPhone 5,3,iPhone 5S,2,It Tricks,10,jailbreak rct,1,java,1,javascript,1,Just Cause 2,1,leeter hero,1,Mac OS X,8,macos,1,macOS,3,malwares,1,marriage in stardew valley,1,marriage stardew valley,1,Mavericks,1,Microsoft,1,Mount and Warblade,1,New Technology,6,optical character recognition,1,osx,1,php,1,Pillars of Eternity,1,PS4,1,RCT,5,RCT 2,1,RCT 3,3,rct 3 cheat,1,rct 4 hack,1,rct3,1,rct3 cheats,1,rct4,3,rct4 hack,1,RCT4 Mobile,4,rct4 redeem codes 2016,1,rct4 redeem codes 2017,1,roller coaster 3 cheats,1,rollercoaster tycoon 3,1,rollercoaster tycoon 3 money,1,rollercoaster tycoon 3 money cheat,1,Security,1,sierra,1,Sony,1,Sql,1,Stardew Valley,3,Stardew Valley Marriage,1,Store,1,Surface,1,Tamil,2,Tech,1,Ui Design,1,unflod malware,1,unlimited coins rct 4,1,Updates,1,Webdesign,5,Word,9,Wordpress,3,yosemite,1,Youtube,1,
ltr
item
Lankaitschool | Lanka itschool: 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).
https://2.bp.blogspot.com/-7O1v4hh9g1U/Wv2h_iY4KCI/AAAAAAAAE64/OZdpevi1i0YGPkXgV_l0PUvLXqxJpwFnwCK4BGAYYCw/s640/maxresdefault.jpg
https://2.bp.blogspot.com/-7O1v4hh9g1U/Wv2h_iY4KCI/AAAAAAAAE64/OZdpevi1i0YGPkXgV_l0PUvLXqxJpwFnwCK4BGAYYCw/s72-c/maxresdefault.jpg
Lankaitschool | Lanka itschool
http://www.lankaitschool.com/2018/05/write-method-that-checks-if-element-at.html
http://www.lankaitschool.com/
http://www.lankaitschool.com/
http://www.lankaitschool.com/2018/05/write-method-that-checks-if-element-at.html
true
7411958204546789358
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy