쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 14-2
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;
for (j = i + 1; j < size; j++)
if (strcmp(list[j], list[least]) < 0)
least = j;
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];
}
댓글
댓글 쓰기