๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[C] ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - Day 1 ๋ณธ๋ฌธ

Development Study/C

[C] ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - Day 1

f1r3_r41n 2024. 7. 17. 15:51
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - Day 1

ํŽธ์ž… ์ค€๋น„๋กœ ์ธํ•œ ๊ธฐ์ดˆ ๋‹ค์ง€๊ธฐ ๊ธฐ๋ก Start!

๐Ÿค” ๊ธฐ๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

1๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€?

์„ธ ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’

#include <stdio.h>

int main(void) {
    int a, b, c, max;

    printf("์„ธ ์ •์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.\n");
    printf("a: "); scanf("%d", &a);
    printf("b: "); scanf("%d", &b);
    printf("c: "); scanf("%d", &c);

    max = a;
    if(b>max) max = b;
    if(c>max) max = c;
    printf("์„ธ ์ •์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์€ %d์ž…๋‹ˆ๋‹ค.\n", max);

    return 0;
}
  • 3๊ฐœ์˜ ์ •์ˆ˜ ๊ฐ’ ๊ฐ€์šด๋ฐ ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

1. max์— a ๊ฐ’ ๋Œ€์ž…
2. b ๊ฐ’์ด max๋ณด๋‹ค ํฌ๋ฉด max์— b ๊ฐ’ ๋Œ€์ž…
3. c ๊ฐ’์ด max๋ณด๋‹ค ํฌ๋ฉด max์— c ๊ฐ’ ๋Œ€์ž…
  • ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •
    • ์„ธ ๋ฌธ์žฅ์ด ์•„๋ž˜๋กœ ๋‚˜๋ž€ํžˆ ์žˆ๋‹ค๋ฉด ์ด ๋ฌธ์žฅ์€ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋จ
      • ์—ฌ๋Ÿฌ ๋ฌธ์žฅ(ํ”„๋กœ์„ธ์Šค)์ด ์ˆœ์ฐจ ์‹คํ–‰๋˜๋Š” ๊ตฌ์กฐ -> ์ˆœ์ฐจ์ (concatenation) ๊ตฌ์กฐ
    • ์‹์˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๋Š” if๋ฌธ -> ์„ ํƒ(selection) ๊ตฌ์กฐ
    • int max = a;
      • ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ์‹œ์ ์— ๊ฐ’์„ ๋„ฃ๋Š” ์ดˆ๊ธฐํ™”
    • max = a;
      • ์ด๋ฏธ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š” ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋„ฃ๋Š” ๋Œ€์ž…

  • ์ˆœ์„œ๋„(flowchart)

#include <stdio.h>

int max3(int a, int b, int c) {
    int max = a;
    if(b>max) max = b;
    if(c>max) max = c;
    return max;
}

int main(void) {
    printf("max3(%d, %d, %d) = %d\n", 3, 2, 1, max3(3, 2, 1));
    printf("max3(%d, %d, %d) = %d\n", 3, 2, 2, max3(3, 2, 2));
    printf("max3(%d, %d, %d) = %d\n", 3, 2, 3, max3(3, 2, 3));
    printf("max3(%d, %d, %d) = %d\n", 6, 7, 2, max3(6, 7, 2));
    return 0;
}
  • ์„ธ ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌ
    • ํ•จ์ˆ˜ํ˜ธ์ถœ์‹์„ ํ‰๊ฐ€ํ•˜๋ฉด ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ
      • ๋ฐ˜ํ™˜๊ฐ’์˜ ์ž๋ฃŒํ˜•์ด void์ธ ํ•จ์ˆ˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ

์ค‘์•™๊ฐ’

#include <stdio.h>

int med3(int a, int b, int c) {
    if(a>=b)
        if(b>=c) return b;
        else if(a<=c) return a;
        else return c;
    else if(a>c) return a;
    else if(b>c) return c;
    else return b;
}

int main(void) {
    int a, b, c;
    printf("์„ธ ์ •์ˆ˜์˜ ์ค‘์•™๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.\n");
    printf("a: "); scanf("%d", &a);
    printf("b: "); scanf("%d", &b);
    printf("c: "); scanf("%d", &c);
    printf("์ค‘์•™๊ฐ’: %d\n", med3(a, b, c));
    return 0;
}
  • ์ค‘์•™๊ฐ’์„ ๊ตฌํ•˜๋Š” ์ ˆ์ฐจ: ํ€ต ์ •๋ ฌ(Quicksort)

์กฐ๊ฑด ํŒ๋‹จ๊ณผ ๋ถ„๊ธฐ

#include <stdio.h>

int main(void) {
    int n;
    printf("์ •์ˆ˜ ์ž…๋ ฅ: "); scanf("%d", &n);

    // ...
    if(n>0) printf("์–‘์ˆ˜\n"); // 1
    else if(n<0) printf("์Œ์ˆ˜\n"); // 2
    else printf("0\n"); // 3
    // ...

    return 0;
}
  • // ... ๋กœ ์‹ธ์—ฌ์ง„ ๋ถ€๋ถ„์—์„œ ์‹คํ–‰๋˜๋Š” ๋ถ€๋ถ„์€ 1, 2, 3 ์ค‘ ํ•˜๋‚˜๋ฟ์ด๋ฉฐ ๋‘ ๊ฐ€์ง€๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๊ฑฐ๋‚˜ ํ•˜๋‚˜๋„ ์‹คํ–‰๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Œ
    • ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์ด ์„ธ ๊ฐ€์ง€๋กœ ๋ถ„๊ธฐํ•˜๊ธฐ ๋•Œ๋ฌธ

  • ์ˆœ์„œ๋„

์ˆœ์„œ๋„์˜ ๊ธฐํ˜ธ



์—ฐ์‚ฐ์ž์™€ ํ”ผ์—ฐ์‚ฐ์ž

  • ์—ฐ์‚ฐ์ž(operator): ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐํ˜ธ
  • ํ”ผ์—ฐ์‚ฐ์ž(operand): ์—ฐ์‚ฐ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ์‹
  • b > max ์—์„œ ์—ฐ์‚ฐ์ž๋Š” > ์ด๊ณ , b์™€ max๋Š” ํ”ผ์—ฐ์‚ฐ์ž
๋‹จํ•ญ ์—ฐ์‚ฐ์ž(unary operator) ... ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ 1๊ฐœ(a++)
2ํ•ญ ์—ฐ์‚ฐ์ž(binary operator) ... ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ 2๊ฐœ(a<b)
3ํ•ญ ์—ฐ์‚ฐ์ž(ternary operator) ... ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ 3๊ฐœ(a?b:c)

์‹(expression)

๋ณ€์ˆ˜
์ƒ์ˆ˜
๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๋ฅผ ์—ฐ์‚ฐ์ž๋กœ ๊ฒฐํ•ฉํ•œ ๊ฒƒ
  • x=n+135
    • ๋ณ€์ˆ˜ x์™€ n์€ intํ˜•
    • x, n, 135, n+135, x=n+135 ๋Š” ๋ชจ๋‘ ์‹
  • xx์—ฐ์‚ฐ์ž์™€ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฒฐํ•ฉํ•œ ์‹ -> xx์‹
    • ๋Œ€์ž… ์—ฐ์‚ฐ์ž์— ์˜ํ•ด x ์™€ n+135 ๊ฐ€ ๊ฒฐํ•ฉ๋œ ์‹ -> ๋Œ€์ž…์‹(assignment expression)

์‹์˜ ํ‰๊ฐ€

  • ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ๋•Œ ์‹์ด ํ‰๊ฐ€๋จ
  • ๋ชจ๋“  ์ˆ˜์‹์—๋Š” ๊ฐ’์ด ์žˆ์œผ๋ฉฐ ๊ทธ ๊ฐ’์€ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ๋•Œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
    • ํ‰๊ฐ€(evaluation): ์‹์˜ ๊ฐ’์„ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ

๊ด€๊ณ„ ์—ฐ์‚ฐ์ž์™€ ๋“ฑ๊ฐ€ ์—ฐ์‚ฐ์ž

  • ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์˜ ๋Œ€์†Œ ๊ด€๊ณ„๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž
    • <, <=, >, >=
  • ๊ฐ’์ด ๊ฐ™์€์ง€์˜ ๊ด€๊ณ„๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋“ฑ๊ฐ€ ์—ฐ์‚ฐ์ž
    • ==, !=
  • ์ฐธ์ด๋ฉด intํ˜• 1, ๊ฑฐ์ง“์ด๋ฉด intํ˜• 0

๋งค๊ฐœ๋ณ€์ˆ˜(parameter) ๋˜๋Š” ํ˜•์‹ ๋งค๊ฐœ๋ณ€์ˆ˜(foraml parameter)

  • ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜(variable)
  • ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ์‹ค์ธ์ˆ˜(actual argument)

์•Œ๊ณ ๋ฆฌ์ฆ˜(algorithm)

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ, ๋ช…ํ™•ํ•˜๊ฒŒ ์ •์˜๋˜๊ณ  ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์œ ํ•œ ๊ฐœ์˜ ๊ทœ์น™์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ


์„ธ ๊ฐ’์˜ ๋Œ€์†Œ ๊ด€๊ณ„

  • ๊ฒฐ์ • ํŠธ๋ฆฌ(decision tree)
    • ์„ธ ๊ฐ’์˜ ๋Œ€์†Œ ๊ด€๊ณ„์˜ ์กฐํ•ฉ์€ 13๊ฐ€์ง€ ์ข…๋ฅ˜
    • ์กฐํ•ฉ์„ ๋‚˜์—ดํ•œ ๋ชจ์–‘์ด ๋‚˜๋ฌด ํ˜•ํƒœ

์กฐ๊ฑด ์—ฐ์‚ฐ์ž

  • 3๊ฐœ์˜ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๊ฐ–๋Š” 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž(conditional operator) ๋ผ๊ณ  ํ•จ
    • ?, :
  • min a<b?a:b;
    • ๋ณ€์ˆ˜ min์— ๋Œ€์ž…๋˜๋Š” ๊ฐ’์€ a๊ฐ€ b๋ณด๋‹ค ์ž‘์œผ๋ฉด a, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด b

์—ฐ์Šต๋ฌธ์ œ

Q1. ๋„ค ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ max4๋ฅผ ์ž‘์„ฑํ•˜๋ผ

Q2. ์„ธ ๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” min3 ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ

Q3. ๋„ค ๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” min4 ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ

#include <stdio.h>

int max4(int a, int b, int c, int d) {
    int max = a;
    if(b>max) max = b;
    if(c>max) max = c;
    if(d>max) max = d;
    return max;
}

int min3(int a, int b, int c) {
    int min = a;
    if(b<min) min = b;
    if(c<min) min = c;
    return min;
}

int min4(int a, int b, int c, int d) {
    int min = a;
    if(b<min) min = b;
    if(c<min) min = c;
    if(d<min) min = d;
    return min;
}

int main(void) {
    printf("max4(%d, %d, %d, %d) = %d\n",
    15, 18, 4, 7, max4(15, 18, 4, 7));
    printf("min3(%d, %d, %d) = %d\n",
    15, 18, 4, min3(15, 18, 4));
    printf("min4(%d, %d, %d, %d) = %d\n",
    15, 18, 4, 7, min4(15, 18, 4, 7));
    return 0;
}

Q4. ์„ธ ๊ฐ’์˜ ๋Œ€์†Œ ๊ด€๊ณ„ 13์ข…๋ฅ˜์˜ ๋ชจ๋“  ์กฐํ•ฉ์— ๋Œ€ํ•ด ์ค‘์•™๊ฐ’์„ ๊ตฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

#include <stdio.h>

int med3(int a, int b, int c) {
    if(a>=b)
        if(b>=c) return b;
        else if(a<=c) return a;
        else return c;
    else if(a>c) return a;
    else if(b>c) return c;
    else return b;
}

int main(void) {
    int a, b, c;
    printf("med3(%d, %d, %d) = %d\n", 3, 2, 1, med3(3, 2, 1)); // a>b>c
    printf("med3(%d, %d, %d) = %d\n", 3, 2, 2, med3(3, 2, 2)); // a>b=c
    printf("med3(%d, %d, %d) = %d\n", 3, 1, 2, med3(3, 1, 2)); // a>c>b
    printf("med3(%d, %d, %d) = %d\n", 3, 2, 3, med3(3, 2, 3)); // a=c>b
    printf("med3(%d, %d, %d) = %d\n", 2, 1, 3, med3(2, 1, 3)); // c>a>b
    printf("med3(%d, %d, %d) = %d\n", 3, 3, 2, med3(3, 3, 2)); // a=b>c
    printf("med3(%d, %d, %d) = %d\n", 3, 3, 3, med3(3, 3, 3)); // a=b=c
    printf("med3(%d, %d, %d) = %d\n", 2, 2, 3, med3(2, 2, 3)); // c>a=b
    printf("med3(%d, %d, %d) = %d\n", 2, 3, 1, med3(2, 3, 1)); // b>a>c
    printf("med3(%d, %d, %d) = %d\n", 2, 3, 2, med3(2, 3, 2)); // b>a=c
    printf("med3(%d, %d, %d) = %d\n", 1, 3, 2, med3(1, 3, 2)); // b>c>a
    printf("med3(%d, %d, %d) = %d\n", 2, 3, 3, med3(2, 3, 3)); // b=c>a
    printf("med3(%d, %d, %d) = %d\n", 1, 2, 3, med3(1, 2, 3)); // c>b>a
    return 0;
}

