C언어_배열, 포인터

2024. 9. 26. 18:00폴리텍_하이테크_AI소프트웨어/C언어

1. 배열을 이용한 평균 계산

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int score[5];
    int sum = 0;

    printf("첫번째 학생의 시험점수를 입력하시요: ");
    scanf("%d", &score[0]);
    printf("두번째 학생의 시험점수를 입력하시요: ");
    scanf("%d", &score[1]);
    printf("세번째 학생의 시험점수를 입력하시요: ");
    scanf("%d", &score[2]);
    printf("네번째 학생의 시험점수를 입력하시요: ");
    scanf("%d", &score[3]);
    printf("다섯번째 학생의 시험점수를 입력하시요: ");
    scanf("%d", &score[4]);

    for (int i = 0; i < 5; i++) {
        sum = sum + score[i];
    }
    printf("5명 학생의 평균은 %.0f입니다.", sum / 5.0);
    return 0;
}

2. 배열 복사

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int a[3][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    int b[15];
    int z = 0;

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 5; j++) {
            printf("%d ", a[i][j]);
            b[z] = a[i][j];
            z += 1;
        }
        printf("\n");
    }

    printf("\n-----------------------\n\n");
    for (int i = 0; i < 15; i++) {
        printf("%d ", b[i]);
    }

    return 0;
}

 

3. 문자열 배열 사용 예제 ('' 사용)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    char s1[12] = { 'H', 'I', ',', ' ', 'm', 'i', 'n', 'h', 'o', '!', '\0' };

    for (int i = 0; i < 12; i++) {
        printf("%c", s1[i]);
    }

    return 0;
}

4. 문자열 배열 사용 예제 ("" 사용)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    char s1[12] = "Hi, Minho!";

    for (int i = 0; i < 12; i++) {
        printf("%c", s1[i]);
    }

    return 0;
}

5. 배열 크기를 자동 계산

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    char s1[] = "Hi, Minho";
    printf("%s", s1);
    return 0;
}

6. strcpy 함수 사용

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

int main() {
    char str1[80] = "apple";
    char str2[80];

    strcpy(str2, str1);
    strcpy(str1, "Banana");

    printf("str1: %s \nstr2: %s", str1, str2);
    return 0;
}

7. gets와 puts 함수 사용

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    char str1[80];

    printf("문자열 입력: ");
    gets(str1);
    puts(str1);

    return 0;
}

8. 문자열 두 개 입력받고 교환

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

int main() {
    char str1[80], str2[80], temp[80];

    printf("문자열 입력: ");
    scanf("%s %s", str1, str2);
    printf("입력된 문자열(바꾸기 전): %s, %s\n", str1, str2);

    strcpy(temp, str1);
    strcpy(str1, str2);
    strcpy(str2, temp);

    printf("입력된 문자열(바꾸기 후): %s, %s", str1, str2);

    return 0;
}

9. 학생 키 입력과 평균 계산 (실습 1)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int height[5];
    int sum = 0;

    for (int i = 0; i < 5; i++) {
        printf("%d번째 학생의 키를 입력하세요: ", i + 1);
        scanf("%d", &height[i]);
    }

    printf("학생들의 키는 아래와 같습니다.\n");
    for (int i = 0; i < 5; i++) {
        printf("%d번째 학생의 키는 %d입니다.\n", i + 1, height[i]);
        sum += height[i];
    }

    printf("5명 학생의 평균 키는 %.2f입니다.", sum / 5.0);
    return 0;
}

10. 최고 성적 찾기 (실습 2)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int score[5];
    int max = 0;

    for (int i = 0; i < 5; i++) {
        printf("%d번째 학생의 성적을 입력하세요: ", i + 1);
        scanf("%d", &score[i]);
        if (max < score[i])
            max = score[i];
    }

    for (int i = 0; i < 5; i++) {
        if (max == score[i])
            printf("최고 성적은 %d번째 성적인 %d입니다.\n", i + 1, max);
    }
    return 0;
}

11. 최저 성적 찾기 (실습 3)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int score[5];
    int min = 100;

    for (int i = 0; i < 5; i++) {
        printf("%d번째 학생의 성적을 입력하세요: ", i + 1);
        scanf("%d", &score[i]);
        if (min > score[i])
            min = score[i];
    }

    for (int i = 0; i < 5; i++) {
        if (min == score[i])
            printf("최저 성적은 %d번째 성적인 %d입니다.\n", i + 1, min);
    }
    return 0;
}

