0

I'm receiving a segementation fault with the code below. I've played with the code a little bit. I'm assuming there are problems in the move and won functions as well, but I'm unable to check them, given the two errors below.

:( init initializes 3x3 board correctly
 \ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 276..."
:( init initializes 4x4 board correctly
 \ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 276..."

Here is the relevant code below.

void init(void)
{
//tells the program how many slots to create, partial credit to Craig Rodrigues for parts of the code
 int slots = (d*d) - 1;

 for (int i = 0; i < d; i++)
 {
   for (int j = 0; j < d; j++)
   {
     board[i][j] = slots;
     slots -= 1;
   }

 }

 //Is the number of the respective tiles odd? If so, switch them
 if (slots % 2 == 0)
 {
    int replacement = board[d-1][d-2];
    board[d-1][d-2] = board[d-1][d-3];
    board[d-1][d-3] = replacement;
 }
}

Okay, it's also quite possible that the error is in draw, and not init. I'll update the code by showing my draw code, just in case you guys see anything.

void draw(void)
{
 //Print the board, replacing the zero with a blank tile
 for (int i = 0; i < d; i++)
 {
    for (int j = 0; j < d; i++)
    {
        if (board[i][j] == 0)
        {
            printf(" [] ");
        }
        else
        {
            printf("[%d]", board[i][j]);
        }
    printf("\n");
    }
 }
}

I'm going add one last thing here: https://sandbox.cs50.net/checks/cd3fae3fe2e44b6681be5a12e70ca53a

UPDATE: After some playing around with the program, I've found that this actually triggers a segmentation fault. I'm still unaware of how to fix this though, but hey, at least I found the reason for the error! (finally...)

7
  • Your code is generating an error, could you please mark the line in which the error occurs. – dLopez Aug 6 '16 at 18:59
  • I'm afraid I can't, because while check50 is telling me that there is an error, it's not quite telling me where the error actually is. For that reason, I'm unable to mark where the error occurs. – jayfeather31 Aug 6 '16 at 19:02
  • It seems you are not assigning a value to your blank tile. – dLopez Aug 6 '16 at 19:07
  • Okay, thanks for the help... though I'm unsure how to implement that. – jayfeather31 Aug 6 '16 at 19:18
  • if you want to pass all check50 tests you need to use 0 as value for the blank tile – dLopez Aug 6 '16 at 19:24
0

per specs, when the board is a multiple of 4 must swap the 1 with 2, you say

int slots = (d*d) - 1;
.....
 if (slots % 2 == 0)
....

let x = 4 then d * d - 1 = 15, which is not a multiple of 4, you probably already know what your mistake.

EDIT:

as already noted the value of d * d must be 0, this is easy with an additional condition if your loops, make sure that the values really are swapped, a literal permutation is not necessary we can make a direct assignment

4
  • Changing that line of code that didn't do anything. I'm still receiving an error. Are there any other problems you see here? – jayfeather31 Aug 6 '16 at 19:25
  • you can read my edition – MARS Aug 6 '16 at 19:57
  • I read the edition, and just to clarify, are you asking me to go ahead and change the (d*d) -1 to simply d * d, along with taking the step of adding another condition? – jayfeather31 Aug 6 '16 at 20:11
  • just change, no need to add another condition – MARS Aug 6 '16 at 21:27

You must log in to answer this question.

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