-
crossword help
Hey, could someone look at this code and tell me what is wrong with this. Im not getting any error while compiling nor is the program giving any output.
There is a puzzle with words hiddn in the puzzle. Besides the words there are just '+'. the C code has to pull words out of the crossword and compare it with a word list to see if the word is present in the list. if present it has to give its coordinates. Also, if the word is present in the reverse direction in the puzzle, the coordinates will be neative.
Code:
#define DIM 15 /* size of array*/
#define WORDNUM 18 /* number of words in the list */
void printwords();
int compareWords(int value, char array[]);
typedef struct {
char structword[15];
int row;
int column;
} WORD;
WORD wordList[WORDNUM];
void readWords(void);
char puzzle[DIM][DIM]={ {'E','U','N','I','T','N','O','C','+','+','+','+','+','+','+'},
{'X','+','+','+','+','+','+','+','+','+','+','+','A','+','+'},
{'I','+','+','+','+','+','+','T','+','P','R','I','N','T','F'},
{'T','+','+','+','+','+','+','U','+','+','+','+','S','+','L'},
{'+','W','H','I','T','E','S','P','A','C','E','+','I','+','O'},
{'T','+','+','+','+','+','+','+','+','+','+','+','+','+','A'}, {'E','+','N','O','I','S','S','E','R','P','X','E','+','+','T'},
{'S','+','+','+','+','+','+','+','+','+','+','+','+','+','+'},
{'T','N','E','M','E','R','C','N','I','+','P','O','W','E','R'},
{'+','+','+','A','+','+','+','+','+','+','+','+','+','+','E'},
{'E','R','O','C','+','I','+','E','L','B','U','O','D','+','T'},
{'+','+','+','R','+','N','+','+','+','+','+','L','L','U','N'},
{'+','+','+','O','+','T','L','U','A','F','E','D','+','+','I'},
{'+','+','+','S','+','+','+','+','+','+','+','+','+','+','O'},
{'+','+','+','+','+','S','Y','M','B','O','L','I','C','+','P'}};
int main()
{
char array[DIM] ;
int x, y, w;
int type=0;
int c = 0, r = 0;
readWords();
int numb =0;
int count;
for (count = 0; count< WORDNUM; count++)
{
printf("%s \n",wordList[count].structword);
}
/* horizontal comparison*/
for (x=1; x<=15; x++)
{
for (y=1; y<=15; y++)
{
w=0;
/*printf("%c",puzzle[x][y]);*/
if (puzzle[x][y]!= '+')
{
r = y;
while (puzzle[x][y]!= '+')
{
array[w] = puzzle[x][y];
printf("%c",array[w]);
w++;
y++;
}
array[w] = '\0';
result = compareWords(r, array);
for (numb=0; numb < WORDNUM; numb++)
{
if (strcmp(wordList[numb].structword , array)
== 0)
{
wordList[numb].row = result;
wordList[numb].column = x;
}
}
y = y + strlen(array);
/*printf("-%d-",y);*/
}
else
continue;
}
}
/* vertical comparison*/
for (y=1; y<=15;y++)
{
for (x=1; x<=15; x++)
{
w=0;
if (puzzle[x][y]!= '+')
{
c = x;
while (puzzle[x][y]!='+')
{
array[w] = puzzle[x][y];
w++;
x++;
}
array[w] = '\0';
result = compareWords(c, array);
for (numb=0; numb < WORDNUM; numb++)
{
if (strcmp(wordList[numb].structword , array) == 0)
{
wordList[numb].row = y;
wordList[numb].column = type;
}
}
x = x + strlen(array);
}
}
}
printwords ();
return 0;
}
void printwords()
{
int linecount;
for (linecount=0; linecount < WORDNUM; linecount++)
{
printf("\n %s (%d,%d)",wordList[linecount].structword,wordList[linecount].row,wordList[linecount].column);
}
}
int compareWords(int value, char array[])
{
int linenum;
int l = strlen(array);
for( linenum = 0; linenum < WORDNUM; linenum++)
{
if ( wordList[linenum].structword[0] == array[0])
return value;
else if ( wordList[linenum].structword[0] == array[l - 1] )
return -1 * (value + l);
else
{
++linenum;
}
}
return 0;
}
void readWords(void)
{
int count = 0;
char word[15];
while (scanf("%s", word)!=EOF)
{
strcpy(wordList[count].structword, word);
count++;
}
}
-
Why not printing the puzzle like this:
Code:
for (y=0 ; y<DIM ; ++y) {
for (x=0 ; x<DIM ; x++)
printf("%c ", puzzle[x][y]);
printf("\n");
}
btw, I suggest cleanning your code up before posting in the forum, makes it helluva easier to make out what youre trying to do.
-
The way I see it, the compiler doesnt proceed after readingfrom the external word list file and copying its contents into the array of structures. I could print the contents of the structures if I put a print command. But nothing happens after that.
Its difficult to format (indent tabs, etc) the code in this Forum, its displays in a different format when I post a codee.
-
Code:
int l = strlen(array);
strlen() returns a size_t, and you might want to consider another name for that variable. l (lowercase L, 12th letter in the alphabet) looks too much like 1 (digit one) in my opinion.
[edit]
And declaring variables in the middle of blocks is C++/C99:
Code:
int main()
{
char array[DIM] ;
int x, y, w;
int type=0;
int c = 0, r = 0;
readWords();
int numb =0;
int count;
[/edit]