1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Calculating the number of occurances of a pattern in an array (given as input files)

  1. Apr 13, 2012 #1
    1. The problem statement, all variables and given/known data
    So basically I got this problem and I am still constructing an algorithm for it. I am not entirely sure the algorithm is going in the right direction, though. The problem is as follows:

    The 2-D array and the pattern are read from two input files named Array and Pattern. Each
    line of these files contains a row; the numbers on each line are separated by spaces.
    Organize hierarchically your code, writing a function that counts the pattern appearances for
    just one fixed orientation, and calling this function from the main function for each of the 4
    orientations. (The number of levels of hierarchy is arbitrary large, that is, the function may call other functions as well.)
    For simplicity, there is no need to consider only distinct patterns: for instance, if the pattern is
    a 2 × 2 array having all the elements equal to 1, its appearance in the larger array may be reported
    4 times, once for each orientation.

    2. Relevant equations

    3. The attempt at a solution

    My problem is with constructing the algorithm initially. What I thought of, is to initialize line by line to a string from the pattern, and compare it to the elements in the array from i=0 until i=n-1 (if n is the number of elements in the row of the pattern) and if all the elements are equal, we move to the next column until we reach the column in the array that is equal to the same number of the final column of the pattern. If so, we can start looking for the pattern by incrementing n-1 columns and m-1 rows for example (these being the number of elements in the rows and columns) and if not, we increment by one. I am not sure if I am moving in the right direction though? I have just studied file processing, so I do not think I can check rows and columns at the same time like in a 2D matrix.
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted