그냥저냥

문제풀이 | The C Programming Language (K&R): Exercise 1-5 본문

개발기/The C Programming Language

문제풀이 | The C Programming Language (K&R): Exercise 1-5

sync86 2026. 5. 4. 17:56
728x90
반응형

책: The C Programming Language (K&R)

본문의 연습문제는 저작권 이슈로 그 내용을 직접적으로 언급하지 않습니다. 이점 양해 바랍니다.

 


Exercise 1-5. 섭씨-화씨 역산과 연산 우선순위

Exercise 1-5 문제는 Exercise 1-3, Exercise 1-4에서 작성 예제를 코드를 역순으로 출력하는 문제입니다.


1. 사전 정보

다만, Exercise 1-3 이나 Exercise 1-4 어디서 작성한 코드를 기준으로 역순으로 출력하라는지 구체적인 언급은 없습니다.

 

여기서 저는 Exercise 1-4 문제를 가지고 풀어보겠습니다.

#include <stdio.h>

/** 
 * print Celsius-Fahrenheit table
 * for fahr = 0, 20, ..., 300
 */

int main(int argc, char** argv)
{
   double fahr    = 0.0;
   int lower      = 1;
   int upper      = 30;
   int step       = 1;
   int celsius = lower;
   printf("============================\n");
   printf("| Celsius-Fahrenheit Table |\n");
   printf("============================\n\n");
   printf("----------------------------\n");
   printf("|    Celsius |  Fahrenheit |\n");
   printf("----------------------------\n");
   while (celsius <= upper) {
       fahr = ((9.0/5.0) * celsius) + 32.0;
       printf("| %10d | %11.2lf |\n", celsius, fahr);
       celsius = celsius + step;
   }
   printf("----------------------------\n");
   return 0;
}

2. 소스코드

#include <stdio.h>

/** 
 * print Celsius-Fahrenheit table
 * for fahr = 0, 20, ..., 300
 */

int main(int argc, char** argv)
{
   double fahr    = 0.0;
   int lower      = 1;
   int upper      = 30;
   int step       = 1;
   int celsius = upper;
   printf("============================\n");
   printf("| Celsius-Fahrenheit Table |\n");
   printf("============================\n\n");
   printf("----------------------------\n");
   printf("|    Celsius |  Fahrenheit |\n");
   printf("----------------------------\n");
   while (celsius >= lower) {
       fahr = ((9.0/5.0) * celsius) + 32.0;
       printf("| %10d | %11.2lf |\n", celsius, fahr);
       celsius = celsius - step;
   }
   printf("----------------------------\n");
   return 0;
}

3. 실행 및 검증

$ ./a.out         
============================
| Celsius-Fahrenheit Table |
============================

----------------------------
|    Celsius |  Fahrenheit |
----------------------------
|         30 |       86.00 |
|         29 |       84.20 |
|         28 |       82.40 |
|         27 |       80.60 |
|         26 |       78.80 |
|         25 |       77.00 |
|         24 |       75.20 |
|         23 |       73.40 |
|         22 |       71.60 |
|         21 |       69.80 |
|         20 |       68.00 |
|         19 |       66.20 |
|         18 |       64.40 |
|         17 |       62.60 |
|         16 |       60.80 |
|         15 |       59.00 |
|         14 |       57.20 |
|         13 |       55.40 |
|         12 |       53.60 |
|         11 |       51.80 |
|         10 |       50.00 |
|          9 |       48.20 |
|          8 |       46.40 |
|          7 |       44.60 |
|          6 |       42.80 |
|          5 |       41.00 |
|          4 |       39.20 |
|          3 |       37.40 |
|          2 |       35.60 |
|          1 |       33.80 |
----------------------------

4. 마무리

위에서 작성한 코드에서 Exercise 1-4와 차이를 남겨보면 아래와 같습니다.

#include <stdio.h>

int main(int argc, char** argv)
{
   /* 생략 */
   
   int celsius = upper;
   
   /* 생략 */
   while (celsius >= lower) {
       /* 생략 */
       
       celsius = celsius - step;
   }
   
   /* 생략 */
}

 

이번 문제에서 섭씨를 순서를 역순으로 하기 위해서는 역으로 1씩 감소해야 합니다.

10 카운트 다운의 예가 적당하겠네요.

10, ... 5, 4, 3, 2, 1

 

때문에 celsius 초기값을 아래와 같이 lower -> upper로 수정하였습니다.

celsius = upper;

 

반복 조건을 수정하였습니다.

celsius가 1보다 크거나 같으냐!

while (celsius >= lower) {
   /* 생략 */
}

 

마지막으로 celsius의 값을 1씩 감소시켰습니다.

celsius = celsius - step;

 

Exercise 1-5 문제는 값을 역으로 출력하는 것을 통해서 반복문에 사용법을 익히라는 문제 같습니다.

문제의 앞 부분에 for, while 문에 대해 언급하고 있네요.

 

다음은 Exercise 1-6 풀어보겠습니다.

728x90
반응형