next up previous
Up: APS105S Tuesday Quiz 2 Previous: 2 Sort Display

Searching a 2-D Array for a Pattern

You are to complete a Turing program that inputs a 2-dimensional integer NxN array and searches it to find a pattern of consecutive integers anywhere in its rows and columns.

The 2-D integer array and the pattern to be searched are read from a file. The name of the file is given as input by the user. The code to do this is given below. The file has the following format:

N
A11 A21 A31 ..  AN1
A12 A22 A32 ..  AN2
.
.
A1N A2N A3N ..  ANN
M 
S1 S2 .. SM
That is, the first number of the file gives N, the size of the NxN array, and the subsequent elements give the contents of the array {tex2html_wrap_inline110} as above.

The final two lines of the file give the number and set of integers that your program will search for, as illustrated above. M is the number of integers and the tex2html_wrap_inline114 .. tex2html_wrap_inline116 are the integers themselves.

The program to read in this file can be found in /usr/copy/aps105/rfile.t and is listed below. You should copy the file /usr/copy/aps105/rfile.t into your file ``part3.t,'' and add your own code at the end of it.

You must write the remainder of the program, for which the task is to see if the pattern of integers (tex2html_wrap_inline114, tex2html_wrap_inline120 .. tex2html_wrap_inline116) can be found in forward sequence in one or more of the rows or columns of the NxN array. Your program's output is the row and column location(s) of the start of the matching sequence(s) in the array, and whether it is a row or a column that matched. Note that you need only find matches in the forward direction - e.g. tex2html_wrap_inline114 matches tex2html_wrap_inline126, tex2html_wrap_inline120 matches tex2html_wrap_inline130, tex2html_wrap_inline132 matches tex2html_wrap_inline134 and so on. The two files M1file and M2file which be found in the directory /usr/copy/aps105 can be used to test your program.

Below is an example input/output flow of the program, using as an example the input file /usr/copy/aps105/M1file:

Enter File Name: M1File

Here is the contents of M1File:  
6
22 76 94 33 44 82
34 44 88 15 88 29
17 33 42 22 15 89
22 76 94 33 44 82
34 44 88 15 88 96
17 33 42 22 15 89

3 
44 88 15

The program output from this file would be:

Match found on a row starting at row 2 column 2
Match found on a row starting at row 5 column 2
Match found on a column starting at row 1 column 5
Match found on a column starting at row 4 column 5

Notes

Here is the code that reads in the file, found in /usr/copy/aps105/rfile.t:

var FileNo: int
var FileName : string
var N, M : int

loop
    put "Enter File Name: " ..
    get FileName
    open : FileNo, FileName, get

    exit when FileNo > 0
    put "Error: Cannot Open", FileName
end loop

% Read in Size of Array, echo, and declare array

get : FileNo, N
put "Array Size: ",N
var SearchArray : array 1 .. N, 1 .. N of int

% Read in the Array to be Searched  (echo it as well)
for i : 1 .. N
    for j : 1 .. N
        get : FileNo, SearchArray (i, j)
        put SearchArray (i, j), " " ..
    end for
    put ""
end for

% Read in Size of Pattern to be searched, and echo

get : FileNo, M
put "Pattern Size: ",M
var Pattern : array 1 .. M of int

% Read in Pattern, and echo

for i : 1 .. M
    get : FileNo, Pattern (i)
    put Pattern (i), " " ..
end for

A solution


next up previous
Up: APS105S Tuesday Quiz 2 Previous: 2 Sort Display

Jonathan Rose
Fri Apr 4 13:33:38 EST 1997