Editorial Staff - - C Sharp Tutorial
If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# program to search an element with Array indices with the output.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, I have shared the output of below program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System; class ArrayBinarySearch { public static void Main() { int[] ints = { 0, 10, 15, 25, 100 }; Console.WriteLine("Array indices and elements: "); for (int i = 0; i < ints.Length; i++) { Console.Write("[{0}]={1, -5}", i, ints[i]); } Console.WriteLine(); FindObject(ints, 25); FindObject(ints, 10); FindObject(ints, 2000); Console.ReadLine(); } public static void FindObject(Array array, Object o) { int index = Array.BinarySearch(array, 0, array.Length, o); Console.WriteLine(); if (index > 0) { Console.WriteLine("Object: {0} found at [{1}]", o, index); } else if (~index == array.Length) { Console.WriteLine("Object: {0} not found. " + "No array object has a greater value.", o); Console.WriteLine(); } else { Console.WriteLine("Object: {0} not found. " + "Next larger object found at [{1}].", o, ~index); } } } |
Array indices and elements:
[0]=0 [1]=10 [2]=15 [3]=25 [4]=100
Object: 25 found at [3]
Object: 10 found at [1]
Object: 2000 not found. No array object has a greater value.
To increase your C# knowledge, practice all C sharp programs, here is a collection of 100+ C# problems with solutions.
Editorial Staff at FreeWebMentor is a team of professional developers leads by Prem Tiwari View all posts by Editorial Staff