Your task is to complete the \'LinearSearch\' function, which has been outlined
ID: 3747796 • Letter: Y
Question
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:
Explanation / Answer
Please find below completed code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Linear_Search {
/// <summary>
///
/// Demonstrates a Linear Search on an array of strings
///
/// author: Mike Roggenkamp
///
/// date: March 2009
///
/// </summary>
class Program {
static void Main(string[] args) {
const string Start_Message = " Welcome 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
/// <summary>
/// Linear search of "words" for the "word"
/// </summary>
/// <param name="words">list of words to be searched</param>
/// <param name="word">the word being searched for</param>
/// <returns> position of "word" in "words" if it is there
/// otherwise returns -1 </returns>
public static int LinearSearch(string[] words, string word) {
int position = 0;
while(position<words.Length && words[position]!=word)
position += 1;
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
/// <summary>
/// Displays the elements of the array "words"
/// </summary>
/// <param name="words">array to be displayed</param>
static void DisplayArray(string[] words) {
foreach (string element in words) {
OutputMessage(" " + element + " ");
}
Console.WriteLine();
}//end DisplayArray
/// <summary>
/// Displays the outcome of the linear search
/// </summary>
/// <param name="position">either the position of "word" if it was found
/// or -1 if "word" was not found</param>
/// <param name="word"></param>
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
/// <summary>
/// Outputs the string "s"
/// </summary>
/// <param name="s">String to be output</param>
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
//OUTPUT
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.