write a program that will locate certain words in a puzzle. your program will pe
ID: 3627283 • Letter: W
Question
write a program that will locate certain words in a puzzle.
your program will perform the following steps:
1. the array characters will be store in a file 'puzzle.letters'. read the characters of the square block array into a two dimensional character array within computer memory. The first line ill not contain letters but will be a single inteeger value which is the dimesnsion of the 2 dimenrional puzzle array
max array size = 20
2. read the words to search for from the file 'words.list' into a words array within the main memory
3. the program will search the array for each word of the list, beginning wiht the frst column of the row to the last column of the last row
4. anytime a match for a word of the word list is found, then th row and column of its first lettter, along with a direction indicator, is to be displayed on the terminal screen as indicated,
eg
century 6, 9, N
decade 8, 4 NW
alps 3,5 E
requirements:
your program must read input from the 2 files named above and it should print an error message and quit the program if either file cannot be opened
your program will write all the output to the terminal
you will need at leat 2 functions in your program in addition to main:
int read_letters(char letters[][MAX], FILE*file_letters) (let MAX be 20)
void word_search(char letters[][MAX], int size, FILE8file_words)
add other functions as they are needed
be sure to have a program description at the top of your program file
try to stick to library function <stdio.h> only
Explanation / Answer
#include <stdio.h>
int main()
{
char array[4][5];
char word[20];
int i = 0, j = 0, row = 4, col = 5;
FILE *fp = fopen("puzzle.txt", "r");
FILE *fp1=fopen("words.txt","r");
if ( fp )
{
for ( ;; )
{
int c = getc(fp);
if ( c == EOF )
{
break;
}
if ( c != ' ' && c != ' ' )
{
array[i][j] = c;
if ( ++j >= col )
{
j = 0;
if ( ++i >= row )
{
break;
}
}
}
}
}
for ( i = 0; i < row; i++ )
{
for ( j = 0; j < col; j++ )
{
putchar(array[i][j]);
}
putchar(' ');
}
if(fp1)
{
for(;;)
{
int c=getc(fp);
if(c==EOF)
{
break;
}//end of file
if(c!=' '&&c!=' ')
{
word[i]=c;
i++;
}
}
} //end of if
for(i=0;i<20;i++)
{
putchar(word[i]);
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
if(array[i][j]==word[i])
{
printf("%c",word[i]);
if(j==4)
break;
}
}
}
fclose(fp);
fclose(fp1);
return 0;
}
Hope this would help you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.