K. N. King C Programming - Chapter 5. Exercises, Programming Projects
[Week 4] Hacking Cat’s Weekly Study
Exercises
Section 5.1 (1)
-
- The following program fragments illustrate the relational and equality operators. Show the output produced by each, assuming that i, j. and k are int variables.

- The following program fragments illustrate the relational and equality operators. Show the output produced by each, assuming that i, j. and k are int variables.
- 1번 문제는 연산자의 결합순서, 우선순위에 관한 문제입니다.
- (a)
i * j의 연산 결과는 6이다. 따라서,i * j == 6연산 시에는 참(1) 결과 값을 반환합니다.
# soneg@soneg: ~/hackingcats/5/exercises (17:27:18)
ζ cat 1.c
#include <stdio.h>
int main()
{
int i, j, k;
i = 2; j = 3;
k = i * j == 6;
printf("%d", k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (17:27:19)
ζ ./1
1%- (b) <, > 는 결합 방향이 → 이므로
(k > i) < j순서로 연산이 진행됩니다.- 따라서,
"(1 > 5) < 10" → "0 < 10" → 1결과적으로 1이 반환됩니다.
- 따라서,
# soneg@soneg: ~/hackingcats/5/exercises (17:30:48)
ζ cat b.c
#include <stdio.h>
int main()
{
int i, j, k;
i= 5; j= 10; k=1;
printf("%d", k > i < j);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (17:30:51)
ζ gcc -o b b.c
# soneg@soneg: ~/hackingcats/5/exercises (17:30:54)
ζ ./b
1%- (c) < 연산자의 우선순위는 == 보다 높습니다. 따라서,
i < j, i < k연산이 먼저 수행되고 그 결과가 같은지 비교하게 됩니다.0 == 0이므로 참(1) 값이 반환됩니다.
# soneg@soneg: ~/hackingcats/5/exercises (17:36:53)
ζ cat c.c
#include <stdio.h>
int main()
{
int i, j, k;
i= 3; j= 2; k=1;
printf("%d", i < j == i < k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (17:36:55)
ζ gcc -o c c.c
# soneg@soneg: ~/hackingcats/5/exercises (17:36:59)
ζ ./c
1%- (d)
i % j + 1이 먼저 계산되고4 < 5연산이 수행되므로 참(1) 값을 반환합니다.
# soneg@soneg: ~/hackingcats/5/exercises (17:40:37)
ζ cat d.c
#include <stdio.h>
int main()
{
int i, j, k;
i=3; j=4; k=5;
printf("%d", i % j + 1 < k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (17:40:39)
ζ gcc -o d d.c
# soneg@soneg: ~/hackingcats/5/exercises (17:40:42)
ζ ./d
1%Section 5.1 (2)
-
- The following program fragments illustrate the logical operators. Show the output produced by each, assuming that i, j, and k are int variables.

- The following program fragments illustrate the logical operators. Show the output produced by each, assuming that i, j, and k are int variables.
- (a) C언어에서는 0이 아닌 정수는 모두 참으로 판단한다. 논리 부정 연산자
!가 정수 값 앞에 오면 그 반대의 값을 반환하므로 거짓(0) 값을 반환하게 된다. 따라서0 < 5의 연산 결과는 참이므로 1 값을 반환한다.
# soneg@soneg: ~/hackingcats/5/exercises/2 (17:45:17)
ζ cat a.c
#include <stdio.h>
int main()
{
int i, j;
i=10; j=5;
printf("%d", !i < j);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/2 (17:45:18)
ζ gcc -o a a.c
# soneg@soneg: ~/hackingcats/5/exercises/2 (17:45:20)
ζ ./a
1%- (b) 이전 문제와 동일하다. 변수 i에 논리 부정 연산이 2번 계산되면 거짓이 되었다가 다시 참이 된다. 변수 j의 논리 부정 연산 결과는 0이므로
1 + 0이 계산되어 1 을 반환한다.
# soneg@soneg: ~/hackingcats/5/exercises/2 (17:46:38)
ζ cat b.c
#include <stdio.h>
int main()
{
int i, j;
i=2; j=1;
printf("%d", !!i + !j);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/2 (17:46:39)
ζ ./b
1%- (c)
i && j연산 결과는 0,0 || k연산 결과는 1 이므로 1을 반환한다.
# soneg@soneg: ~/hackingcats/5/exercises/2 (19:44:17)
ζ cat c.c
#include <stdio.h>
int main()
{
int i, j, k;
i=5; j=0, k=-5;
printf("%d", i && j || k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/2 (19:44:19)
ζ gcc -o c c.c
# soneg@soneg: ~/hackingcats/5/exercises/2 (19:44:22)
ζ ./c
1- (d)
1 < 2는 참을 반환하고1 || 3또한 참을 반환하므로 1이 출력된다.
# soneg@soneg: ~/hackingcats/5/exercises/2 (19:49:33)
ζ cat d.c
#include <stdio.h>
int main()
{
int i, j, k;
i=1; j=2, k=3;
printf("%d", i < j || k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/2 (19:49:35)
ζ gcc -o d d.c
# soneg@soneg: ~/hackingcats/5/exercises/2 (19:49:38)
ζ ./d
1%Section 5.1 (3)
-
- The following program fragments illustrate the short-circuit behavior of logical expressions. Show the output produced by each, assuming that i, j. and k are int variables.

- The following program fragments illustrate the short-circuit behavior of logical expressions. Show the output produced by each, assuming that i, j. and k are int variables.
- (a)
++j가 계산되어 변수j의 값은 5로 변경된다. 이후i < j, j < k연산이 순차적으로 진행된다. 최종적으로1 || 0이 계산되며 OR 연산에 따라 참(1) 값이 출력된다.
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:51:37)
ζ cat a.c
#include <stdio.h>
int main()
{
int i, j, k;
i=3; j=4, k=5;
printf("%d", i < j || ++j < k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:51:37)
ζ gcc -o a a.c
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:51:42)
ζ ./a
1- (b)
7 - 7 && 7++ < 9연산은0 && 1연산과 동일하다. 따라서 첫 번째 프린트 문에서는 0을 반환한다. 이후 i, j, k 값은 초기 설정 값과 동일하다.
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:54:38)
ζ cat b.c
#include <stdio.h>
int main()
{
int i, j, k;
i=7; j=8, k=9;
printf("%d ", i - 7 && j++ < k);
printf("%d, %d, %d", i, j, k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:54:39)
ζ gcc -o b b.c
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:54:42)
ζ ./b
0 7, 8, 9%- (c) 첫 번째 프린트 문에 의해
i = 8로 설정된다. 이 때, OR 연산에서 좌측의 연산 결과가 참이기 때문에 우측 연산(j=k)는 무시된다. 따라서,i, j, k의 값은 8, 8, 9 로 설정된다.
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:59:08)
ζ cat c.c
#include <stdio.h>
int main()
{
int i, j, k;
i=7; j=8, k=9;
printf("%d ", (i = j) || (j = k));
printf("%d, %d, %d", i, j, k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:59:09)
ζ gcc -o c c.c
# soneg@soneg: ~/hackingcats/5/exercises/3 (19:59:11)
ζ ./c
1 8, 8, 9% - (d) 이전 문제와 동일하게
++i의 연산 결과가 참이므로, || 우측의 연산은 진행되지 않는다.- 따라서, i, j, k 의 최종 값은 2, 1 1 가 된다.
# soneg@soneg: ~/hackingcats/5/exercises/3 (20:03:27)
ζ cat d.c
#include <stdio.h>
int main()
{
int i, j, k;
i=1; j=1, k=1;
printf("%d ", ++i || ++j && ++k);
printf("%d, %d, %d", i, j, k);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/3 (20:03:30)
ζ gcc -o d d.c
# soneg@soneg: ~/hackingcats/5/exercises/3 (20:03:34)
ζ ./d
1 2, 1, 1Section 5.1. (4)
-
- Write a single expression whose value is either-1.0, or +1. depending on whether i is less than, equal to, or greater than j. respectively.

- Write a single expression whose value is either-1.0, or +1. depending on whether i is less than, equal to, or greater than j. respectively.
# soneg@soneg: ~/hackingcats/5/exercises/4 (20:20:31)
ζ cat 4.c
#include <stdio.h>
int main(void)
{
int i, j;
printf("Enter value of i: ");
scanf("%d", &i);
printf("Enter value of j: ");
scanf("%d", &j);
if (i < j)
{
printf("-1.0");
} else if (i == j)
{
printf("0");
} else {
printf("+1.0");
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises/4 (20:20:32)
ζ gcc -o 4 4.c
# soneg@soneg: ~/hackingcats/5/exercises/4 (20:20:34)
ζ ./4
Enter value of i: 1
Enter value of j: 2
-1.0%Section 5.2. (5)

- 조건문을 위와 같이 설정했을 경우, n 값이 1 이상인지 먼저 확인하여 참 혹은 거짓 값을 반환한다.
- n 값이 0 이라면
n >= 1결과가 거짓이므로 두번째 연산 ((n >= 1) <= 10)은 수행되지 않는다.- 따라서 0 이 반환된다.
# soneg@soneg: ~/hackingcats/5/exercises (20:28:45)
ζ cat 5.c
#include <stdio.h>
int main(void)
{
int n = 0;
if (n >= 1 <= 10)
{
printf("%d", n);
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (20:28:48)
ζ gcc -o 5 5.c
# soneg@soneg: ~/hackingcats/5/exercises (20:28:50)
ζ ./5
0%Section 5.2. (6)

n == -9는 참이 아니므로 아무런 값도 출력되지 않는다.
# soneg@soneg: ~/hackingcats/5/exercises (20:33:11)
ζ cat 6.c
#include <stdio.h>
int main(void)
{
int n = 5;
if (n == 1-10)
{
printf("n is between 1 and 10\n");
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (20:33:12)
ζ gcc -o 6 6.c
./% # soneg@soneg: ~/hackingcats/5/exercises (20:33:16)
ζ ./6
# soneg@soneg: ~/hackingcats/5/exercises (20:33:23)
ζSection 5.2. (7)
i >= 0는 참이므로 17을 출력한다.
# soneg@soneg: ~/hackingcats/5/exercises (20:37:51)
ζ cat 7.c
#include <stdio.h>
int main(void)
{
int i = 17;
printf("%d\n", i >= 0 ? i : -i);
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (20:37:52)
ζ gcc -o 7 7.c
.% # soneg@soneg: ~/hackingcats/5/exercises (20:37:56)
ζ ./7
17Section 5.2. (8)

- 19살 이상인지 미만인지만 확인하면 된다. 삼항연산자를 사용하여 표현식을 작성했다.
# soneg@soneg: ~/hackingcats/5/exercises (20:42:49)
ζ cat 8.c
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int age;
printf("Enter your age:");
scanf("%d", &age);
if (age >= 19 ? true : false) {
printf("You are adult.");
} else {
printf("You are teenager.");
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (20:42:50)
ζ gcc -o 8 8.c
# soneg@soneg: ~/hackingcats/5/exercises (20:42:51)
ζ ./8
Enter your age:80
You are adult.Section 5.2 (9)
- 동일하다.

Section 5.3 (10)
- onettwo 가 출력된다. (case 문에 종료 구문이 존재하지 않아 순차적으로 모두 출력된다.)

Section 5.3 (11)

# soneg@soneg: ~/hackingcats/5/exercises (21:09:03)
ζ cat 11.c
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int area_code;
printf("Enter your area code: ");
scanf("%d", &area_code);
switch (area_code) {
case 229: printf("Albany"); break;
case 404: printf("Atlanta"); break;
case 470: printf("Atlanta"); break;
case 478: printf("Macon"); break;
case 678: printf("Atlanta"); break;
case 706: printf("Columbus"); break;
case 762: printf("Columbus"); break;
case 770: printf("Atlanta"); break;
case 912: printf("Savannah"); break;
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/exercises (21:09:06)
ζ gcc -o 11 11.c
# soneg@soneg: ~/hackingcats/5/exercises (21:09:08)
ζ ./11
Enter your area code: 404
Atlanta% Programming Projects
PP (1)

# soneg@soneg: ~/hackingcats/5/projects (21:13:16)
ζ cat 1.c
#include <stdio.h>
int main(void) {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if ((number / 100) > 0) {
printf("The number %d has %d digits", number, (number / 100));
} else if ((number / 10) > 0) {
printf("The number %d has %d digits", number, (number / 10));
} else {
printf("The number %d has 1 digits", number);
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (21:13:18)
ζ gcc -o 1 1.c
# soneg@soneg: ~/hackingcats/5/projects (21:13:21)
ζ ./1
Enter a number: 23
The number 23 has 2 digits%PP (2)

# soneg@soneg: ~/hackingcats/5/projects (21:19:36)
ζ cat 2.c
#include <stdio.h>
int main(void) {
int hour, minute;
printf("Enter a 24-hour time: ");
scanf("%d:%d", &hour, &minute);
if ((hour / 12) == 1) {
printf("Equivalent 12-hour time: %d:%d PM", hour, minute);
} else if ((hour / 12) > 1) {
printf("Equivalent 12-hour time: %d:%d PM", (hour % 12), minute);
} else {
printf("Equivalent 12-hour time: %d:%d AM", hour, minute);
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (21:19:37)
ζ gcc -o 2 2.c
# soneg@soneg: ~/hackingcats/5/projects (21:19:39)
ζ ./2
Enter a 24-hour time: 12:45
Equivalent 12-hour time: 12:45 PM%PP (3)
- 생략

PP (4)

ζ cat 4.c
#include <stdio.h>
int main(void)
{
int knots;
printf("Enter value of wind speed (in knots): ");
scanf("%d", &knots);
if (knots < 1) {
printf("Description: Calm");
} else if (knots <= 3) {
printf("Description: Light air");
} else if (knots <= 27) {
printf("Description: Breeze");
} else if (knots <= 47) {
printf("Description: Gale");
} else if (knots <= 63) {
printf("Description: Storm");
} else {
printf("Description: Hurricane");
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (21:34:44)
ζ gcc -o 4 4.c
# soneg@soneg: ~/hackingcats/5/projects (21:34:47)
ζ ./4
Enter value of wind speed (in knots): 35
Description: Gale% PP (5)

# soneg@soneg: ~/hackingcats/5/projects (21:46:52)
ζ cat 5.c
#include <stdio.h>
int main(void)
{
int income;
printf("Enter value of your income: ");
scanf("%d", &income);
if (income < 750) {
printf("Amount of tax: 1 percent of income");
} else if (income <= 2250) {
printf("Amount of tax: $7.50 plus 2 percent of income");
} else if (income <= 3750) {
printf("Amount of tax: $37.50 plus 3 percent of income");
} else if (income <= 5250) {
printf("Amount of tax: $82.50 plus 4 percent of income");
} else if (income <= 7000) {
printf("Amount of tax: $142.50 plus 5 percent of income");
} else {
printf("Amount of tax: $230.00 plus 6 percent of income");
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (21:46:53)
ζ gcc -o 5 5.c
# soneg@soneg: ~/hackingcats/5/projects (21:46:55)
ζ ./5
Enter value of your income: 6666
Amount of tax: $142.50 plus 5 percent of income%PP (6)
- 생략

PP (7)

# soneg@soneg: ~/hackingcats/5/projects (21:51:06)
ζ cat 7.c
#include <stdio.h>
int main(void)
{
int a, b, c, d;
int largest, smallest;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
largest = a; smallest = b;
if (largest < b) {
largest = b;
} else if (largest < c) {
largest = c;
} else if (largest < d) {
largest = d;
}
if (smallest > b) {
smallest = b;
} else if (smallest > c) {
smallest = c;
} else if (smallest > d) {
smallest = d;
}
printf("Largest: %d\n", largest);
printf("Smallest: %d\n", smallest);
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (21:51:08)
ζ gcc -o 7 7.c
# soneg@soneg: ~/hackingcats/5/projects (21:51:09)
ζ ./7
Enter four integers: 21 43 10 35
Largest: 43
Smallest: 10PP (8)
- 생략

PP (9)

# soneg@soneg: ~/hackingcats/5/projects (22:11:20)
ζ cat 9.c
#include <stdio.h>
int main(void) {
int yy1, yy2, mm1, mm2, dd1, dd2;
int days1, days2;
printf("Enter first date (mm/dd/yy): ");
scanf("%d/%d/%d", &dd1, &mm1, &yy1);
printf("Enter second date (mm/dd/yy): ");
scanf("%d/%d/%d", &dd2, &mm2, &yy2);
days1 = 365 * yy1 + 30 * mm1 + dd1;
days2 = 365 * yy2 + 30 * mm2 + dd2;
if (days1 < days2) {
printf("%d/%d/%02d is earlier than %d/%d/%02d", dd1, mm1, yy1, dd2, mm2, yy2);
} else {
printf("%d/%d/%02d is earlier than %d/%d/%02d", dd2, mm2, yy2, dd1, mm1, yy1);
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (22:11:22)
ζ ./9
Enter first date (mm/dd/yy): 3/6/08
Enter second date (mm/dd/yy): 5/17/07
5/17/07 is earlier than 3/6/08%PP (10)

# soneg@soneg: ~/hackingcats/5/projects (22:15:47)
ζ cat 10.c
#include <stdio.h>
int main(void)
{
int score;
printf("Enter numeric grade: ");
scanf("%d", &score);
switch (score / 10) {
case 9: printf("Letter grade: A"); break;
case 8: printf("Letter grade: B"); break;
case 7: printf("Letter grade: C"); break;
case 6: printf("Letter grade: D"); break;
default: printf("Letter grade: F");
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (22:15:49)
ζ gcc -o 10 10.c
# soneg@soneg: ~/hackingcats/5/projects (22:15:50)
ζ ./10
Enter numeric grade: 84
Letter grade: B% PP (11)

# soneg@soneg: ~/hackingcats/5/projects (22:20:48)
ζ cat 11.c
#include <stdio.h>
int main(void)
{
int number;
printf("Enter a two-digit number: ");
scanf("%d", &number);
switch (number / 10) {
case 1: printf("ten-"); break;
case 2: printf("twenty-"); break;
case 3: printf("thirty-"); break;
case 4: printf("fourty-"); break;
case 5: printf("fifty-"); break;
case 6: printf("sixsty-"); break;
case 7: printf("seventy-"); break;
case 8: printf("eighty-"); break;
case 9: printf("ninety-"); break;
}
switch (number % 10) {
case 1: printf("one"); break;
case 2: printf("two"); break;
case 3: printf("three"); break;
case 4: printf("four"); break;
case 5: printf("five"); break;
case 6: printf("six"); break;
case 7: printf("seven"); break;
case 8: printf("eight"); break;
case 9: printf("nine"); break;
}
return 0;
}
# soneg@soneg: ~/hackingcats/5/projects (22:20:50)
ζ gcc -o 11 11.c
# soneg@soneg: ~/hackingcats/5/projects (22:20:53)
ζ ./11
Enter a two-digit number: 45
fourty-five%🐛.. 🐛.. 🐛..
