wicwik wicwik - 14 days ago 5
C Question

My program crashes while comparing elements in array

Hi I need help with my program. Part of code does not seem to work as expected:

void up(int pole[4][4])
{

int i,j;

for (i = 3; i >= 1; i--)
{
for (j = 3; j >= 0; i--)
{
if (pole[i][j] == pole[i-1][j])
{
pole[i-1][j] += pole[i][j];
pole[i][j] = 0;
}
}
}

system("cls");

for (i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d ", pole[i][j]);
}
printf("\n");
}
}


when I call the function up in switch like this:

switch(keynumber)
{
case 119: //w
up(base);
break;


I just end up with my program crashing.
The reason why I am doing this is that I want to make 2048 game in console so first I created an array of which 2 random elements of the array will be the number 2 and then I compare these elements depending on which key you will push.

Answer
for (i = 3; i >= 1; i--)

{

   for (j = 3; j >= 0; i--)  // Why here is i-- ?? instead if j-- 
       {
          if (pole[i][j] == pole[i-1][j])
            {
             pole[i-1][j] += pole[i][j];
             pole[i][j] = 0;
             }
        }

}
When the second loop is breaking?

Comments