C언어(자기주도C언어프로그래밍)/Chapter11. 함수1

자기주도C언어프로그래밍 Chapter11. 함수1 - 연습문제&자가진단

사상 2023. 3. 26. 23:04

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=10b0 

 

JUNGOL

 

www.jungol.co.kr

사이트에서 직접 프로그램을 돌려볼 수 있습니다.

 

교재 P.216 연습문제1 - 소스1

#include <stdio.h>

void line(){
    puts("==============================");
}

int main(){
    line();

    puts("line 함수를 호출하였습니다.");
    puts("line 함수를 다시 호출합니다.");

    line();

    return 0;
}

 

교재 P.218 연습문제1 - 소스2

#include <stdio.h>

void line();

int main(){
    line();

    puts("line 함수를 호출하였습니다.");
    puts("line 함수를 다시 호출합니다.");

    line();

 

    return 0;
}

void line(){
    puts("==============================");
}

 

※ 함수는 main함수 위에서 코드까지 전부 작성 해도 되고

main함수 위에서 이름만 정의 한 다음 main 함수 밑에서 함수에 대해 써도 된다

(이때 main 함수 위에서 이름을 정의 하는 이유는 main함수에서 ~~라는 함수가 나올 것이다라는 것을 미리 알려주기 위함)

 

교재 P.219 자가진단1

#include <stdio.h>

void string1(){
    puts("~!@#$^&*()_+|");
}
int main(){
    int i, num;
    scanf("%d", &num);
    
    for(i=1;i<=num;i++){
        string1();
    }

    return 0;
}

 

교재 P.220 연습문제2

#include <stdio.h>

void plusten(int su){
    printf("10큰수 : %d\n", su+10);
}

void minusten(int su){
    printf("10작은수 : %d", su-10);
}

int main(){
    int num;

    scanf("%d", &num);

    plusten(num);
    minusten(num);

    return 0;
}

 

교재 P.221 자가진단2

#include <stdio.h>

void area(int radius){
    printf("%.2lf", radius * radius * 3.14);
}


int main(){
    int r;
    scanf("%d", &r);

    area(r);

    return 0;
}

 

교재 P.222 연습문제3

#include <stdio.h>
void star(int n){
    int i, j;

    for(i=1;i<=n;i++){
        for(j=1;j<=i;j++){
            printf("*");
        }
        printf("\n");
    }
}

int main(){
    int n;

    scanf("%d", &n);
    star(n);

    return 0;
}

 

교재 P.223 자가진단3

#include <stdio.h>
void number(int num){
    int i, j, n=1;
    for(i=1;i<=num;i++){
        for(j=1;j<=num;j++){
            printf("%d ", n++);
        }
        printf("\n");
    }
}

int main(){
    int num;
    scanf("%d", &num);

    number(num);

    return 0;
}

 

교재 P.224 연습문제4

#include <stdio.h>

int add(int x, int y){
    return x+y;
}

int sub(int x, int y){
    int cha = x - y;
    if(cha < 0) cha *= -1;
    return cha;
}

int main(){
    int a, b, sum;

    scanf("%d %d", &a, &b);

    sum = add(a, b);
    printf("두 수의 합 = %d \n", sum);
    printf("두 수의 차 = %d \n", sub(a, b));
}

 

교재 P.225 자가진단4

#include <stdio.h>

int max(int x, int y, int z){
    int max;

    if(x > y && x > z){
        max = x;
    }
    else if(y > x && y > z){
        max = y;
    }
    else{
        max = z;
    }

    return max;
}

int main(){
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    printf("%d", max(a, b, c));

    return 0;
}

 

교재 P.226 연습문제5 - 소스1

#include <stdio.h>

double pyung(int a, int b, int c){
    int sum = a + b + c;
    return sum / 3.0;
}

int main(){
    int kor, eng, mat;
    double avg;

    printf("세과목의 점수를 입력하세요. ");
    scanf("%d %d %d", &kor, &eng, &mat);

    avg = pyung(kor, eng, mat);

    printf("평균 : %.2f\n", avg);

    return 0;
}

 

교재 P.227 연습문제5 - 소스2

#include <stdio.h>

double pyung(int a, int b, int c){
    return (a+b+c) / 3.0;
}

int main(){
    int kor, eng, mat;

    printf("세과목의 점수를 입력하세요. ");
    scanf("%d %d %d", &kor, &eng, &mat);

    printf("평균 : %.2f\n", pyung(kor, eng, mat));

    return 0;
}

 

교재 P.228 자가진단5

#include <stdio.h>

