0

Can someone please tell me is this code for the won function works ? And if not , why?

   int counter=0;
for(int line=1; line<=d;line++)
{
    for(int column=1;column<=d;column++)
    {
        if(board[line][column]!=(line-1)*d+column)
        {
          counter ++;  
        }
    }
}
if(counter==(d-1) && board[d][d]==0)
    return true;
else
return false;

}

Thank you a lot

1

There are several issues. First, the coordinates for board[][] start at [0][0], not [1][1], so you are not processing the top row or the left column. Similarly, by checking <=d, you're looking at invalid array locations off the right side of the board or below the bottom row when line or column actually equals d. Remember, an array with n elements in a dimension runs from 0 to n-1, not 1 to n.

After that, both checks in the final if statement are bad. counter can max out at (d*d)-1 and the limits on the board array are both d-1, not d. There may be other issues, but these are significant enough.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .