Algorithm/C

쉽게 풀어 쓴 C언어 Express 10장 프로그래밍 14번 솔루션 답지

Printemp 2021. 10. 24.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 21

void display(int x[][SIZE]);

int main()
{
	srand((unsigned)time(NULL));
	int walk[SIZE][SIZE] = { 0 };
	int i=0, n,init,number,a=11,b=11;
	
	while (i < 11)
	{
		walk[a][b] = 1;
		number = rand() % 8;
		switch (number)
		{
			case 0:
				a += 1;
				break;
			case 1:
				a += 1;
				b += 1;
				break;
			case 2:
				b += 1;
				break;
			case 3:
				a -= 1;
				b += 1;
				break;
			case 4:
				a -= 1;
				break;
			case 5:
				a -= 1;
				b -= 1;
				break;
			case 6:
				b -= 1;
				break;
			case 7:
				a += 1;
				b -= 1;
				break;
		}
		
		i++;
	}
	display(walk);
}

void display(int x[][SIZE])
{
	int i, n;
	for (i = 0; i < SIZE; i++)
	{
		for (n = 0; n < SIZE; n++)
		{
			if (x[i][n] == 1)
				printf("*");
			else
				printf(".");
		}
		printf("\n");
	}
}

댓글

💲 추천 글