what is the problem? i think there's no logic problem or compile error
but the loop suddenly stopped after looping 16 times...
is this gcc compiler's error? i almost freaked out ...
like there's no compile error that pops up but doesn't work,,,
thanks in advance : ) sorry for the editing question
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define RIGHT 0
#define DOWN 1
#define LEFT 2
#define UP 3
int main()
{
int x,y;
scanf("%d %d", &x, &y);
int **arr;
arr = (int**)malloc(sizeof(int*)*x);
for(int i = 0; i < x; i++) arr[i] = (int*)malloc(sizeof(int) * y );
int x_ = 0, y_ = 0;
int MAX_x = x-1;
int MAX_y = y-1;
int MIN_x = 0;
int MIN_y = 0;
int direction = RIGHT;
int num = 0;
while(true)
{
arr[x_][y_] = num++;
printf("%d", num);
//printf("%d", arr[x_][y_]);
switch ( direction )
{
case RIGHT:
if( y_ == MAX_y )
{
direction = DOWN; MIN_x ++; x_++;
}
else
{
y_++;
}
break;
case DOWN:
if( x_ == MAX_x )
{
direction = LEFT; MAX_y --; y_--;
}
else
{
x_++;
}
break;
case LEFT:
if( y_ == MIN_y )
{
direction = UP; MAX_x --; x_++;
}
else
{
y_--;
}
break;
case UP:
if( x_ == MIN_x )
{
direction = RIGHT; MIN_y ++; y_++;
}
else
{
x_++;
}
break;
default : break;
}
if(num == x * y )
break;
}
for(int i = 0; i < x; i ++)
{
for(int j = 0; j < y; j ++ )
{
printf("%3d", arr[i][j]);
}
printf("\n");
}
}