wicwik
wicwik

Reputation: 33

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.

Upvotes: 2

Views: 81

Answers (2)

user6794537
user6794537

Reputation:

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?

Upvotes: 2

user7069084
user7069084

Reputation:

Refer line number 8, Instead of

    for (j = 3; j >= 0; i--)

it should be

    for (j = 3; j >= 0; j--)

As value of j isn't decrementing, it is an infinite loop.

Upvotes: 0

Related Questions