Array Sequential Searcher - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Array Sequential Searcher

Demo Code

using System;//from  w w  w  .  j a  v a  2s . co  m
class SequentialSearcher
{
   public static int SequentialSearch (int key,int[] arr)
   {
      for (int i = 0; i < arr.Length; i++)
      {
         if (arr[i] == key)
            return i;
         }
         return -1;
      }
      public static void Main()
      {
         int searchKey;
         int searchResult;
         int [] testScores = {2 , 3, 6, 2, 0, 12, 20, 34, 10, 5, 14, 30};
         do
         {
            Console.Write("Please enter the test score you would like to locate: ");
            searchKey = Convert.ToInt32(Console.ReadLine());
            searchResult = SequentialSearch(searchKey, testScores);
            if (searchResult >= 0)
               Console.WriteLine("The score was found in position: {0}\n", searchResult);
            else
               Console.WriteLine("The score was not found\n");
            Console.Write("Would you like to do another search? Y)es N)o ");
         } while (Console.ReadLine().ToUpper() == "Y");
     }
}

Result


Related Tutorials