본문 바로가기

WIL(What I Learned)/KNK C Programming

Chapter.2 Programming Projects

1. Write a program that uses printf to display the following picture on the screen:

       *
      *
     *
*   *
 * *
  *

my sol)

#include <stdio.h>

int main()
{
    printf("       *\n");
    printf("      *\n");
    printf("     *\n");
    printf("*   *\n");
    printf(" * *\n");
    printf("  * \n");
    return 0;
}

2. Write a program that computes the volume of a sphere with a 10-meter radius, using the formula v = 4/3πr^3. Write the fraction 4/3 as 4.0f/3.0f (Try writing it as 4/3. What happens?) Hint: C doesn't have an exponentiation operator, so you'll need to multiply r by itself twice to compute r^3.

참고) https://github.com/raywritescode/cpma2/blob/master/ch02/review/ch02pr03.c

 

GitHub - raywritescode/cpma2: Coding projects from "C Programming: A Modern Approach, Second Edition" by K.N. King

Coding projects from "C Programming: A Modern Approach, Second Edition" by K.N. King - GitHub - raywritescode/cpma2: Coding projects from "C Programming: A Modern Approach, Second E...

github.com

sol)

#include <stdio.h>
#define _USE_MATH_DEFINES // for C
#include <math.h>

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

    float v = (4.0f/3.0f) * M_PI * r * r * r;
    printf("%f", v);

    return 0;
}
  • 변수 r와 v를 int형으로 설정했다가 float형으로 바꾸었다.
  • 변수 r 선언 후 scanf로 값을 입력하고 변수 v를 선언 및 초기화하였다. (초기화가 맞는 표현인지는 확실하지 않다.)
  • 문제에서 하란 한대로 4.0f/3.0f 대신 4/3을 썼을 때는 올바른 값이 도출되지 않았다.
  • 다음은 예시 입출력이다.
2
33.510323%

(구부피 계산기를 이용하니 반지름이 2인 구의 부피는 33.5이다.)


3. Modify the program of Programming Project 2 so that it prompts the user to enter the radius of the sphere.

-> 이미 2번에서 사용자 입력을 받게끔 프로그램을 짰다.


4. Write a program that asks the user to enter a dollars-and-cents amount, then displays the amount with 5% tax added:

Enter an amount: 100.00

With tax added: $105.00

sol)

#include <stdio.h>
int main()
{
    float input;
    scanf("%f", &input);
    printf("Enter an amount: %.2f\n", input);
    float output = input * 105 / 100;
    printf("With tax added: $%.2f", output);

    return 0;
}

 

(입출력 결과)

Enter an amount: 100.00
With tax added: $105.00%

 

(실제 솔루션)

#include <stdio.h>

int main(void)
{
  float original_amount, amount_with_tax;

  printf("Enter an amount: ");
  scanf("%f", &original_amount);
  amount_with_tax = original_amount * 1.05f;
  printf("With tax added: $%.2f\n", amount_with_tax);

  return 0;
}
  • 우선 변수 명명이 다르다.
  • 5% 가산한 값을 구할 때 1.05f를 곱해주었다.

5. Write a program that asks the user to enter a value for x and then displays the value of the following polynomial:

3x^5+2x^4-5x^3-x^2+7x-6

Hint: C doesn't have an exponentiation operator, so you'll need to multiply  by itself repeatedly in order to compute the powers of x. (For example, x * x * x is x cubed.

my sol)

#include <stdio.h>

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

    int polynomial = 3*(x*x*x*x*x) + 2*(x*x*x*x) - 5*(x*x*x)-(x*x)+7*x-6;
    
    printf("The value of the polynomial is: %d", polynomial);

    return 0;
}

입출력 예시)

4
The value of the polynomial is: 3270%

실제 계산 결과)

https://ko.numberempire.com/expressioncalculator.php


6. Modify the program of Programming Project 5 so that the polynomial is evaluated using the following formula:

