Integer Search
using System;
class ArraySearch
{
public void search(int[] a, int item) // Method for searching the 'item' in the given array 'a'
{
for (int i = 0; i < 5; i++)
{
if (a[i] == item) // Compares each element of the array with the item
{
Console.WriteLine("Found at position: " + (i + 1));
return; // Returns when a matching position is found
}
}
Console.WriteLine("Not found"); // When no match is found
return;
}
static void Main(string[] args)
{
int[] myArray = { 1, 2, 3, 4, 5 }; // Target array
int Item_1 = 3, Item_2 = 10; // Elements to be searched
ArraySearch j = new ArraySearch();
// Searching for Item_1
Console.WriteLine("Item 1 search:");
j.search(myArray, Item_1);
// Searching for Item_2
Console.WriteLine("Item 2 search:");
j.search(myArray, Item_2);
Console.ReadLine();
}
}
Output:
Item 1 search:
Found at position: 3
Item 2 search:
Not found