Q5. ์ค‘์•™๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Œ. ๊ทธ๋Ÿฌ๋‚˜ med3.c์—์„œ์˜ med3 ํ•จ์ˆ˜์— ๋น„ํ•ด ํšจ์œจ์ด ๋–จ์–ด์ง€๋Š”๋ฐ, ๊ทธ ์ด์œ  ์„ค๋ช…ํ•˜๋ผ

int med3(int a, int b, int c) {
    if((b>=a&&c<=a)||(b<=a&&c>=a)) return a;
    else if((a>b&&c<b)||(a<b&&c>b)) return b;
    return c;
}
  • My Answer: med3.c์˜ med3 ํ•จ์ˆ˜๋Š” ๋งŽ์œผ๋ฉด 4๋ฒˆ์˜ ์กฐ๊ฑด์ ˆ์— ๊ฑธ๋ฆฌ๋Š”๋ฐ ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด 4๋ฒˆ์˜ ์กฐ๊ฑด์ ˆ์— ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ ์œผ๋กœ ๋ถˆ๋ฆฌํ•˜๋‹ค.
/*
์ฒซ if ๋ฌธ์˜ ๋‹ค์Œ ํŒ์ •์— ์ฃผ๋ชฉํ•ฉ๋‹ˆ๋‹ค.
    if ((b >= a && c<= a) || (b <= a && c >= a)
์—ฌ๊ธฐ์„œ b>=a ๋ฐ b<=a์˜ ํŒ์ •์„ ๋’ค์ง‘๋Š” ํŒ์ •(์‹ค์งˆ์ ์œผ๋กœ ๋™์ผํ•œ ํŒ์ •)์ด, ๋’ค์ด์€ else ์ดํ›„์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.
    else if ((a > b && c < b) || (a < b && c > b)
์ฆ‰, ์ฒซ ๋ฒˆ์งธ if ๋ฌธ์ด ์„ฑ๋ฆฝ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๋‘ ๋ฒˆ์งธ if ๋ฌธ์—์„œ๋„ (์‹ค์งˆ์ ์œผ๋กœ) ๊ฐ™์€ ํŒ์ •์„ ํ•˜๋ฏ€๋กœ ํšจ์œจ์ด ๋‚˜๋น ์ง‘๋‹ˆ๋‹ค.
*/
  • Correct Answer

2๏ธโƒฃ ๋ฐ˜๋ณต

1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ •์ˆ˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ - while๋ฌธ ๋ฐ˜๋ณต

#include <stdio.h>

int main(void) {
    int i=1, n;
    int sum=0;
    printf("1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.\n");
    printf("n ์ž…๋ ฅ: "); scanf("%d", &n);
    while(i<=n) {
        sum+=i;
        i++;
    }
    printf("1๋ถ€ํ„ฐ %d๊นŒ์ง€์˜ ํ•ฉ: %d\n", n, sum);
    return 0;
}

while(์ œ์–ด์‹) ๋ช…๋ น๋ฌธ
  • ๋ฐ˜๋ณต(repetition) ๊ตฌ์กฐ or ๋ฃจํ”„(loop): ์–ด๋–ค ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•˜๋Š” ๋™์•ˆ ์ฒ˜๋ฆฌ(ํ”„๋กœ๊ทธ๋žจ ๋ช…๋ น๋ฌธ ๋˜๋Š” ๋ช…๋ น์–ด์˜ ์ง‘ํ•ฉ)๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ
  • while๋ฌธ์€ ์‹คํ–‰ ์ „์— ๋ฐ˜๋ณต์„ ๊ณ„์†ํ• ์ง€๋ฅผ ํŒ๋‹จํ•จ -> ์‚ฌ์ „ ํŒ๋‹จ ๋ฐ˜๋ณต ๊ตฌ์กฐ
    • ์ œ์–ด์‹์˜ ํ‰๊ฐ“๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ฉด ํ”„๋กœ๊ทธ๋žจ ๋ช…๋ น๋ฌธ์ด ๊ณ„์† ๋ฐ˜๋ณต๋จ

  • ์ˆœ์„œ๋„