12. 주사위 게임 (실습 4)

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

int main() {
    srand(time(NULL));
    int dice[3];
    int count = 0;

    while (1) {
        dice[0] = rand() % 6 + 1;
        dice[1] = rand() % 6 + 1;
        dice[2] = rand() % 6 + 1;
        count++;

        if (dice[0] == dice[1] && dice[1] == dice[2]) {
            printf("주사위를 %d번 던졌습니다.\n", count);
            printf("주사위 숫자는 모두 %d입니다.\n", dice[0]);
            break;
        }
    }
    return 0;
}

13. 가위 바위 보 게임 (실습 5)

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

int main() {
    srand(time(NULL));
    int game, count = 0, win = 0, lose = 0, draw = 0;
    char str1[3][10] = { "가위", "바위", "보" };

    while (1) {
        printf("**************** 가위 바위 보 게임 ****************\n");
        printf("1. 가위 _ 2. 바위 _ 3. 보 _ 0. 종료\n");
        printf("입력해주세요: ");
        scanf("%d", &game);
        int com = rand() % 3 + 1;

        if (game == 0) {
            printf("종료합니다.\n");
            break;
        } else if (game < 1 || game > 3) {
            printf("잘못 입력했습니다.\n\n");
            continue;
        }

        count++;
        printf("컴퓨터 : %s\n", str1[com - 1]);
        printf("사용자 : %s\n", str1[game - 1]);

        if (game == com) {
            printf("비겼습니다.\n");
            draw++;
        } else if ((game == 1 && com == 3) || (game == 2 && com == 1) || (game == 3 && com == 2)) {
            printf("이겼습니다.\n");
            win++;
        } else {
            printf("졌습니다.\n");
            lose++;
        }
        printf("%d전 %d승 %d무 %d패\n\n", count, win, draw, lose);
    }
    return 0;
}

14. 포인터와 주소값 출력

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int a = 5;
    int* ap = &a;

    printf("a의 값: %d\n", a);
    printf("a의 주소값: %p\n", (void*)&a);
    printf("ap의 값(주소값): %p\n", (void*)ap);
    printf("ap가 가리키는 값: %d\n", *ap);

    *ap = 10;
    printf("a의 값: %d\n", *ap);
    return 0;
}

15. 포인터로 값 변경

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
	char a = 'A';
	char* b = &a;
	printf("현재 a의 값은 %c\n", a);
	*b = 'B';
	printf("변경된 a의 값은 %c\n", a);
	printf("b의 값은 %d\n", b);
	return 0;
}

 

16. 포인터를 이용한 값 교환 (swap 함수)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void swap(int* x, int* y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a = 3;
    int b = 5;

    printf("a의 값: %d, b의 값: %d\n", a, b);
    swap(&a, &b);
    printf("교환 후 a의 값: %d, b의 값: %d\n", a, b);
    return 0;
}

17. 포인터를 사용한 2배 연산

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void twice(int* a) {
    *a = 2 * (*a);
}

int main() {
    int num;
    printf("수를 입력하세요: ");
    scanf("%d", &num);

    twice(&num);
    printf("입력한 수의 2배는 %d입니다.\n", num);
    return 0;
}

18. 포인터를 이용한 내림차순 정렬

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void sort(int* a, int* b, int* c) {
    int temp;

    if (*a < *b) {
        temp = *a;
        *a = *b;
        *b = temp;
    }
    if (*a < *c) {
        temp = *a;
        *a = *c;
        *c = temp;
    }
    if (*b < *c) {
        temp = *b;
        *b = *c;
        *c = temp;
    }
}

int main() {
    printf("수 3개를 차례로 입력하세요\n");
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    sort(&a, &b, &c);
    printf("정렬 결과는 %d, %d, %d입니다.\n", a, b, c);
    return 0;
}

 

'폴리텍_하이테크_AI소프트웨어 > C언어' 카테고리의 다른 글

C언어_포인터  (2) 2024.10.10
버블정렬, 선택정렬  (0) 2024.10.04
C언어_for, while, do while  (0) 2024.09.19
C언어_04 if, switch  (0) 2024.09.12
C언어_03 관계, 논리, 비교, 비트 연산  (0) 2024.09.05