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!

## vicky_dev 12

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

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

## vicky_dev 12

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...