1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ •์ˆ˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ - for๋ฌธ ๋ฐ˜๋ณต

#include <stdio.h>

int main(void) {
    int n;
    int sum=0;
    printf("1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.\n");
    printf("n ์ž…๋ ฅ: "); scanf("%d", &n);
    for(int i=1; i<=n; i++) {
        sum+=i;
    }
    printf("1๋ถ€ํ„ฐ %d๊นŒ์ง€์˜ ํ•ฉ: %d\n", n, sum);
    return 0;
}

for(์‹1; ์‹2; ์‹3) ๋ช…๋ น๋ฌธ
  • ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์€ while๋ฌธ๋ณด๋‹ค for๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ํ•ฉํ•จ
    • ์‹1์€ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•˜๊ธฐ ์•ž์„œ ์ฒ˜์Œ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋จ
    • ์ œ์–ด์‹์ธ ์‹2๋ฅผ ํ‰๊ฐ€ํ•œ ๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ฉด ๋ฃจํ”„ ๋ณธ๋ฌธ์ด ๋ฐ˜๋ณตํ•˜์—ฌ ์‹คํ–‰๋จ
    • ์‹3์ด ํ‰๊ฐ€, ์‹คํ–‰๋จ

  • ์ˆœ์„œ๋„

์–‘์ˆ˜๋งŒ ์ž…๋ ฅํ•˜๊ธฐ

#include <stdio.h>

int main(void) {
    int n, sum=0;
    printf("1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.\n");
    do {
        printf("n ์ž…๋ ฅ: "); scanf("%d", &n);
    } while(n<=0);
    for(int i=1; i<=n; i++) {
        sum+=i;
    }
    printf("1๋ถ€ํ„ฐ %d๊นŒ์ง€์˜ ํ•ฉ: %d\n", n, sum);
    return 0;
}

do๋ฌธ while(์ œ์–ด์‹);
  • do๋ฌธ์€ ์ผ๋‹จ ๋ฃจํ”„ ๋ณธ๋ฌธ์„ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•œ ๋‹ค์Œ ๊ณ„์† ๋ฐ˜๋ณตํ•  ๊ฒƒ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์‚ฌํ›„ ํŒ๋‹จ ๋ฐ˜๋ณต๋ฌธ
    • ์ œ์–ด์‹์„ ํ‰๊ฐ€ํ•œ ๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ฉด ๋ฃจํ”„ ๋ณธ๋ฌธ์˜ ๋ช…๋ น๋ฌธ์ด ๋ฐ˜๋ณต๋จ

  • ์ˆœ์„œ๋„

๊ตฌ์กฐ์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ(structured programming)

  • ํ•˜๋‚˜์˜ ์ž…๊ตฌ์™€ ํ•˜๋‚˜์˜ ์ถœ๊ตฌ๋ฅผ ๊ฐ€์ง„ ๊ตฌ์„ฑ ์š”์†Œ๋งŒ์„ ๊ณ„์ธต์ ์œผ๋กœ ๋ฐฐ์น˜ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•
    • ์ˆœ์ฐจ, ์„ ํƒ, ๋ฐ˜๋ณต์ด๋ผ๋Š” 3์ข…๋ฅ˜์˜ ์ œ์–ด ํ๋ฆ„ ์‚ฌ์šฉ

๋‹ค์ค‘ ๋ฃจํ”„

#include <stdio.h>

int main(void) {
    for(int i=1; i<=9; i++) {
        for(int j=1; j<=9; j++)
            printf("%3d ", i*j);
        putchar('\n');
    }
}

  • ์ˆœ์„œ๋„

๋…ผ๋ฆฌ ์—ฐ์‚ฐ๊ณผ ๋“œ๋ชจ๋ฅด๊ฐ„ ๋ฒ•์น™

#include <stdio.h>

