The assignment is to write a word searching program The prog

The assignment is to write a word searching program. The program takes as input an NxN square filled with letters, and a list of words. There are words buried in the square, either left to right, right to left, top to bottom, bottom to top, diagonally, and so on. This is best explained by an example data file and the output. Suppose that this is the input file:

T A E D Q Q

Z H P N I U

C K E W D I

V U X O F C

B P I R G K

N R T B R B

EXIT

THE

QUICK

BROWN

FOX

Before explaining the program, let me describe the data in more detail. For reading in the square, there is a space after each letter in the square, including the last letter in each row. Looking at the first row, it is “T”, then space, then “A”, then space, etc. and there is a space and a newline character after the last “Q”. By reading the first line of the data, we can tell that this will be a six­by­six square. With respect to the words, they are just a word followed immediately by a newline character. The words will not contain spaces, numbers, etc. This is a word searching problem. The words such as “EXIT” are contained in the square, oriented in one of eight ways, including left to right, right to left, diagonally down, etc. The output of the program should be the puzzle with the non­used letters removed. Thus, only the words found are printed. The output should be printed exactly like the input square, but with blanks replacing those letters that are never contained within a word. Here is the output from the above input file:

T_ _ _ _ Q

_ H _ N _ U

_ _ E W _ I

_ _ X O F C

_ _I R _ K

_ _ T B _ _

The output must be properly formatted. There should be no blank lines before or after the puzzle and each row of the puzzle is followed by a space and then a newline. So in the example above, there WOULD be a space after the Q in the first row of the solution, there is also a space after the space in the last row. The square will never be larger than 50 by 50, so you can declare an array of that size or dynamically allocate space. The words will never be longer than the width of the square ­ six in this example.   Your data files to use for the assignment are on the loki machine:

/home/pcavanaugh/CSCI2240/assignfiles/data1

/home/pcavanaugh/CSCI2240/assignfiles/data2

/home/pcavanaugh/CSCI2240/assignfiles/data3

The solutions to those puzzles are in the same directory, named solution1, solution2, and solution3. Make sure to write your program to accept the exact format of the test files.

To test redirect the data file into your program: cat data1 | ./square or ./square < data1

Note: After the programs are collected, any compiled program (executable) in that directory is deleted and the program is recompiled for testing. Specifically, make sure you compile the program with the same options that are used for grading:

gcc ­Wall –ansi –pedantic wordsearch.c

or whatever the correct file name is. If the compiler generates any errors or warnings you will be penalized.

Make sure that each method has proper documentation:

/* Method Name : calcTax

Parameters : subtotal and tax rate, both doubles

Return value(s) : The calculated tax, a double

Partners : None

Description : This method calcs the tax on a sale amount

*/

double calcTax ( double subtot, double taxrate)

{

return subtot * taxrate;

}

This is an introductory course so I can\'t use too advanced code. We have only gone over functions, string, and pointers.

Solution

#include <ctype.h>
#include <stdio.h>
#include <string.h>

static int prompt_for(char *buffer, size_t buflen);
static int search_direction(char a[18][18], char *string, int length, int X, int Y, int dx, int dy);
static int search_from(char a[18][18], char *string, int length, int X, int Y);

int main(void)
{
int X, Y, length;
char a[18][18], string[50];

printf(\"WORDSEARCH GAME\ \");

FILE *ws = fopen(\"wordsearch.txt\", \"r\");

if (!ws)
{
printf(\"Error! File did not open.\ \");
return 1;
}
for (Y = 0; Y < 18; Y++)
{
for (X = 0; X < 18; X++)
{
fscanf(ws, \" %c\", &a[Y][X]);
a[Y][X] = toupper(a[Y][X]);
}
}
fclose(ws);

printf(\" \");
for (X = 0; X < 18; X++)
printf(\"%-2d\", X);
printf(\"\ _____________________________________\ \");
for (Y = 0; Y < 18; Y++)
{
printf(\"%-2d|\", Y);
for (X = 0; X < 18; X++)
printf(\"%c \", a[Y][X]);
printf(\"\ \ \");
}

while ((length = prompt_for(string, sizeof(string))) != EOF)
{
printf(\"searching for: [%s]\ \", string);

int count = 0;
for (Y = 0; Y < 18; Y++)
{
for (X = 0; X < 18; X++)
{
if (a[Y][X] == (string[0]) && search_from(a, string, length, X, Y))
count++;
}
}

printf(\"Found %s %d times\ \", string, count);
}

printf(\"\ \");
return 0;
}

static int prompt_for(char *buffer, size_t buflen)
{
printf(\"\ Enter the word to be searched: \");
if (fgets(buffer, buflen, stdin) == 0)
return EOF;
size_t length = strlen(buffer);
if (buffer[length-1] == \'\ \')
buffer[--length] = \'\\0\';
if (length == 0)
return EOF;
for (size_t i = 0; i < length; i++)
buffer[i] = toupper(buffer[i]);
return length;
}

static int search_from(char a[18][18], char *string, int length, int X, int Y)
{
struct yx { int dy; int dx; } directions[] =
{
{ +1, 0 }, { -1, 0 }, { +1, +1 }, { -1, +1 },
{ 0, +1 }, { 0, -1 }, { -1, -1 }, { +1, -1 },
};
enum { num_directions = sizeof(directions) / sizeof(directions[0]) };
int count = 0;

for (int i = 0; i < num_directions; i++)
{
if (search_direction(a, string, length, X, Y, directions[i].dx, directions[i].dy))
count++;
}
return count;
}

static int search_direction(char a[18][18], char *string, int length, int X, int Y, int dx, int dy)
{
for (int i = 1; i < length; i++)
{
int x = X + i * dx;
int y = Y + i * dy;
if (x < 0 || x >= 18 || y < 0 || y >= 18)
return 0;
if (a[y][x] != string[i])
return 0;
}

printf(\"Found word %s starting at (%d,%d) to (%d,%d)\ \",
string, Y, X, Y + (length - 1) * dy, X + (length - 1) * dx);
/* Validating search! */
char *pad = \"\";
for (int i = 0; i < length; i++)
{
int x = X + i * dx;
int y = Y + i * dy;
printf(\"%s%c (%d,%d)\", pad, a[y][x], y, x);
pad = \", \";
}
putchar(\'\ \');

return 1;
}

First of you have to create a file with name wordsearch and it sould be in .txt format

In that file you have to give your word puzzle.

When program runs it search for the wordsearch.txt and take it as source and then your game will start.

The assignment is to write a word searching program. The program takes as input an NxN square filled with letters, and a list of words. There are words buried i
The assignment is to write a word searching program. The program takes as input an NxN square filled with letters, and a list of words. There are words buried i
The assignment is to write a word searching program. The program takes as input an NxN square filled with letters, and a list of words. There are words buried i
The assignment is to write a word searching program. The program takes as input an NxN square filled with letters, and a list of words. There are words buried i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site