Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, we are going to take you back to your grade school days. Speci

ID: 3599191 • Letter: F

Question

For this project, we are going to take you back to your grade school days. Specifically, you will write a simple word search game, similar to those puzzles that you were given in second or third grade right before a holiday. With those puzzles,you were given a square matrix of letters and you had to find words like turkey and pilgrim and mayflowerbefore Thanksgiving, and words like santa and snow and elfbefore Christmas c c a ti This project takes an integer size and a filename as its rwo command-line arguments. The size represents the number of rows and columns in a square matrix of characters. The filename specified t contains the actual square matrix of these characters. As a quick example, consider the file datal that is shown at the right (a 5x5 puzzle): a ce For the puzzle above, you would specify 5 and data! as your two command-line arguments, as in . /a. out 5 data! With this example, a sample execution of the program is shown below (the user is searching for the word cat) The word puzzle is ccati dogac otace gotac ecata Enter a word to find in the puzzle cat Tbe word cat was found 6 times, as shown below 2 times written left-to-right 1 times written top-to-bottom 1 times written bottom-to-top 2 times written right-to-left cat tac tac ca Enter another word to find (or 'zzz' to exit) zzz A couple of comments regarding how the game should be structured The input matrix will be lowercase letters, as will the words that the user enters to find Words can be written left-to-right (normal English), right-to-left (backwards), top-to-bottom (each letter below the previous letter), and bottom-to-top (cach letter above the previous letter). · We have configured this project and its grading so that you can approach it in pieces. Start by getting the program to read (and print) the data file. Then tackle finding words that are written left-to-right. Once you have left-to-right implemented pick another direction and implement that. Continue until you have written all four directions Create a directory project5 on your machine. In that directory, create a file named words.c In words.c, write the code nceded to implement this program. Make sure that you include a header block of . your name and a brief overview of the program comments with . You must use a two-dimensional dynamic array for this project, as different data sets have different sizes .You must use at least three functions in this program. * To submit your project, first bundle your project5 directory into a single (compressed) zip file. Once you have a zap file that contains your projects code submit that fille to Blackboard.

Explanation / Answer

Hi,

Great Question.

Code is as follows:

//File reading portion is commented. You can uncomment it and comment the 25 initializing character.

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

void findLeftToRight(char *words, int mSize, char word[256], char **output){
printf(" Word to be found:%s",word);
int i,j,k;
int len = strlen(word) - 1;
int index = 0;
int match = 0;
printf(" Word length is :%d",len);
for(i=0;i<mSize;i++){
index = 0;   
for(j=0;j<mSize;j++){
if(words[i*mSize+j] == word[index]){
//printf("%c ",words[i*mSize+j]);
index++;
}else if(words[i*mSize+j] == word[0]){
//printf("%c %d",words[i*mSize+j],j);
index = 1;
}else {
index = 0;
//printf("%c ",words[i*mSize+j]);
}
//printf("%c %d %d ",words[i*mSize+j],index,j);
if(index == len){
//printf(" %d ",j);
k = j;
while(index > 0){
output[i][k] = word[index-1];;
//printf(" %c",output[i][k]);
index--;
k--;
}
//printf(" A match found ");
match++;
}
}
}
printf("We have %d match(es) from left to right and output matrix is ",match);
for(i=0;i<mSize;i++){
for(j=0;j<mSize;j++){
printf("%c ",output[i][j]);
}
printf(" ");
}
}

void findRightToLeft(char *words, int mSize, char word[256], char **output){
//printf(" Word to be found:%s",word);
int i,j,k;
int len = strlen(word) - 1;
int index = 0;
int match = 0;
//printf(" Word length is :%d",len);
for(i=0;i<mSize;i++){
index = 0;   
for(j=mSize-1;j>=0;j--){
if(words[i*mSize+j] == word[index]){
//printf("%c ",words[i*mSize+j]);
index++;
}else if(words[i*mSize+j] == word[0]){
//printf("%c %d",words[i*mSize+j],j);
index = 1;
}else {
index = 0;
//printf("%c ",words[i*mSize+j]);
}
//printf("%c %d %d %c ",words[i*mSize+j],index,j,word[index]);
if(index == len){
//printf(" %d ",j);
k = j;
while(index > 0){
output[i][k] = word[index-1];;
//printf(" %c",output[i][k]);
index--;
k++;
}
//printf(" A match found ");
match++;
}
}
}
printf("We have %d match(es) from right to left and output matrix is ",match);
for(i=0;i<mSize;i++){
for(j=0;j<mSize;j++){
printf("%c ",output[i][j]);
}
printf(" ");
}
}

