본문 바로가기
솔루션모음/쉽게 풀어 쓴 C언어 Express 실습문제

[쉽게 풀어 쓴 C언어 Express] 14장 프로그래밍 programming 솔루션 답지

by 이얏호이야호 2020. 4. 11.

쉽게 풀어 쓴 C언어 express 솔루션입니다.

 

1.

// 이중 포인터 프로그램

#include <stdio.h>

void set_proverb(char **q, int n);

int main(void) {

 char *s = NULL;

 int n;

 printf("몇번째 속담을 선택하시겠습니까?");

 scanf("%d", &n);

 set_proverb(&s, n);

 printf("selected proverb = %s\n",s);

 return 0;

}

void set_proverb(char **q, int n) {

 static char *array[10]= {

 "A bad shearer never had a good sickle.",

 "A bad workman (always) blames his tools. ",

 "A bad workman quarrels with his tools.",

 "A bad thing never dies.",

 "A bad workman finds fault with his tools.",

 "A bad workman always blames his tools.",

 "A bargain is a bargain.",

 "A barking dog never bites.",

 "A barking dog was never a good hunter."

 }

 ;

 *q = array[n];

}

 

2.

#include <stdio.h>

int get_sum(int array[], int size);

int main(void) {

 int m[][1]= {

 1, 1, 1

 }

 ;

 int result = get_sum(m[0], 1);

 printf("%d\n", result);

 return 0;

}

int get_sum(int array[], int size) {

 int i=0;

 int sum=0;

 for (i=0;i<size; i++)

 sum += array[i];

 return sum;

}

 

 

 

 

 

 

 

 

 

3. 문제 오타 수정해주세요! 함수의 원형이 pr_str_array(char **dp, int n)이어야 합니다.

#include <stdio.h>

void pr_str_array(char **dp, int n) {

 int i;

 for (i=0;i<n;i++) {

 printf("%s\n", dp[i]);

 }

}

int main(void) {

 char *array[10]= {

 "A bad shearer never had a good sickle.",

 "A bad workman (always) blames his tools. ",

 "A bad workman quarrels with his tools.",

 "A bad thing never dies.",

 "A bad workman finds fault with his tools.",

 "A bad workman always blames his tools.",

 "A bargain is a bargain.",

 "A barking dog never bites.",

 "A barking dog was never a good hunter."

 }

 ;

 pr_str_array(array, 10);

 return 0;

}

 

 

4.

#include <stdio.h>

void set_max_ptr(int m[], int size, int **pmax);

int main(void) {

 int m[6]= {

 5,6,1,3,7,9

 }

 ;

 int *pmax;

 set_max_ptr(m, 6, &pmax);

 printf("가장 큰 값은 %d\n", *pmax);

 return 0;

}

void set_max_ptr(int m[], int size, int **pmax) {

 int i=0;

 int max;

 max = m[0];

 for (i=1;i<6;i++) {

 if( m[i] > max ) {

 max = m[i];

 (*pmax) = &m[i];

 }

 }

}

5.

#include <stdio.h>

void sort_strings(char *s[], int size);

int main(void) {

 int i;

 char *s[3] = {

 "mycopy", "src", "dst"

 }

 ;

 sort_strings(s, 3);

 for (i=0;i<3;i++)

 printf("%s\n", s[i]);

 return 0;

}

void sort_strings(char *list[], int size) {

 int i, j, least;

 char *temp;

 for (i = 0; i < size-1; i++) {

 least = i;

 // i번째 값을 최소값으로 가정

 for (j = i + 1; j < size; j++) // 최소값 탐색

 if(strcmp(list[j], list[least]) < 0)

 least = j;

 // i번째 원소와 least 위치의 원소를 교환

 temp = list[i];

 list[i] = list[least];

 list[least] = temp;

 }

}

 

 

6.

#include <stdio.h>

int get_average(int list[], int n);

