0

so the line of code below gives me the output of:

  • 2|3|-4
  • -3|-2|-1
  • 8|-7|95

Can anyone see what I ve done wrong? Thank you.

void init(void)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            board[i][j] = d^2 - i*d - j - 1; 
            if (i + 1 == d && j + 1 == d)
                board[i][j] = 95; 
        }
    }
    if ((d^2 - 1) % 2 != 0)
    {
        int temp = board[d-1][d-2];
        board[d-1][d-2] = board[d-1][d-3];
        board[d-1][d-3] = temp;
    }
}
  • So I got it to work by changing d^2 to d*d. Can anyone please explain why the former is wrong? Also how do I make the underscore symbol using int? I think I can only pass numeric values to int, and since board is an integer variable, how do I make it to show "_"? – Martin Wzatek Mar 21 '16 at 11:45
  • Solved the underscore thing thanks to the function that follows initialize(), draw(). So my only question now is why d^2 can not be used. – Martin Wzatek Mar 21 '16 at 12:28
0

The ^ operator is not an exponent operator as it is in traditional mathematics. It is the BITWISE XOR operator. You weren't actually squaring d. There is no exponent operator. The closest thing to this is the pow(a,b) function ( #include ) that returns a to the power of b.

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

  • within Main(), below this comment // log the current state of the board (for testing), variables i and j are defined. for init() to work, why the local variables has to be the same i,j for the "for" loop? why not any different variables be used? different variables were used in the init(), but it gives segmentation fault. – aguy01 Jun 11 '16 at 22:27
  • Not sure what you're asking, but it looks like a whole new question. If so, please post it as a new question, with details and code included. – Cliff B Jun 12 '16 at 2:15

You must log in to answer this question.

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