Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!