0

I have been working on this program more than a week and even started once again from the beginning. I created init() function and draw() function in a separate file (they both are in main function there) and they work perfectly there, but when I put them in fifteen.c the array won't initialize (all values are 0) and it prints out underscore because of the condition in my draw() function. I just don't understand what changes, why it works in my original file and not in fifteen.c file.

here's the link to the fifteen.c https://gist.github.com/anatkachen/7d6b1695a95fe52b4d679fd3a51fec18

here's my code from original file

int main (int argc, string argv[])
{
if (argc != 2)
{
printf("Command-lineargument is not valid. Rerun the program. Type in the key.\n");
return 1;
}

if (argv[1] == NULL)
{
printf("error");
return 2;
}


int d = atoi (argv[1]);

if( d < 3 || d > 9 )
{
    printf ("the number is wrong");
}

int tilevalue = d*d - 1;
int board [d][d];
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        // printing __ instead of 0 for the last tile
         if ( j == d - 1 && i == d - 1)
            {
               board[i][j] = 0;
             }

        // switching 1 and 2 for even user imput
         else if ( i == d - 1 && j == d - 3)
               {
                   if (d % 2 == 0)
                   {
                   board[i][j] = 1;
                   }

                   else
                   {
                    board[i][j] = tilevalue;
                    tilevalue = tilevalue - 1;
                   }
                }

         else if ( i == d - 1 && j == d - 2)
               {
                   if (d % 2 == 0)
                   {
                   board[i][j] = 2;
                   }
                   else
                   {
                    board[i][j] = tilevalue;
                    tilevalue = tilevalue - 1;
                   }
                }

         //setting value to tiles and printing them out
          else
                {
                     board[i][j] = tilevalue;
                     tilevalue = tilevalue - 1;
                }
    }

}
printf("\n");

for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
    if (board[i][j] == 0)
    {
        printf (" _ ");
    }
    else
    {
    printf ("%2i ", board[i][j]);
    }

} 
printf("\n");
}

return 0;
}
1

board[][] is a global variable. When you redeclared it inside of the init() function, you created what is called a shadow variable. A shadow variable is a variable that is created with local scope that masks one with greater scope, such as a var outside a currently active loop or a global variable.

When the code exits init(), the local board[][] array is destroyed and the global array is unmasked and available for use again, but still contains the same values it had before it was masked or hidden - in this case, all 0's.

Don't redeclare it locally. Remove the line int board [d][d];.

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 .