MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Practice Problem 18

Fundamentals

Question 1-1
What do you call variables that don't change their value within a program?


Question 1-2
What should we call simple functions created using the replace function?

Program reading
In the following program, what is the purpose of the macro TRI?
Determine the answer based on the processing content and variable names.

Question 2-1
#include <stdio.h>

#define TRI(A, H) ((A) * (H) / 2)

int main(void)
{
    int side, high, square;

    scanf("%d,%d", &side, &high);
    printf("%d\n", TRI(side, high));

    return 0;
}

Program Manual

Question 3-1
Practice Problem 11で作成した、
Create a program that displays whether the Olympics are held in a given year.
However, the section calculating the Olympic Games should be created as a separate function.」
"Based on this problem, further modify it to return the function's return value as an enum constant."

descriptive

Question 4-1
numericsを直接書き込むかvariableを使えば良いのにもかかわらず、
わざわざConstantを使うのはなぜか理由を間接にDescriptionせよ。

Fundamentals (Answer Key)

Solution 1-1
Constant


Solution 1-2
macro

Program Reading (Solution Example)

Solution 2-1
A macro to calculate the area of a triangle.

Program Documentation (Example Solution)

Solution 3-1
#include <stdio.h>

int olympic(int year);

enum {
    OLYMIPC_NON,
    OLYMIPC_SUMMER,
    OLYMIPC_WINTER,
};

int main(void)
{
    int year, hold;

    scanf("%d", &year);
    hold = olympic(year);

    switch (hold) {
    case OLYMIPC_NON:
        printf("開かれない\n");
        break;
    case OLYMIPC_SUMMER:
        printf("夏季五輪\n");
        break;
    case OLYMIPC_WINTER:
        printf("冬季五輪\n");
        break;
    };

    return 0;
}

int olympic(int year)
{
    if (year % 2 == 0) {
        if (year % 4 == 0) {
            return OLYMIPC_SUMMER;
        } else {
            return OLYMIPC_WINTER;
        }
    } else {
        return OLYMIPC_NON;
    }
}

By being named for its holding status,
It's clearer than distinguishing with numbers like 0.1.2.
descriptive (answer)

Solution 4-1
Naming things makes them clearer and prepares you for change.
"Since constants cannot be reassigned, there's no risk of accidentally modifying their values, which can prevent bugs from occurring."



About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19