int cal_power(int num, int power){
    int i, result=1;
    for(i=1;i<=power;i++){
        result = result*num;
    }

    return result;
}
int main(){
    int a, b;
    scanf("%d %d", &a, &b);

    printf("%d", cal_power(a, b));

    return 0;
}

교재 P.228 연습문제6

#include <stdio.h>
int gesan(int x, int y, char op){
    switch(op){
        case '+':
            return x+y;
        case '-':
            return x-y;
        case '*':
            return x*y;
        case '/':
            return x/y;
    }
    return 0;
}
int main(){
    int a, b;
    char c;

    scanf("%d %c %d", &a, &c, &b);
    printf("%d %c %d = %d \n", a, c, b, gesan(a, b, c));

    return 0;
}

 

교재 P.229 자가진단6

#include <stdio.h>
int gesan(int x, int y, char op){
    if(op=='+'){
        return x+y;
    }
    else if(op=='-'){
        return x-y;
    }
    else if(op=='*'){
        return x*y;
    }
    else if(op=='/'){
        return x/y;
    }
    return 0;
}
int main(){
    int a, b;
    char c;

    scanf("%d %c %d", &a, &c, &b);
    printf("%d %c %d = %d", a, c, b, gesan(a, b, c));

    return 0;
}

 

교재 P.230 연습문제7

#include <stdio.h>

void swapvalue(int x, int y){
    int tmp;

    tmp = x;
    x = y;
    y = tmp;

    printf("첫 번째 함수 실행중 x = %d, y = %d \n", x, y);
}

void swapreference(int &x, int &y){
    int tmp;

    tmp = x;
    x = y;
    y = tmp;

    printf("두 번째 함수 실행중 x = %d, y = %d \n", x, y);
}

int main(){
    int a, b;

    printf("두 수를 입력하세요. ");
    scanf("%d %d", &a, &b);

    swapvalue(a, b);
    printf("첫 번째 함수 실행후 a = %d, b = %d \n", a, b);

    swapreference(a, b);
    printf("두 번째 함수 실행후 a = %d, b = %d \n", a, b);

    return 0;
}

 

※ void swapreference(int &x, int &y) 에서 &를 쓰는 이유는 참조에 의한 전달이기 때문입니다.

여기서는 a와  b의 값을 가져오는게 아니라  a와 b가 저장되어 있는 위치를 전달하여 그 위치의 이름을 x와 y로 새롭게 붙여주는 것입니다. (x와 y는 새로운 변수가 아님 // a와 b의 새로운 별명(?)느낌)

 

교재 P.232 자가진단7

#include <stdio.h>

void cal(int &x, int &y){
    if(x > y){
        y *= 2;
        x /= 2;
    }
    else{
        x *= 2;
        y /= 2;
    }
}
int main(){
    int a, b;
    scanf("%d %d", &a, &b);

    cal(a, b);
    printf("%d %d", a, b);
}

 

교재 P.233 연습문제8 - 소스1

#include <stdio.h>

int a, b;
int hap, gop;

void input(){
    printf("두 수를 입력하세요. ");
    scanf("%d %d", &a, &b);
}

void gesan(){
    hap = a + b;
    gop = a * b;
}

void output(){
    printf("합 : %d \n", hap);
    printf("곱 : %d \n", gop);
}

int main(){
    input();
    gesan();
    output();
    return 0;
}

 

교재 P.235 연습문제8 - 소스2

#include <stdio.h>

void input(int &x, int &y){
    printf("두 수를 입력하세요. ");
    scanf("%d %d", &x, &y);
}

int plus(int x, int y){
    return x + y;
}

int multi(int &x, int &y){
    return x*y;
}

void output(int x, int y){
    printf("합 : %d \n", x);
    printf("곱 : %d \n", y);
}

int main(){
    int a, b;
    int hap, gop;

    input(a, b);
    hap = plus(a, b);
    gop = multi(a, b);
    output(hap, gop);

    return 0;
}

 

※ 여기서 void input(int &x, int &y)에는 반드시 &가 있어야 한다 (x와 y는 각각 a와 b의 별명(?)이기 때문에 a, b위치에 입력을 받아야 하므로)

나머지 부분(plus, multi, output)은 &가 있어도 되고 없어도 된다.

 

#include <stdio.h>

int print_multiplication_tables(int x, int y){
    int i, j, temp; // 3 5
    if(x > y){
        temp = x;
        x = y;
        y = temp;
    }

    for(i=x;i<=y;i++){
        printf("== %ddan ==\n", i);
        for(j=1;j<=9;j++){
            printf("%d * %d = %2d\n", i, j, i*j);
        }
        printf("\n");
    }
    return 0;
}

int main(){
    int a, b;
    scanf("%d %d", &a, &b);

    print_multiplication_tables(a, b);
    
    return 0;
}