int main(void) {

 int score[3][3]= { {

 100, 30, 67

 }

 , {

 89, 50, 12

 }

 , {

 19, 60, 90

 }

 }

 ;

 int i, avg;

 for (i=0;i<3;i++) {

 avg = get_average(score[i], 3);

 printf("%d행의 평균값=%d\n", i, avg);

 }

 return 0;

}

int get_average(int list[], int n) {

 int i;

 int sum = 0;

 for (i=0;i<n;i++)

 sum += list[i];

 return sum/n;

}

 

 

 

 

 

 

 

 

 

7.

(a) 이진화

#include <stdio.h>

#define WIDTH 10

#define HEIGHT 10

void threshold_image(int p[HEIGHT][WIDTH], int h, int w);

int main(void) {

 int image[HEIGHT][WIDTH] = { {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,0,9,9,0,0,0,0,0

 }

 , {

 0,0,9,0,9,0,0,0,0,0

 }

 , {

 0,0,0,0,8,0,0,0,0,0

 }

 , {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,0,0,7,0,0,0,0,0

 }

 , {

 0,0,0,0,8,0,0,0,0,0

 }

 , {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,6,6,9,7,7,0,0,0

 }

 }

 ;

 brighten_image(image, HEIGHT, WIDTH);

 return 0;

}

void threshold_image(int p[HEIGHT][WIDTH], int h, int w) {

 int x, y;

 for (y = 0; y < h; y++)

 for (x = 0; x < w; x++)

 if(p[y][x] > 5) p[y][x] = 255; else p[y][x] = 0;

}

(b) 반전

#include <stdio.h>

#define WIDTH 10

#define HEIGHT 10

void reverse_image(int p[HEIGHT][WIDTH], int h, int w);

int main(void) {

 int image[HEIGHT][WIDTH] = { {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,0,9,9,0,0,0,0,0

 }

 , {

 0,0,9,0,9,0,0,0,0,0

 }

 , {

 0,0,0,0,8,0,0,0,0,0

 }

 , {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,0,0,7,0,0,0,0,0

 }

 , {

 0,0,0,0,8,0,0,0,0,0

 }

 , {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,0,0,9,0,0,0,0,0

 }

 , {

 0,0,6,6,9,7,7,0,0,0

 }

 }

 ;

 reverse_image(image, HEIGHT, WIDTH);

 return 0;

}

void reverse_image(int p[HEIGHT][WIDTH], int h, int w) {

 int x, y;

 for (y = 0; y < h; y++)

 for (x = 0; x < w; x++)

 p[y][x] = 255 - p[y][x];

}

 

 

8.

#include <stdio.h>

#define HEIGHT 10

#define WIDTH 10 

void array_copy(int src[][WIDTH], int dst[][WIDTH]);

void array_print(int a[][WIDTH]);

int score[HEIGHT][WIDTH]= { {

 100, 30, 67

 }

 , {

 89, 50, 12

 }

 , {

 19, 60, 90

 }

}

;

int tmp[HEIGHT][WIDTH];

int main(void) {

 array_copy(score, tmp);

 array_print(tmp);

 return 0;

}

void array_print(int a[][WIDTH]) {

 int r,c;

 for (r=0;r<HEIGHT;r++) {

 for (c=0;c<WIDTH;c++)

 printf("%d ", a[r][c]);

 printf("\n");

 }

}

void array_copy(int src[][WIDTH], int dst[][WIDTH]) {

 int *p=&src[0][0];

 int *end=&src[HEIGHT-1][WIDTH-1];

 int *q=&dst[0][0];

 while(p <= end) {

 *q++ = *p++;

 }

}

 

 

9.

#include <stdio.h>

#define HEIGHT 10

#define WIDTH 10

#define ALIVE 1 

#define DEAD 0

 void print_board(int board[HEIGHT][WIDTH]) {

 int h, w;

 for (h = 0; h < HEIGHT; h++) {

 for (w = 0; w < WIDTH; w++) {

 if (board[h][w] == ALIVE) {

 printf("X");

 } else {

 printf("-");

 }

 }

 printf("\n");

 }

 printf("\n");

}

void clear_board(int board[HEIGHT][WIDTH]) {

 int h, w;

 for (h = 0; h < HEIGHT; h++) {

 for (w = 0; w < WIDTH; w++) {

 board[h][w] = DEAD;

 }

 }

}

int get_value(int board[HEIGHT][WIDTH], int row, int col) {

 if (row < 0 || row >= HEIGHT

  || col < 0 || col >= WIDTH

  || board[row][col] != ALIVE ) {

 return 0;

 } else {

 return 1;

 }

}

int get_neighbor_count(int board[HEIGHT][WIDTH], int row, int col) {

 int neighbor = 0;

 if( row == 0 || col == 0 || row ==(HEIGHT-1) || col ==(WIDTH-1)) {

 // 가장자리이면 표준적인 방법

 neighbor += get_value(board, row - 1, col - 1);

 neighbor += get_value(board, row - 1, col);

 neighbor += get_value(board, row - 1, col + 1);

 neighbor += get_value(board, row, col - 1);

 neighbor += get_value(board, row, col + 1);

 neighbor += get_value(board, row + 1, col - 1);

 neighbor += get_value(board, row + 1, col);

 neighbor += get_value(board, row + 1, col + 1);

 } else {

 // 내부이면 빠른 방법

 int *p = &(board[row][col]);

 neighbor += *(p-WIDTH-1);

 neighbor += *(p-WIDTH);

 neighbor += *(p-WIDTH+1);

 neighbor += *(p-1);

 neighbor += *(p+1);

 neighbor += *(p+WIDTH-1);

 neighbor += *(p+WIDTH);

 neighbor += *(p+WIDTH+1);

 }

 return neighbor;

}

void do_generation(int board[HEIGHT][WIDTH]) {

 int tmp_board[HEIGHT][WIDTH];

 int neighbor, h, w;

 for (h = 0; h < HEIGHT; h++) {

 for (w = 0; w < WIDTH; w++) {

 neighbor = get_neighbor_count(board, h, w);

 if (neighbor==3) {

 tmp_board[h][w] = ALIVE;

 } else if (neighbor == 2 && board[h][w] == ALIVE) {

 tmp_board[h][w] = ALIVE;

 } else if (neighbor >= 4 && board[h][w] == ALIVE) {

 tmp_board[h][w] = DEAD;

 } else {

 tmp_board[h][w] = DEAD;

 }

 }

 }

 for (h = 0; h < HEIGHT; h++) {

 for (w = 0; w < WIDTH; w++) {

 board[h][w] = tmp_board[h][w];

 }

 }

}

void initialize_board(int board[HEIGHT][WIDTH]) {

 board[3][4] = ALIVE;

 board[3][5] = ALIVE;

 board[3][6] = ALIVE;

 board[4][6] = ALIVE;

 board[5][5] = ALIVE;

}

int main(void) {

 int board[HEIGHT][WIDTH];

 char user;

 int generation = 0;

 clear_board(board);

 initialize_board(board);

 print_board(board);

 do {

 do_generation(board);

 print_board(board);

 printf("generation %d\n", ++generation);

 printf("종료하려면 q, 계속하려면 m: ");

 scanf(" %c", &user);

 }

 while (user != 'q') ;

 return 0;

}

 

 

 

 

 

 

 

 

 

더 많은  쉽게 풀어 쓴 C언어EXPRESS 이론 솔루션

 https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%9D%B4%EB%A1%A0

더 많은 쉽게 풀어 쓴 C언어EXPRESS 프로그래밍 솔루션 : 

https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%8B%A4%EC%8A%B5%EB%AC%B8%EC%A0%9C

댓글