C# Program to Search an Element with Array Indices

This is a C# Program to search an element with array indices.

Problem Description

This C# Program Searches an element with Array Indices.

Problem Solution

Here the the element is searched in the array.

Program/Source Code

Here is source code of the C# Program to Search an element with Array Indices. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Search an element with Array Indices
 */
using System;
 
class ArrayBinarySearch
{
    public static void Main()
    {
        int[] ints = { 0, 10, 100, 1000, 1000000 };
        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, 1000);
        FindObject(ints, 2000000);
        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);
        }
    }
}
Program Explanation

This C# program is used to search an element with array indices. We have already defined the values of ‘ints[]’ array variable. Using for loop print the coefficient element values present in the ints[] array variable.

advertisement

The Findobject() function uses the BinarySearch() function to search an array element. Nested if else condition statement is used to check that the value of ‘index’ variable is greater than 0. If the condition is true then execute the statement and print the index of the value present in the array.

Otherwise, if the condition is false then execute else if condition statement. Check the value of ‘index’ variable is equal to the length of the array variable. If the condition is true then execute the else if statement and print the statement as the value is not found in the array.

Otherwise, if the condition is false then execute the else statement and print the larger object found in the array index value.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
Runtime Test Cases
 
Array indices and elements:
[0]=0    [1]=10   [2]=100  [3]=1000 [4]=1000000
Object: 25 not found. Next larger object found at [2].
Object: 1000 found at [3]
Object: 2000000 not found. No array object has a greater value.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.