Advertisement
callumbinner22

Linear Search

Jun 8th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication60
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] animals = new string[] { "cat", "dog", "mouse", "horse", "donkey", "rabbit", "monkey", "pigeon", "pig", "cow", "hen", "sheep", "shark", "fish", "lion", "tiger", "whale", "sloth", "goat", "chetah" };
  14.             Console.WriteLine("Please enter the animal you would to search for");
  15.             string animal = Console.ReadLine();
  16.  
  17.           Console.WriteLine("Your location is " +  Linearsearch(animal, animals));
  18.         }
  19.  
  20.         static int Linearsearch(string value, string[] animals)
  21.         {
  22.             for ( int x = 0; x<animals.Length-1; x++)
  23.             {
  24.                 if (animals [x] == value)
  25.                 {
  26.                     return x;
  27.                 }
  28.             }
  29.             return -1;
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement