i m making a program which initializes a 2d Dynamic array by first asking for the number of rows and columns,and then filling the array with random characters.
Then i have to check for a three lettered array in the 2D array,(Horizontally,Vertically,Diagonally).
i m having problem with the conditions for checking the elements.
i have made conditions for checking the elements,which are:

``````for(int q=0;q<r;q++)
{
for(int p=0;p<c;p++)
{
if(a[q][p]==b[0]) //vertical
{
if (a[q+1][p]==b[1])
{
if (a[q+2][p]==b[2])
cout << endl << "Found at ("<< q << "," << p << ") (" << q+1 << "," << p << ") (" << q+2 <<"," << p << ")"<< endl;
}
}

if(a[q][p]==b[0]) //row
{
if (a[q][p+1]==b[1])
{
if (a[q][p+1]==b[2])
cout << endl << "Found at ("<< q << "," << p << ") (" << q << "," << p+1 << ") (" << q <<"," << p+2 << ")"<< endl;
}
}

if(a[q][p]==b[0]) //diagonal
{
if (a[q+1][p+1]==b[1])
{
if (a[q+2][p+2]==b[2])
cout << endl << "Found at ("<< q << "," << p+1 << ") (" << q << "," << p+1 << ") (" << q +2<<"," << p+2 << ")"<< endl;
}
}
}
}``````

r==number of rows,c==number of cloumns;
b[3] is the array to be compared with the elements of the array.

i cannot think of the logic,when the program checks certain element and when it does not!

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
``````const int WORD_LEN = 3;

for(int q=0;q<r;q++)
{
for(int p=0;p<c;p++)
{
``````

Edited by mike_2000_17: Fixed formatting

You could go along these lines...

``````const int WORD_LEN = 3;

for(int q=0;q<r;q++)
{
for(int p=0;p<c;p++)
{
if( q < r-WORD_LEN) //vertical
{
// Check only if a word can be matched, this will avoid the array out-of-bound problem also
if( a[q][p]==b[0] && a[q+1][p]==b[1] && a[q+2][p]==b[2] )
{
// Matched...``````

And similar logic for horizontal and diagonal...

it does not help!
what i want to know is that how should i check the conditions,how would i check conditions horizontally,diagonally and vertically?

what you have done is that u took the if statements from inside and made it a single one,its equivalent to mine.

and what should be the last value that i should check in loop????

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.