CS 2213 Advanced Programming, Spring 2001 Programming Assignment 2


Due In Class, March 27, 2001

In this assignment write a program to solve word search problems.


Part 0: Fixed size arrays

Create a directory called assign2 for this assignment. Make a subdirectory of this called part0.

Copy all of the files from /usr/local/courses/cs2213/spring2001/assign2 into this directory. You should have 4 files: a makefile, a header file (.h) and two test files (.in).

Your program will read in a two dimensional array of characters having ROWS rows and COLUMNS columns. ROWS and COLUMNS are constants defined in the header file. It will then read in a number of words and attempt to find them in the array moving horizontally (left or right), vertically (up or down) or diagonally (4 directions). For each word it will print the word and where it found it in the array with a description of its direction.

You will write the following functions and put them in wordsearch.c. These have prototypes in wordsearch.h

int fillArrayFixed(char a[ROWS][COLUMNS]);

void printArrayFixed(char a[ROWS][COLUMNS]); int getWord(char *w, int size); int searchFixed(char a[ROWS][COLUMNS],char *s, int *xp, int *yp);

Write a main program in driver.c that starts by printing a line in the form:
This program was written by ...
It then reads two integers from standard input using:
scanf("%d%d\n",&rows,&columns);
It checks that rows is the same as ROWS and columns is the same as COLUMNS. If not, this is an error that should be reported. If these are OK, the program uses fillArrayFixed to read in the array and then uses getWord in a loop to read words until an error occurs. The size parameter for getWord should be at least as big as the larger of ROWS and COLUMNS. For each word read in, searchFixed is called and the results are printed using one line for each word.

The output produced for each input word should contain the word, and whether or not the word was found. If the word was found, the row and columns numbers should be given along with a description of the direction. For this output only, report the row and column numbers starting at 1 rather than 0. One way to easily get a description of the direction is to use the following array:

   char *directions[] = {"not found","across","down","backwards","up",
                        "diagonal down", "diagonal down and back",
                        "diagonal up", "diagonal up and back"};
Test your program by redirecting the input to come from the file test1.in.

Try to format your output so that it looks something like:

                word    row column direction
             POINTER      1      1 down
           KERNIGHAN      3      2 across
             RITCHIE      6      7 backwards
            OPERATOR      7      9 backwards
            ...


Part 1
Make a directory called part1 for this. Copy everything from your part0 directory into the part1 directory. Replace fillArrayFixed, printArrayFixed, and searchFixed with the following:

int fillArray(char *a, int rows, int columns);
void printArray(char *a, int rows, int columns);
int search(char *a, int rows, int columns,char *s, int *xp, int *yp);
These assume that a is a 2-dimensional array with the given number of rows and columns. Rewrite your program to use these. Do not use the constants ROWS and COLUMNS.

Read in the rows and columns as before and use these to create your array (using malloc). Your functions will have to do the array subscripting with a calculation. Test your program with the files test1.in and test2.in. The first one should give the same results as for Part 0.


Part 2
This part is optional and is for extra credit (not much).
Create a part2 directory for this one. Copy all of your Part 1 files into this directory and modify the programs so that they will find all occurrences of the words, not just one.


Handing in your assignment
Use the cover sheet as your first page. Fill in the table of contents with the appropriate page numbers. Make sure there are no lint warnings that should not be there.