Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task is to complete the 'LinearSearch' function, which has been outlined in the form of pseudocode, in the provided test framework. Upon completing the

Your task is to complete the 'LinearSearch' function, which has been outlined in the form of pseudocode, in the provided test framework. Upon completing the LinearSearch function you should get output that looks like this (after pressing enter enough times):

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.

USE CODE:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linear_Search { ///  /// /// 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 < words.Length) && (words[position] != word)) * increment position * end while * * if (position < words.Length) * return position // found it * else * return -1 // key not in the list * */ // following return provided so that code compiles return position; // it can be deleted once above algorithm is completed } //end LinearSearch ///  /// 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 < 0) { Console.WriteLine("the word \"{0}\" not found in the list ", word); } else { Console.WriteLine("the word \"{0}\" found in position {1} in the list ",word, position); } OutputMessage(continueMessage); Console.ReadKey(); } //end OutputSearchResult ///  /// 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

technologies on organizations.

Answered: 1 week ago