Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linear_Search { /// /// /// Demonstrates a Linear Search on an array of strings /// ///
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linear_Search { ///Usmg C# complete the LinearSearch function vnch has been outlined in the form of pseudocode in the provided test framework. Upon completing the LinearSearch function you should get output th ooks like this (after pressing enter enough times) Welcome to Linear Search Denonstration The aray contains the following 9 words: t he uick fox Junps t he dog he word "the found in position in the list ress any key to continue: he word "able not found in the list ress any key to continue: he word "ove" found in position 5 in the list Press any key to he word "zebra not found in the list ress any key to continue: he word "dog" found in position 8 in the list Press any key to continue: Press any key to exit progran:. Implement the LinearSearch function as defined by the specification. It should take two arguments: an array of strings and a string to search for and return the position of that string in the array or -1 if the string was not found in the array If the array contains multiple copies of the search string, you should return the first position the string appears in. Note that the Main() function of the provided code is never called- only the Linearsearch) function is called in order to test your program. As a result, there is no problem is the program uses ReadKey (). However, it also means your LinearSearch() method must be public/// /// Demonstrates a Linear Search on an array of strings /// /// author: Mike Roggenkamp /// /// date: March 2009 /// /// class Program { static void Main(string[] args) { const string Start_Message = " \tWelcome to Linear Search Demonstration " + " The array contains the following 9 words: "; string[] words = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; int location; OutputMessage(Start_Message); DisplayArray(words); location = LinearSearch(words, "the"); OutputSearchResult(location, "the"); location = LinearSearch(words, "able"); OutputSearchResult(location, "able"); location = LinearSearch(words, "over"); OutputSearchResult(location, "over"); location = LinearSearch(words, "zebra"); OutputSearchResult(location, "zebra"); location = LinearSearch(words, "dog"); OutputSearchResult(location, "dog"); ExitProgram(); } //end Main ////// Linear search of "words" for the "word" /// /// list of words to be searched /// the word being searched for ///position of "word" in "words" if it is there /// otherwise returns -1 public static int LinearSearch(string[] words, string word) { int position = 0; /* * while ((position /// Displays the elements of the array "words" /// /// array to be displayed static void DisplayArray(string[] words) { foreach (string element in words) { OutputMessage("\t" + element + " "); } Console.WriteLine(); }//end DisplayArray ////// Displays the outcome of the linear search /// /// either the position of "word" if it was found /// or -1 if "word" was not found /// static void OutputSearchResult(int position, string word) { string continueMessage = " Press any key to continue:"; if (position /// Outputs the string "s" /// /// String to be output static void OutputMessage(string s) { Console.Write(s); }// end OutPutMessage static void ExitProgram() { OutputMessage(" Press any key to exit program: "); Console.ReadKey(); }//end ExitProgram }//end class }//end namespace
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started