Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help debugging my project for C# code. i took the algorithm from C++ code and i am getting errors with the declaration for f[].

Need help debugging my project for C# code. i took the algorithm from C++ code and i am getting errors with the declaration for f[].

Project idea: Prompt user to input pattern to search, generate a random string, run the code for the algorithm KMP [preKMP & Search] with the pattern that was inputted and the string that was generated, print out results for what is found with the algorithm.

1.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace KMP { public class ProgramTest { public static void Main(string[] args) { Program myProgram = new Program("Thank you for running the " + "Brute Force Algorithm");

myProgram.InputPattern(); //read in the inputted pattern from user

myProgram.RandomGenerator(); //run the random generator program

myProgram.preKMP(); myProgram.Search();

myProgram.DisplayTime();

Console.Read(); } } }

2.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace KMP { public class Program { string s; string pattern; int f[];

public Program(string name) {

}

public void InputPattern() { Console.WriteLine("Enter the pattern you want to search: "); pattern = (Console.ReadLine()).ToUpper(); //read user input

}

public void RandomGenerator() { Random geneSequence = new Random(); // The random number sequence

for (int i = 0; i < 100; i++) { int x = geneSequence.Next(1, 4);

switch (x) { case 1: s = s + 'C'; break;

case 2: s = s + 'T'; break;

case 3: s = s + 'A'; break;

case 4: s = s + 'G'; break; }

}

Console.WriteLine(s); //display results of string }

public void preKMP() { int m = pattern.Length; int k; f[0] = -1; for (int i = 1; i < m; i++) { k = f[i - 1]; while (k >= 0) { if (pattern[k] == pattern[i - 1]) break; else k = f[k]; } f[i] = k + 1; } }

public void Search() // s is string sequence, pattern is what is inputted from user {

//kmp algorithm is executed here

int m = pattern.Length; int n = s.Length; int f[m];

preKMP(pattern, f); int i = 0; int k = 0;

while ( i < n) { if (k == -1) { ++i; k = 0; } else if (s[i] == pattern[k]) { i++; k++; if (k == m) return 1; } else k = f[k]; } return 0;

}

public void DisplayTime() { var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds;

Console.WriteLine("Time processed: {0}ms", watch.ElapsedMilliseconds); }

}

}

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

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

Students also viewed these Databases questions

Question

How many (a) quarts and (b) gallons are there in 10.0 L?

Answered: 1 week ago

Question

Understand why customers are loyal to a particular service firm.

Answered: 1 week ago