int main(void) {
    int n;
    do {
        printf("2์ž๋ฆฌ ์ •์ˆ˜ n ์ž…๋ ฅ: "); scanf("%d", &n);
    } while(n<10||n>99);
    printf("n = %d\n", n);
    return 0;
}
  • x&&y: ๋…ผ๋ฆฌ๊ณฑ(and)
    • ๋‘˜ ๋‹ค ์ฐธ์ด๋ฉด ์ฐธ
  • x||y: ๋…ผ๋ฆฌํ•ฉ(or)
    • ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด ์ฐธ
  • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์˜ ๋‹จ์ถ• ํ‰๊ฐ€(short circuit evaluation)
    • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์˜ ์‹ ์ „์ฒด๋ฅผ ํ‰๊ฐ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ๋งŒ์œผ๋กœ ์ •ํ™•ํ•ด์ง€๋Š” ๊ฒฝ์šฐ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ

x&&y์™€ !(!x||!y)๋Š” ๋™์ผํ•˜๋‹ค
x||y์™€ !(!x&&!y)๋Š” ๋™์ผํ•˜๋‹ค
  • ๋“œ๋ชจ๋ฅด๊ฐ„ ๋ฒ•์น™(De Morgan's laws)
    • ๊ฐ ์กฐ๊ฑด์„ ๋ถ€์ •ํ•˜๊ณ  ๋…ผ๋ฆฌ๊ณฑ์„ ๋…ผ๋ฆฌํ•ฉ์œผ๋กœ, ๋…ผ๋ฆฌํ•ฉ์„ ๋…ผ๋ฆฌ๊ณฑ์œผ๋กœ ๋ฐ”๊พธ๊ณ  ๋‹ค์‹œ ์ „์ฒด๋ฅผ ๋ถ€์ •ํ•˜๋ฉด ์›๋ž˜์˜ ์กฐ๊ฑด๊ณผ ๊ฐ™์Œ
    • ์ฆ‰, n<10||n>99๋Š” !(n>=10&&n<=99)์™€ ๊ฐ™์Œ

์ง๊ฐ ์ด๋“ฑ๋ณ€ ์‚ผ๊ฐํ˜• ์ถœ๋ ฅ

#include <stdio.h>

int main(void) {
    int height;
    do {
        printf("๋ช‡ ๋‹จ ์‚ผ๊ฐํ˜•์ž…๋‹ˆ๊นŒ?: "); scanf("%d", &height);
    } while(height<=0);
    for(int i=1; i<=height; i++) {
        for(int j=1; j<=i; j++)
            putchar('*');
        putchar('\n');
    }
}

  • ์ˆœ์„œ๋„

0์ด ์•„๋‹ˆ๋ฉด ์ฐธ์ด๊ณ  0์ด๋ฉด ๊ฑฐ์ง“

if(a) printf("ABC");
  • ๋ณ€์ˆ˜ a์˜ ๊ฐ’์ด 0์ด ์•„๋‹ˆ๊ธฐ๋งŒ ํ•˜๋ฉด(1์ด๋“  100์ด๋“  -2๋„ ๋ชจ๋‘) 'ABC' ์ถœ๋ ฅ
    • C ์–ธ์–ด์—์„œ๋Š” 0์€ ๊ฑฐ์ง“, 0์ด ์•„๋‹Œ ๋ชจ๋“  ๊ฐ’์€ ์ฐธ์œผ๋กœ ๊ฐ„์ฃผ

์‚ฌ์ „ ํŒ๋‹จ ๋ฐ˜๋ณต๊ณผ ์‚ฌํ›„ ํŒ๋‹จ ๋ฐ˜๋ณต์˜ ์ฐจ์ด์ 

  • ์‚ฌ์ „ ํŒ๋‹จ ๋ฐ˜๋ณต๋ฌธ์ธ while๋ฌธ๊ณผ for๋ฌธ์€ ์ฒ˜์Œ์— ์ œ์–ด์‹์„ ํ‰๊ฐ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ 0์ด๋ฉด ๋ฃจํ”„ ๋ณธ๋ฌธ์€ ํ•œ ๋ฒˆ๋„ ์‹คํ–‰๋˜์ง€ ์•Š์Œ
  • ์‚ฌํ›„ ํŒ๋‹จ ๋ฐ˜๋ณต๋ฌธ์ธ do๋ฌธ์€ ๋ฃจํ”„ ๋ณธ๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ํ•œ ๋ฒˆ์€ ์‹คํ–‰๋จ

์—ฐ์Šต๋ฌธ์ œ

Q7. ์‹ค์Šต 1-5๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ n์ด 7์ด๋ฉด '1 + 2 + 3 + ... + 7 = 28'๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

Q8. 1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ํ•ฉ์€ (1+10)*5์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€์šฐ์Šค์˜ ๋ง์…ˆ์ด๋ผ๋Š” ์ด ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ •์ˆ˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

Q9. ์ •์ˆ˜ a, b๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ทธ ์‚ฌ์ด์˜ ๋ชจ๋“  ์ •์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” sumof ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ(a์™€ b์˜ ๋Œ€์†Œ ๊ด€๊ณ„์— ์ƒ๊ด€์—†์ด ํ•ฉ์„ ๊ตฌํ•˜๋ผ)

#include <stdio.h>

void q7(int n) {
    int sum=0;
    for(int i=1; i<=n; i++) {
        sum+=i;
        if(i==n) printf("%d = ", i);
        else printf("%d + ", i);
    }
    printf("%d\n", sum);
}

void q8(int n) {
    int sum;
    if(n%2==0) sum=(1+n)*(n/2);
    else sum=(1+(n-1))*((n-1)/2)+n;
    printf("1๋ถ€ํ„ฐ %d๊นŒ์ง€์˜ ํ•ฉ: %d\n", n, sum);
}

void sumof(int a, int b) {
    int big, small, sum=0;
    if(a>=b) {
        big=a; small=b;
    }
    else {
        big=b; small=a;
    }
    for(int i=small; i<=big; i++) {
        sum+=i;
    }
    printf("%d๋ถ€ํ„ฐ %d๊นŒ์ง€์˜ ํ•ฉ: %d\n", small, big, sum);
}

int main(void) {
    int n;
    printf("1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.\n");
    printf("n์˜ ๊ฐ’: "); scanf("%d", &n);
    q7(n);
    q8(n);
    int a, b;
    printf("a์˜ ๊ฐ’: "); scanf("%d", &a);
    printf("b์˜ ๊ฐ’: "); scanf("%d", &b);
    sumof(a, b);
    return 0;
}

Q10. ๋‘ ๋ณ€์ˆ˜ a, b์— ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๊ณ  b-a๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ(๋ณ€์ˆ˜ b์— ์ž…๋ ฅํ•œ ๊ฐ’์ด a ์ดํ•˜๋ฉด ๋ณ€์ˆ˜ b์˜ ๊ฐ’์„ ๋‹ค์‹œ ์ž…๋ ฅ)

Q11. ์–‘์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ž๋ฆฟ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

void q10() {
    int a, b;
    printf("a์˜ ๊ฐ’: "); scanf("%d", &a);
    do {
        printf("b์˜ ๊ฐ’: "); scanf("%d", &b);
        if(a>=b) printf("a๋ณด๋‹ค ํฐ ๊ฐ’์„ ์ž…๋ ฅํ•˜์„ธ์š”!\n");
    } while(a>=b);
    printf("b-a = %d\n", b-a);
}

void q11() {
    int num, digits=0;
    do {
        printf("๊ฐ’: "); scanf("%d", &num);
    } while(num<=0);
    while(num>0) {
        num/=10;
        digits++;
    }
    printf("๊ทธ ์ˆ˜๋Š” %d์ž๋ฆฌ์ž…๋‹ˆ๋‹ค.\n", digits);
}

Q12. ์œ„์ชฝ๊ณผ ์™ผ์ชฝ์— ๊ณฑํ•˜๋Š” ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ณฑ์…ˆํ‘œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ(๊ตฌ๋ถ„์„ ์€ ์ˆ˜์ง์„ , ๋งˆ์ด๋„ˆ์Šค, ํ”Œ๋Ÿฌ์Šค ๊ธฐํ˜ธ ์‚ฌ์šฉ)

Q13. ๊ณฑ์…ˆ์ด ์•„๋‹ˆ๋ผ ๋ง์…ˆ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

Q14. ์ž…๋ ฅํ•œ ์ˆ˜๋ฅผ ํ•œ ๋ณ€์œผ๋กœ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜•์„ * ๊ธฐํ˜ธ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

Q15. ์ž…๋ ฅํ•œ ๋†’์ด์™€ ๋„ˆ๋น„์— ๋งž๋Š” ์ง์‚ฌ๊ฐํ˜•์„ * ๊ธฐํ˜ธ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

void q12() {
    for(int i=0; i<=9; i++) {
        if(i==0) printf("  | ");
        else printf("%2d| ", i);
        for(int j=1; j<=9; j++) {
            if(i==0) printf("%3d ", j);
            else printf("%3d ", i*j);    
        }
        putchar('\n');
        if(i==0) printf("---+------------------------------------\n");
    }
}

void q13() {
    for(int i=0; i<=9; i++) {
        if(i==0) printf("  | ");
        else printf("%2d| ", i);
        for(int j=1; j<=9; j++) {
            if(i==0) printf("%3d ", j);
            else printf("%3d ", i+j);    
        }
        putchar('\n');
        if(i==0) printf("---+------------------------------------\n");
    }
}

void q14() {
    int side;
    printf("์‚ฌ๊ฐํ˜•์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.\n"); 
    printf("์ž…๋ ฅ ์ˆ˜: "); scanf("%d", &side);
    for(int i=1; i<=side; i++) {
        for(int j=1; j<=side; j++)
            putchar('*');
        putchar('\n');
    }
}

void q15() {
    int width, height;
    printf("์‚ฌ๊ฐํ˜•์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.\n");
    printf("๋†’์ด: "); scanf("%d", &height);
    printf("๋„ˆ๋น„: "); scanf("%d", &width);
    for(int i=1; i<=height; i++) {
        for(int j=1; j<=width; j++)
            putchar('*');
        putchar('\n');
    }
}

Q16. ์™ผ์ชฝ ์•„๋ž˜, ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๊ฐ€ ์ง๊ฐ์ธ ์ด๋“ฑ๋ณ€ ์‚ผ๊ฐํ˜•์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ž‘์„ฑํ•˜๋ผ

Q17. n๋‹จ์˜ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ

Q18. ์•„๋ž˜๋ฅผ ํ–ฅํ•œ n๋‹จ์˜ ์ˆซ์ž ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ

void triangleLB() {
    int height;
    do {
        printf("๋ช‡ ๋‹จ ์‚ผ๊ฐํ˜•์ž…๋‹ˆ๊นŒ?: "); scanf("%d", &height);
    } while(height<=0);
    for(int i=1; i<=height; i++) {
        for(int j=1; j<=i; j++)
            putchar('*');
        putchar('\n');
    }
}

void triangleLU() {
    int height;
    do {
        printf("๋ช‡ ๋‹จ ์‚ผ๊ฐํ˜•์ž…๋‹ˆ๊นŒ?: "); scanf("%d", &height);
    } while(height<=0);
    for(int i=height; i>=1; i--) {
        for(int j=1; j<=i; j++)
            putchar('*');
        putchar('\n');
    }
}

void triangleRU() {
    int height;
    do {
        printf("๋ช‡ ๋‹จ ์‚ผ๊ฐํ˜•์ž…๋‹ˆ๊นŒ?: "); scanf("%d", &height);
    } while(height<=0);
    for(int i=height; i>=1; i--) {
        for(int j=1; j<=height; j++) {
            if((height-i)>=j) putchar(' ');
            else putchar('*');
        }
        putchar('\n');
    }
}

void triangleRB() {
    int height;
    do {
        printf("๋ช‡ ๋‹จ ์‚ผ๊ฐํ˜•์ž…๋‹ˆ๊นŒ?: "); scanf("%d", &height);
    } while(height<=0);
    for(int i=1; i<=height; i++) {
        for(int j=1; j<=height; j++) {
            if((height-i)<j) putchar('*');
            else putchar(' ');
        }
        putchar('\n');
    }
}

void spira(int n) {
    int max = (n-1)*2+1;
    int half = max/2+1;
    for(int i=0; i<n; i++) {
        for(int j=1; j<=max; j++) {
            if((half-i)<=j&&(half+i)>=j) putchar('*');
            else putchar(' ');
        }
        putchar('\n');
    }
}

void nrpira(int n) {
    int max = (n-1)*2+1;
    int half = max/2+1;
    for(int i=n-1; i>=0; i--) {
        for(int j=1; j<=max; j++) {
            if((half-i)<=j&&(half+i)>=j) printf("%d", half-i);
            else putchar(' ');
        }
        putchar('\n');
    }
}
728x90
๋ฐ˜์‘ํ˜•
Comments