((((3x+2)x-5)x-1)x+7)x-6

Note that the modified program performs fewer multiplications. This technique for evaluating polnomials is known as Horners' Rule.

Horners' Rule(https://en.wikipedia.org/wiki/Horner%27s_method)

#include <stdio.h>

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

    int polynomial;

    polynomial = ( ( ( (3*x + 2) *x - 5) * x - 1 ) * x + 7) * x -6;
    
    printf("The value of the polynomial is: %d", polynomial);

    return 0;
}

실제 계산 결과)

https://ko.numberempire.com/expressioncalculator.php


7. Write a program that asks the user to enter a U.S. Dollar amount and then shows how to pay that amount using the smallest number of $20, $10, $5, and $1 bills:

Enter a dollar amount: 93

$20 bills: 4
$10 bills: 1
$5 bills: 0
$1 bills: 3

Hint: divide the amount by 20 to determine the number of $20 bills needed, and then reduce the amount by the total value of the $20 bills. Repeat for the other bill sized. Be sure to use integer values throughout, not floating-point numbers.

 

mysol)

#include <stdio.h>

int main()
{
    int amt, bills_20, bills_10, bills_5, bills_1;
    printf("Enter a dollar amount: ");
    scanf("%d", &amt);
    printf("\n\n");

    bills_20 = amt / 20;
    amt -= bills_20 * 20;
    printf("$20 bills: %d\n", bills_20);

    bills_10 = amt / 10;
    amt -= bills_10 * 10;
    printf("$10 bills: %d\n", bills_10);

    bills_5 = amt / 5;
    amt -= bills_5 * 5;
    printf("$5 bills: %d\n", bills_5);

    bills_1 = amt / 1;
    amt -= bills_1 * 1;
    printf("$1 bills: %d", bills_1);
}

 

입출력 예시)

Enter a dollar amount: 93


$20 bills: 4
$10 bills: 1
$5 bills: 0
$1 bills: 3%

8. Write a program that calcuates the remaining balance on a loan after the first, second, and third monthly payments:

Enter amount of loan: 20000.00
Enter interest rate: 6.0
Enter monthly payment: 386.66

Balance remaining after first payment: $19713.34
Balance remaining after second payment: $19425.25
Balance remaining afte third payment: $19135.71​

Display each balance with two digits after the decimal point. Hint: Each month, the balance is decreased by the amount of the payment, but increased by the balance times the monthly interest rate. To find the monthly interest rate, convert the interest rate entered by the user to a percentage and divide it by 12.

 

  • float값에서 소수점 아래 두번째자리까지만 나타내고 싶을 때.
#include <iostream>
using namespace std;
int main()
{
    float var = 37.66666;
 
    // Directly print the number with .2f precision
    printf("%.2f", var);
    return 0;
}

(출처)

 

mysol)

#include <stdio.h>

int main()
{
    float loan;
    float rate;
    float monthly;

    printf("Enter amount of loan: ");
    scanf("%f", &loan);
    printf("Enter interest rate: ");
    scanf("%f", &rate);
    printf("Enter monthly payment: ");
    scanf("%f", &monthly);

    float balance = loan - monthly + loan * ((rate / 100) / 12);
    printf("Balance remaining after first payment: %.2f\n", balance);

    balance = balance - monthly + balance * ((rate / 100) / 12);
    printf("Balance remaining after second payment: %.2f\n", balance);

    balance = balance - monthly + balance * ((rate / 100) / 12);
    printf("Balance remaining after third payment: %.2f", balance);
    
    return 0;
}

예시 입출력)

Enter amount of loan: 20000.00
Enter interest rate: 6.0
Enter monthly payment: 386.66
Balance remaining after first payment: 19713.34
Balance remaining after second payment: 19425.25
Balance remaining after third payment: 19135.71%

 

'WIL(What I Learned) > KNK C Programming' 카테고리의 다른 글

Chapter 2. 연습문제  (0) 2022.11.29