1 minute read

22.9.26 C프로그래밍응용 수업에서 나온 과제입니다.

* 모양을 이용하여 본인의 이름을 출력하는 문제입니다.

naive ver) code

#include <stdio.h>

int main()
{
    int data[10][10] = {

		{0,0,0,0,0,0,0,0,1,0},

		{1,1,1,1,1,1,0,0,1,0},

		{0,0,0,0,1,0,0,0,1,0},

		{0,0,0,1,0,0,1,1,1,0},

		{0,1,0,0,1,0,0,0,1,0},

		{1,0,0,0,0,1,0,0,1,0},

		{0,0,1,1,1,1,1,1,0,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,0,1,1,1,1,1,1,0,0} };



	int data1[10][10] = {

		{0,0,1,1,1,1,1,1,0,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,0,1,1,1,1,1,1,0,0},

		{0,0,0,0,0,0,0,0,0,0},

		{1,1,1,1,1,1,1,1,1,1},

		{0,0,1,0,0,0,0,1,0,0},

		{0,0,1,0,0,0,0,1,0,0},

		{0,0,1,0,0,0,0,1,0,0},

		{0,0,0,0,0,0,0,0,0,0} };



        int data2[10][10] = {

		{0,0,1,1,0,0,0,0,0,1},

		{1,1,1,1,1,1,0,0,0,1},

		{0,0,0,0,1,0,0,0,0,1},

		{0,0,0,1,0,0,1,1,1,1},

		{0,1,0,0,1,0,0,0,0,1},

		{1,1,1,1,1,1,1,1,1,1},

		{0,0,0,0,0,0,0,0,0,1},

		{1,1,1,1,1,1,1,1,1,1},

		{1,0,0,0,0,0,0,0,0,0},

		{1,1,1,1,1,1,1,1,1,1} };


	int i, j;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (data[i][j] == 1) printf("*");
			else printf(" ");
		}
		printf("\n");
	}

	printf("\n\n");
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (data1[i][j] == 1) printf("*");
			else printf(" ");
		}
		printf("\n");
	}

    printf("\n\n");
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (data2[i][j] == 1) printf("*");
			else printf(" ");
		}
		printf("\n");
	}
}

Use Function

출력하는 부분이 반복되므로 이는 함수를 만들어서 더 간결하게 표현이 가능합니다. 함수를 만들어보면 다음과 같습니다.

void disp(int *dat)
{
    int point;
    int i, j;
    point = 0;

    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            if(*(dat+point) == 1) printf("*");
            else printf(" ");
            point ++;
        }
        printf("\n");
    }
    printf("\n\n");
}

Smart ver) code

포인터를 응용하여 함수를 만들어보았습니다.

이제 이름을 * 모양으로 출력하는 코드를 전체적으로 다시 구현해보면 다음과 같습니다.

#include <stdio.h>

void disp(int * dat)
{
    int point;
    int i, j;
    point = 0;

    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            if(*(dat+point) == 1) printf("*");
            else printf(" ");
            point ++;
        }
        printf("\n");
    }
    printf("\n\n");
}

int main()
{
    int data[10][10] = {

		{0,0,0,0,0,0,0,0,1,0},

		{1,1,1,1,1,1,0,0,1,0},

		{0,0,0,0,1,0,0,0,1,0},

		{0,0,0,1,0,0,1,1,1,0},

		{0,1,0,0,1,0,0,0,1,0},

		{1,0,0,0,0,1,0,0,1,0},

		{0,0,1,1,1,1,1,1,0,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,0,1,1,1,1,1,1,0,0} };



	int data1[10][10] = {

		{0,0,1,1,1,1,1,1,0,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,1,0,0,0,0,0,0,1,0},

		{0,0,1,1,1,1,1,1,0,0},

		{0,0,0,0,0,0,0,0,0,0},

		{1,1,1,1,1,1,1,1,1,1},

		{0,0,1,0,0,0,0,1,0,0},

		{0,0,1,0,0,0,0,1,0,0},

		{0,0,1,0,0,0,0,1,0,0},

		{0,0,0,0,0,0,0,0,0,0} };



        int data2[10][10] = {

		{0,0,1,1,0,0,0,0,0,1},

		{1,1,1,1,1,1,0,0,0,1},

		{0,0,0,0,1,0,0,0,0,1},

		{0,0,0,1,0,0,1,1,1,1},

		{0,1,0,0,1,0,0,0,0,1},

		{1,1,1,1,1,1,1,1,1,1},

		{0,0,0,0,0,0,0,0,0,1},

		{1,1,1,1,1,1,1,1,1,1},

		{1,0,0,0,0,0,0,0,0,0},

		{1,1,1,1,1,1,1,1,1,1} };

    disp(&data[0][0]);
    disp(&data1[0][0]);
    disp(&data2[0][0]);
}

끝~

Leave a comment