3차배열 동적할당

Software / / 2013. 12. 21. 08:12
#include 
#include 

int width,height;

int*** make()
{
	int ***arr = NULL;
	int x,y,z;
	//***********************************************배열만들기
	printf("==============1:배열만들기==============\n");
	printf("x:");
	scanf("%d",&width);
	arr = (int ***)malloc(sizeof(int**) * width);

	printf("y:");
	scanf("%d",&height);
	for( x = 0 ; x < width ; x++)
	{
		arr[x] = (int **)malloc(sizeof(int*) * height);
	}

		printf("z:%d\n",3);

	for(x=0 ; x < width ; x++)
	{
		for (y=0 ; y < height ;y++)
		{
			arr[x][y] = (int *)malloc(sizeof(int) * 3);
		}
	}

	//***********************************************배열만들기
	
	//***********************************************배열안에0채워넣기
	for (z = 0 ; z < 3 ; z++)
	{
		for (y = 0 ; y < height ; y++)
		{
			for (x = 0 ; x < width ; x++)
			{
				arr[x][y][z] = 0;
			}
		}
	}
	//***********************************************배열안에0채워넣기

	//***********************************************[0]프린트
	for (z = 0 ; z<3 ; z++)
	{
		printf("z축:%d\n",z);
		for (y = 0 ; y < height ; y++)
		{
			for (x = 0 ; x < width ; x++)
			{
				printf("[%d]",arr[x][y][z]);
			}
			printf("\n");
		}
		printf("\n");
	}
	//***********************************************[0]프린트
	return arr;
}

void print()
{
	int x,y,z;
	printf("==============2:[x y z]프린트==============\n");
	for (z= 0 ; z<3 ; z++)
	{
		printf("z축:%d\n",z);
		for (y = 0 ; y < height ; y++)
		{
			for (x = 0 ; x < width ; x++)
			{
				printf("[%d %d %d] ",x,y,z);
			}
			printf("\n");
		}
		printf("\n");
	}
}

int*** sum(int ***arr)
{
	int x,y,z,sum;
	printf("==============3:[합]==============\n");
	//***********************************************sum값 배열에 넣기
	for (z = 0 ; z < 3 ; z++)
	{
		for (y = 0 ; y < height ; y++)
		{
			for (x = 0 ; x < width ; x++)
			{
				sum = x + y + z;
				if (sum%2 == 0)	sum = 2;//짝수
				else			sum = 1;//홀수
				arr[x][y][z] = sum;
			}
		}
	}
	//***********************************************sum값 배열에 넣기

	//***********************************************sum값 출력
	for (z= 0 ; z < 3 ; z++)
	{
		printf("z축:%d\n",z);
		for (y = 0 ; y < height ; y++)
		{
			for (x = 0 ; x < width ; x++)
			{
				printf("[%d] ",arr[x][y][z]);
			}
			printf("\n");
		}
		printf("\n");
	}
	//***********************************************sum값 출력
	return arr;
}

void quit(int ***arr)
{
	int x,y;

	for (x = 0 ; x < width ; x++)
	{
		for(y = 0 ; y < height ; y++)
		{
			free(arr[x][y]);
		}
	}


	for(x = 0 ; x < width ; x++)
	{
		free(arr[x]);
	}

	free(arr);
}

void main()
{
	int ch=0;
	int*** arr_ad = NULL;

	while(ch != 4)
	{
		printf("==============\n");
		printf("1:배열만들기\n");
		printf("2:[x y z]프린트\n");
		printf("3:[합]\n");
		printf("4:종료\n");
		printf("==============\n");
		printf("메뉴선택:");
		scanf("%d",&ch);
		fflush(stdin);
		switch(ch)
		{
		case 1:
			arr_ad = make();
			break;

		case 2:
			print();
			break;

		case 3:
			arr_ad = sum(arr_ad);
			break;

		case 4:
			quit(arr_ad);
		}
	}
}
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기