void findTopToBottom(char *words, int mSize, char word[256], char **output){
//printf(" Word to be found:%s",word);
int i,j,k;
int len = strlen(word) - 1;
int index = 0;
int match = 0;
//printf(" Word length is :%d",len);
for(j=3;j<mSize-1;j++){
index = 0;   
for(i=0;i<mSize;i++){
if(words[i*mSize+j] == word[index]){
//printf("%c ",words[i*mSize+j]);
index++;
}else if(words[i*mSize+j] == word[0]){
//printf("%c %d",words[i*mSize+j],j);
index = 1;
}else {
index = 0;
//printf("%c ",words[i*mSize+j]);
}
//printf("%c %d %d %c ",words[i*mSize+j],index,j,word[index]);
if(index == len){
//printf(" %d ",j);
k = i;
while(index > 0){
output[k][j] = word[index-1];;
//printf(" %c",output[i][k]);
index--;
k--;
}
//printf(" A match found ");
match++;
}
}
}
printf("We have %d match(es) from top to bottom and output matrix is ",match);
for(i=0;i<mSize;i++){
for(j=0;j<mSize;j++){
printf("%c ",output[i][j]);
}
printf(" ");
}
}

void findBottomToTop(char *words, int mSize, char word[256], char **output){
//printf(" Word to be found:%s",word);
int i,j,k;
int len = strlen(word) - 1;
int index = 0;
int match = 0;
//printf(" Word length is :%d",len);
for(j=0;j<mSize;j++){
index = 0;   
for(i=mSize-1;i>=0;i--){
if(words[i*mSize+j] == word[index]){
//printf("%c ",words[i*mSize+j]);
index++;
}else if(words[i*mSize+j] == word[0]){
//printf("%c %d",words[i*mSize+j],j);
index = 1;
}else {
index = 0;
//printf("%c ",words[i*mSize+j]);
}
//printf("%c %d %d %c ",words[i*mSize+j],index,j,word[index]);
if(index == len){
//printf(" %d ",j);
k = i;
while(index > 0){
output[k][j] = word[index-1];
printf(" %c %c",output[k][j],word[index-1]);
index--;
k++;
}
//printf(" A match found ");
match++;
}
}
}
printf("We have %d match(es) from bottom to top and output matrix is ",match);
for(i=0;i<mSize;i++){
for(j=0;j<mSize;j++){
printf("%c ",output[i][j]);
}
printf(" ");
}
}


int main(int argc, char ** argv) {
if(argc != 3){
printf("Please pass two arguments. 1. integer 2. File which contains puzzle ");
return -1;
}
int mSize = atoi(argv[1]);
char words[mSize][mSize];
int i,j;
char word[256];
//char output[mSize][mSize];
char **output = (char**)malloc(sizeof(char*)*mSize);
for(i =0 ; i <mSize;i++)
*(output+i) = (char*)malloc(sizeof(char)*mSize);
  
for(i=0;i<mSize;i++){
for(j=0;j<mSize;j++){
output[i][j]='.';
}
}
words[0][0]='c';
words[0][1]='c';
words[0][2]='a';
words[0][3]='t';
words[0][4]='i';
words[1][0]='d';
words[1][1]='o';
words[1][2]='g';
words[1][3]='a';
words[1][4]='c';
words[2][0]='o';
words[2][1]='t';
words[2][2]='a';
words[2][3]='c';
words[2][4]='e';
words[3][0]='g';
words[3][1]='o';
words[3][2]='t';
words[3][3]='a';
words[3][4]='c';
words[4][0]='e';
words[4][1]='c';
words[4][2]='a';
words[4][3]='t';
words[4][4]='a';

//FILE *file = fopen(argv[2], "r");
//if (file) {
// while ((c = getc(file)) != EOF){
// if(j == (mSize-1)){
// i++;
// j = 0;
// }
// words[i][j] = (char) c;
// }
// fclose(file);
//}
printf("Puzzle: ");
for(i=0;i<mSize;i++){
for(j=0;j<mSize;j++){
printf("%c ",words[i][j]);
}
printf(" ");
}
printf(" Enter a word to find in the puzzle:");
fgets(word, sizeof word, stdin);
findLeftToRight((char*)words,mSize,word, output);
findRightToLeft((char*)words,mSize,word, output);
findTopToBottom((char*)words,mSize,word, output);
findBottomToTop((char*)words,mSize,word, output);
}

You can see the code in action using: https://ideone.com/C0QyF5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote