So basically, I have to write a program in C. The following are the instructions
ID: 3680498 • Letter: S
Question
So basically, I have to write a program in C. The following are the instructions.
Word Search is a popular puzzle game that consists of a grid of alphabetical letters, and a number of words that players have to find within the grid. The words are formed by a series of contiguous letters in the grid along a single direction, where the direction is one of: forward, backward, upward, downward, No “curved” words may exist, and words cannot continue on to the next line or the next column. Your task is to write a program store the followind pattern: (SHOWN BENEATH) and lets the user repeatedly specify words to be found in the grid. If a specified word is successfully found, the program will alter the grid such that the letters of the word become capitalized. If the user enters the word ‘QUIT’, the program will end. You may assume that the puzzle grids provided will always be square, and always contain alphabetical characters. They will be provided in files where the first line is a number indicating the size of the grid (the width/height), followed by each row of the puzzle.
TO WRITE THIS CODE, YOU MUST MAKE USE OF THE GIVEN FUNCTIONS AND YOU SHOULD NOT ALTER THE MAIN FUNCTION!
I PERSONALLY HAVE WRITTEN THE FIRST TWO FUNCTIONS. I JUST NEED HELP WITH THE REMAININNG THREE FUNCTION(TWO ALREADY IN THE SKELETON AND MAKE ONE NEW ONE)!
THIS IS THE PUZZLE
10
h g a m o n i h r a
a o m o k a w o n
s n f r o l b o b d n
a r f s i h c a g e l
n i e e w o n o k g
o l f u n d t h c k o
c a t a o h b i a m r
e r c g a n h s l g f
a m a l l c a l l i g
a t o r x
THIS IS THE SKELETON WITH MY WORK INCLUDED IN IT.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#define MAX_WORD_LEN 20
#define MAX_COL 20
#define MAX_ROW 20
#define MAX_FILENAME 100
#define QUIT_KEYWORD "QUIT"
/* APS105 students *MUST* make use of the functions prototyped below */
/* Prompts the user for an input file to read from.
* Parses the input file and stores the puzzle into the 2D array provided.
* Stores the size of the puzzle using the integer pointer provided.
* Returns 'true' if successful, 'false' if something went wrong.
*/
bool readPuzzle(char cGrid[][MAX_COL], int *pdPuzzleSize) {
int i = 0, j = 0;
char myPuzz [MAX_FILENAME];
// printf("File name: ");
// scanf("%s", &puzzFile);
printf("Please enter the files name: ");
scanf("%s", myPuzz);
FILE *puzzle3;
puzzle3 = fopen(myPuzz, "r");
if (puzzle3 == NULL) {
printf("The program failed to open. Exiting Program...");
return false;
}
fscanf(puzzle3, "%d", pdPuzzleSize);
if (*pdPuzzleSize > 20 || *pdPuzzleSize < 4) {
printf("The Size of the puzzle is not compatible.");
exit(1);
}
for (i = 0; !feof(puzzle3) && i < *pdPuzzleSize; i++) {
for (j = 0; !feof(puzzle3) && j<*pdPuzzleSize; j++) {
fscanf(puzzle3, " %c", &cGrid[i][j]);
}
}
for (i = 0; !feof(puzzle3) && i < *pdPuzzleSize; i++) {
for (j = 0; !feof(puzzle3) && j< *pdPuzzleSize; j++) {
printf(" %c", tolower(cGrid[i][j]));
}
printf(" ");
}
fclose(puzzle3);
return true;
}
/* Queries the user for a word to search, storing it in the string provided */
void readSearchWord(char searchWord[], int dPuzzleSize);
/* Finds the search word in the 2D array provided */
void findWordInPuzzle(char cGrid[][MAX_COL], int dPuzzleSize, char searchWord[]);
/* High-level main function. APS105 students *MUST NOT* modify this function */
int main() {
int i = 0, j = 0;
char cGrid[MAX_ROW][MAX_COL] = {0};
char searchWord[MAX_WORD_LEN + 1] = {0};
bool status = true;
int dPuzzleSize = 0;
// Read puzzle from file
status = readPuzzle(cGrid, &dPuzzleSize);
if (status) {
// Print the grid that was read in, then query
// the user for a string and try to find it
printPuzzle(cGrid, dPuzzleSize);
readSearchWord(searchWord, dPuzzleSize);
// If the entered word isn't 'QUIT', continue...
while (strcmp(searchWord, QUIT_KEYWORD)) {
printf("Searching for the word: %s ... ", searchWord);
findWordInPuzzle(cGrid, dPuzzleSize, searchWord);
printPuzzle(cGrid, dPuzzleSize);
// Query user for string, then try to find it
readSearchWord(searchWord, dPuzzleSize);
}
}
printf("Exiting program... ");
return 0;
}
Explanation / Answer
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static int prompt_for(char *buffer, size_t buflen);
static int search_direction(char array[18][18], char *string, int length, int X, int Y, int dx, int dy);
static int search_from(char array[18][18], char *string, int length, int X, int Y);
int main(void)
{
int X, Y, length;
char array[18][18], string[50];
printf("WORDSEARCH SOLVER - PRESS CTRL-C TO QUIT ");
FILE *wsch = fopen("wrdsearch.txt", "r");
if (!wsch)
{
printf("Error! File did not open. ");
return 1;
}
for (Y = 0; Y < 18; Y++)
{
for (X = 0; X < 18; X++)
{
fscanf(wsch, " %c", &array[Y][X]);
array[Y][X] = toupper(array[Y][X]);
}
}
fclose(wsch);
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 ", array[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 (array[Y][X] == (string[0]) && search_from(array, 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(" Please 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] = '';
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 array[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(array, string, length, X, Y, directions[i].dx, directions[i].dy))
count++;
}
return count;
}
static int search_direction(char array[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 (array[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, array[y][x], y, x);
pad = ", ";
}
putchar(' ');
return 1;
}
Given a list of US presidential surnames thus:
adams
arthur
buchanan
bush
carter
cleveland
clinton
coolidge
eisenhower
fillmore
ford
garfield
grant
harding
harrison
hayes
hoover
jackson
jefferson
johnson
kennedy
lincoln
madison
mckinley
monroe
nixon
obama
pierce
polk
reagan
roosevelt
taft
taylor
truman
tyler
vanburen
washington
wilson
When run as ws < presidents, so the names are not seen on the screen until echoed,
it finds every one of the names at least once, thus:
WORDSEARCH SOLVER - PRESS CTRL-C TO QUIT
0 1 2 3 4 5 6 7 8 9 1011121314151617
______________________________________
0 |M N O S L I W E R E L Y T L E A G N
1 |A H O O V E R T A Y L O R V E N N A
2 |D F D R O O S E V E L T O N O M I M
3 |I N T P M H I E G D I L O O C O D U
4 |S O N L I J Q A D A M S S R N N R R
5 |O X L O G E F F M O I R E E G R A T
6 |N I B T S O R A O R M O V A W O H N
7 |F N H U R R B C R N L R E G B E W E
8 |R O S D C O E A E I O N L A U A J R
9 |K W U N L H H F N I O S T N S W A U
10|E R B A J B A C F S S M N H H R D B
11|N E W L O T O N K E C E I H T H A N
12|N T H E H L A C A K J N N H O I M A
13|E R G V N C A F I N G H U H A J S V
14|D A R E S J C N T T A R N B O Y A E
15|Y C A L O D L N O S I R R A H W E O
16|N E N C N E T N N O T N I L C O E S
17|D A T Y Y P O L K G A R F I E L D R
Please enter the word to be searched: Searching for: [ADAMS]
Found word ADAMS starting at (4,7) to (4,11)
A (4,7), D (4,8), A (4,9), M (4,10), S (4,11)
Found word ADAMS starting at (9,16) to (13,16)
A (9,16), D (10,16), A (11,16), M (12,16), S (13,16)
Found ADAMS 2 times
Please enter the word to be searched: Searching for: [ARTHUR]
Found word ARTHUR starting at (9,16) to (14,11)
A (9,16), R (10,15), T (11,14), H (12,13), U (13,12), R (14,11)
Found ARTHUR 1 times
Please enter the word to be searched: Searching for: [BUCHANAN]
Found word BUCHANAN starting at (6,2) to (13,9)
B (6,2), U (7,3), C (8,4), H (9,5), A (10,6), N (11,7), A (12,8), N (13,9)
Found BUCHANAN 1 times
Please enter the word to be searched: Searching for: [BUSH]
Found word BUSH starting at (7,14) to (10,14)
B (7,14), U (8,14), S (9,14), H (10,14)
Found word BUSH starting at (10,2) to (7,2)
B (10,2), U (9,2), S (8,2), H (7,2)
Found BUSH 2 times
Please enter the word to be searched: Searching for: [CARTER]
Found word CARTER starting at (15,1) to (10,1)
C (15,1), A (14,1), R (13,1), T (12,1), E (11,1), R (10,1)
Found CARTER 1 times
Please enter the word to be searched: Searching for: [CLEVELAND]
Found word CLEVELAND starting at (16,3) to (8,3)
C (16,3), L (15,3), E (14,3), V (13,3), E (12,3), L (11,3), A (10,3), N (9,3), D (8,3)
Found CLEVELAND 1 times
Please enter the word to be searched: Searching for: [CLINTON]
Found word CLINTON starting at (16,14) to (16,8)
C (16,14), L (16,13), I (16,12), N (16,11), T (16,10), O (16,9), N (16,8)
Found CLINTON 1 times
Please enter the word to be searched: Searching for: [COOLIDGE]
Found word COOLIDGE starting at (3,14) to (3,7)
C (3,14), O (3,13), O (3,12), L (3,11), I (3,10), D (3,9), G (3,8), E (3,7)
Found COOLIDGE 1 times
Please enter the word to be searched: Searching for: [EISENHOWER]
Found word EISENHOWER starting at (8,8) to (17,17)
E (8,8), I (9,9), S (10,10), E (11,11), N (12,12), H (13,13), O (14,14), W (15,15), E (16,16), R (17,17)
Found EISENHOWER 1 times
Please enter the word to be searched: Searching for: [FILLMORE]
Found word FILLMORE starting at (7,0) to (0,7)
F (7,0), I (6,1), L (5,2), L (4,3), M (3,4), O (2,5), R (1,6), E (0,7)
Found FILLMORE 1 times
Please enter the word to be searched: Searching for: [FORD]
Found word FORD starting at (5,6) to (8,3)
F (5,6), O (6,5), R (7,4), D (8,3)
Found FORD 1 times
Please enter the word to be searched: Searching for: [GARFIELD]
Found word GARFIELD starting at (17,9) to (17,16)
G (17,9), A (17,10), R (17,11), F (17,12), I (17,13), E (17,14), L (17,15), D (17,16)
Found GARFIELD 1 times
Please enter the word to be searched: Searching for: [GRANT]
Found word GRANT starting at (13,2) to (17,2)
G (13,2), R (14,2), A (15,2), N (16,2), T (17,2)
Found GRANT 1 times
Please enter the word to be searched: Searching for: [HARDING]
Found word HARDING starting at (6,16) to (0,16)
H (6,16), A (5,16), R (4,16), D (3,16), I (2,16), N (1,16), G (0,16)
Found HARDING 1 times
Please enter the word to be searched: Searching for: [HARRISON]
Found word HARRISON starting at (9,6) to (2,13)
H (9,6), A (8,7), R (7,8), R (6,9), I (5,10), S (4,11), O (3,12), N (2,13)
Found word HARRISON starting at (15,14) to (15,7)
H (15,14), A (15,13), R (15,12), R (15,11), I (15,10), S (15,9), O (15,8), N (15,7)
Found HARRISON 2 times
Please enter the word to be searched: Searching for: [HAYES]
Found word HAYES starting at (12,13) to (16,17)
H (12,13), A (13,14), Y (14,15), E (15,16), S (16,17)
Found HAYES 1 times
Please enter the word to be searched: Searching for: [HOOVER]
Found word HOOVER starting at (1,1) to (1,6)
H (1,1), O (1,2), O (1,3), V (1,4), E (1,5), R (1,6)
Found HOOVER 1 times
Please enter the word to be searched: Searching for: [JACKSON]
Found word JACKSON starting at (14,5) to (8,11)
J (14,5), A (13,6), C (12,7), K (11,8), S (10,9), O (9,10), N (8,11)
Found JACKSON 1 times
Please enter the word to be searched: Searching for: [JEFFERSON]
Found word JEFFERSON starting at (12,10) to (4,2)
J (12,10), E (11,9), F (10,8), F (9,7), E (8,6), R (7,5), S (6,4), O (5,3), N (4,2)
Found JEFFERSON 1 times
Please enter the word to be searched: Searching for: [JOHNSON]
Found word JOHNSON starting at (10,4) to (16,4)
J (10,4), O (11,4), H (12,4), N (13,4), S (14,4), O (15,4), N (16,4)
Found word JOHNSON starting at (13,15) to (7,9)
J (13,15), O (12,14), H (11,13), N (10,12), S (9,11), O (8,10), N (7,9)
Found JOHNSON 2 times
Please enter the word to be searched: Searching for: [KENNEDY]
Found word KENNEDY starting at (9,0) to (15,0)
K (9,0), E (10,0), N (11,0), N (12,0), E (13,0), D (14,0), Y (15,0)
Found KENNEDY 1 times
Please enter the word to be searched: Searching for: [LINCOLN]
Found word LINCOLN starting at (7,10) to (13,4)
L (7,10), I (8,9), N (9,8), C (10,7), O (11,6), L (12,5), N (13,4)
Found LINCOLN 1 times
Please enter the word to be searched: Searching for: [MADISON]
Found word MADISON starting at (0,0) to (6,0)
M (0,0), A (1,0), D (2,0), I (3,0), S (4,0), O (5,0), N (6,0)
Found MADISON 1 times
Please enter the word to be searched: Searching for: [MCKINLEY]
Found word MCKINLEY starting at (10,11) to (17,4)
M (10,11), C (11,10), K (12,9), I (13,8), N (14,7), L (15,6), E (16,5), Y (17,4)
Found MCKINLEY 1 times
Please enter the word to be searched: Searching for: [MONROE]
Found word MONROE starting at (2,15) to (7,15)
M (2,15), O (3,15), N (4,15), R (5,15), O (6,15), E (7,15)
Found MONROE 1 times
Please enter the word to be searched: Searching for: [NIXON]
Found word NIXON starting at (7,1) to (3,1)
N (7,1), I (6,1), X (5,1), O (4,1), N (3,1)
Found NIXON 1 times
Please enter the word to be searched: Searching for: [OBAMA]
Found word OBAMA starting at (8,5) to (4,9)
O (8,5), B (7,6), A (6,7), M (5,8), A (4,9)
Found OBAMA 1 times
Please enter the word to be searched: Searching for: [PIERCE]
Found word PIERCE starting at (3,3) to (8,8)
P (3,3), I (4,4), E (5,5), R (6,6), C (7,7), E (8,8)
Found PIERCE 1 times
Please enter the word to be searched: Searching for: [POLK]
Found word POLK starting at (17,5) to (17,8)
P (17,5), O (17,6), L (17,7), K (17,8)
Found POLK 1 times
Please enter the word to be searched: Searching for: [REAGAN]
Found word REAGAN starting at (4,13) to (9,13)
R (4,13), E (5,13), A (6,13), G (7,13), A (8,13), N (9,13)
Found REAGAN 1 times
Please enter the word to be searched: Searching for: [ROOSEVELT]
Found word ROOSEVELT starting at (1,12) to (9,12)
R (1,12), O (2,12), O (3,12), S (4,12), E (5,12), V (6,12), E (7,12), L (8,12), T (9,12)
Found word ROOSEVELT starting at (2,3) to (2,11)
R (2,3), O (2,4), O (2,5), S (2,6), E (2,7), V (2,8), E (2,9), L (2,10), T (2,11)
Found ROOSEVELT 2 times
Please enter the word to be searched: Searching for: [TAFT]
Found word TAFT starting at (11,5) to (14,8)
T (11,5), A (12,6), F (13,7), T (14,8)
Found TAFT 1 times
Please enter the word to be searched: Searching for: [TAYLOR]
Found word TAYLOR starting at (1,7) to (1,12)
T (1,7), A (1,8), Y (1,9), L (1,10), O (1,11), R (1,12)
Found TAYLOR 1 times
Please enter the word to be searched: Searching for: [TRUMAN]
Found word TRUMAN starting at (5,17) to (0,17)
T (5,17), R (4,17), U (3,17), M (2,17), A (1,17), N (0,17)
Found TRUMAN 1 times
Please enter the word to be searched: Searching for: [TYLER]
Found word TYLER starting at (0,12) to (0,8)
T (0,12), Y (0,11), L (0,10), E (0,9), R (0,8)
Found TYLER 1 times
Please enter the word to be searched: Searching for: [VANBUREN]
Found word VANBUREN starting at (13,17) to (6,17)
V (13,17), A (12,17), N (11,17), B (10,17), U (9,17), R (8,17), E (7,17), N (6,17)
Found VANBUREN 1 times
Please enter the word to be searched: Searching for: [WASHINGTON]
Found word WASHINGTON starting at (7,16) to (16,7)
W (7,16), A (8,15), S (9,14), H (10,13), I (11,12), N (12,11), G (13,10), T (14,9), O (15,8), N (16,7)
Found WASHINGTON 1 times
Please enter the word to be searched: Searching for: [WILSON]
Found word WILSON starting at (0,6) to (0,1)
W (0,6), I (0,5), L (0,4), S (0,3), O (0,2), N (0,1)
Found WILSON 1 times
Please enter the word to be searched:
***
note-try this, if required modification can be done.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.