공부/윤성우의 열혈 C 프로그래밍

윤성우의 열혈 C 프로그래밍 Chapter20 도전! 프로그래밍 3 정답

셩잇님 2020. 8. 17. 22:44
반응형

- 도전 1

길이가 4 x 4인 int형 2차원 배열을 선언하고, 배열의 모든 요소를 오른쪽 방향으로 90도씩 이동시켜서 그 결과를 출력하는 프로그램을 작성해 보자.

 

더보기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void RotateArray(int(*x1)[4]);
void ShowArray(int(*x1)[4]);

int main(void) {

    //Problem 1
    int Array[4][4] = {
        { 1, 2, 3, 4},
        { 5, 6, 7, 8},
        { 9, 10, 11, 12},
        { 13, 14, 15, 16}
    };

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            printf("%d ", Array[i][j]);
        }
        printf("\n");
    }
    printf("\n");

    for (int i = 0; i < 3; i++)
    {
        RotateArray(Array);
        ShowArray(Array);
    }
}

void RotateArray(int(*x1)[4]) {

    int Temp[4][4];

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            Temp[j][3 - i] = x1[i][j];
        }
    }

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            x1[i][j] = Temp[i][j];
        }
    }
}

void ShowArray(int(*x1)[4]) {

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            printf("%d ", x1[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

 

- 도전 2

달팽이 배열을 만들어서 이를 출력하는 프로그램을 작성하고자 한다. 프로그램 사용자로부터 하나의 숫자 n을 입력 받아서 n x n의 길이에 해당하는 달팽이 배열을 출력해주는 프로그램을 작성해 보자.

 

더보기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

 

int MakeSnail(int(*ptrArr)[100], int size);
int ShowArray2(int(*ptrArr)[100], int size);

 

int main (void) {

 

    int num;
    int arr[50][50] = { 0 };

    printf("숫자를 입력하시오 : ");
    scanf("%d", &num);

    MakeSnail(arr, num);
    ShowArray2(arr, num);
}

 

int MakeSnail(int(*ptrArr)[100], int size) {

    int i = 0, j = 0;
    int x = 0, y = 0;
    int count = 1, limit = size, turn = 0;
    int plusMinus = +1;

    while (count < (size * size)) {

        for (i = 1; i < limit; i++) {
            ptrArr[x][y] = count;
            y = y + 1 * plusMinus;
            count++;
        }
        turn++;

         if (count >= (size * size)) {
            break;
         }

        for (j = 1; j < limit; j++) {
            ptrArr[x][y] = count;
            x = x + 1 * plusMinus;
            count++;
        }

        plusMinus = plusMinus * (-1);
        turn++;

        if (turn % 4 == 0) {
            limit = limit - 2;
            x++;
            y++;
        }
    }
    ptrArr[x][y] = count;
    return 0;
}

int ShowArray2(int(*ptrArr)[100], int size) {

    int i, j;

    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++)
        {
            printf("%2d ", ptrArr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

 

- 도전 3

0이상 99 이하의 난수를 총 5개 생성하는 프로그램을 작성해보자

 

더보기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

 

int main (void) {

 

    printf("난수의 범위 : 0부터 %d까지 \n", RAND_MAX);
    for (int i = 0; i < 5; i++)
    {
        printf("난수 출력 : %d \n", rand()%100);
    }

}

 

- 도전 4

두 개의 주사위를 던졌을 때의 결과를 출력하는 프로그램을 작성해보자. 물론 그 결과는 예측이 불가능해야 한다.

 

더보기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>

 

int main (void) {

 

    int dice1, dice2;

    srand((int)time(NULL));

    dice1 = (rand() % 6) + 1;
    dice2 = (rand() % 6) + 1;

    printf("주사위 1의 결과 : %d \n", dice1);
    printf("주사위 2의 결과 : %d \n", dice2);

}

 

- 도전 5

가위 바위 보 게임을 만들어 보자. 사용자로부터 가위 바위 보 중에서 하나를 입력 받는다. 그리고 컴퓨터는 난수 생성을 통해서 가위 바위 보 중에서 하나를 선택하게 한다. 이 둘을 비교해서 승자와 패자를 가려주는 프로그램을 작성해 보자. 단 프로그램의 진행은 사용자가 질 때까지 계속되어야 하고, 마지막에 가서는 게임의 결과(예: 4승 3무)까지 출력해 주도록 하자.

 

더보기

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

 

int main (void) {

 

    int User, Computer;
    int Win = 0, Draw = 0;

    srand(time(NULL));

    while (1) {

        printf("바위는 1, 가위는 2, 보는 3: ");
        scanf("%d", &User);
        Computer = rand() % 3 + 1;

        if (Computer == 1) {

            if (User == 1) {

                printf("당신은 바위 선택, 컴퓨터는 바위 선택, 비겼습니다. \n");
                Draw++;
            }

            else if (User == 2) {
                printf("당신은 가위 선택, 컴퓨터는 바위 선택, 졌습니다. \n");
                break;
            }

            else {
                printf("당신은 보 선택, 컴퓨터는 바위 선택, 이겼습니다. \n");
                Win++;
            }
        }

        else if (Computer == 2) {
            if (User == 1) {
                printf("당신은 바위 선택, 컴퓨터는 가위 선택, 이겼습니다. \n");
                Win++;
            }

        else if (User == 2) {
            printf("당신은 가위 선택, 컴퓨터는 가위 선택, 비겼습니다. \n");
            Draw++;
            }

        else {
            printf("당신은 보 선택, 컴퓨터는 가위 선택, 졌습니다. \n");
            break;
            }
        }

        else {
            if (User == 1) {
                printf("당신은 바위 선택, 컴퓨터는 보 선택, 졌습니다. \n");
                break;
            }

        else if (User == 2) {
                printf("당신은 가위선택, 컴퓨터는 보 선택, 이겼습니다. \n");
                Win++;
        }

        else {
                printf("당신은 보 선택, 컴퓨터는 보 선택, 비겼습니다. \n");
                Draw++;
        }

      }
    }
    printf("\n");
    printf("게임의 결과: %d 승, %d 무\n", Win, Draw);

}

 

- 도전 6

친구와 둘이서 숫자 맞추기 게임을 해 본적이 있을 것이다(보통은 야구 게임이라고 불린다). 이것을 컴퓨터와 할 수 있도록 프로그램을 작성해보자.

 

반응형