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

Practice Problem 8

Fundamentals

Question 1-1
What is the option to add a statement to an if statement that executes when the condition is false?


Question 1-2
What is the colloquial term for combining multiple sentences like the one above?


Question 1-3
What is the sentence used to separate sentences to be processed by number?

Program reading
What is the purpose of the following program?
Determine the answer based on the processing content and variable names.

Question 2-1
#include <stdio.h>

int main(void)
{
    int year;

    scanf("%d", &year);

    if (year % 2 == 0) {
        if (year % 4 == 0) {
            printf("summer\n");
        } else {
            printf("winter\n");
        }
    } else {
        printf("do not\n");
    }

    return 0;
}

Program Manual

Question 3-1
Write a program that displays the corresponding lunar month when a month is entered.
Also, display a message if a non-existent month is entered.

ヒント:陰暦は1The Moonから順番に、
睦The Moon,如The Moon,弥生,卯The Moon,皐The Moon,Water無The Moon,文The Moon,葉The Moon,長The Moon,神無The Moon,霜The Moon,師走

descriptive

Question 4-1
switch文では各case文の後にbreak文を置くのはなぜか。
また、これが不要なのはどんな場合か簡潔に述べよ。

Fundamentals (Answer Key)

Solution 1-1
else statement


Solution 1-2
else-if statement


Solution 1-3
switch statement or switch-case statement

Program Reading (Solution Example)

Solution 2-1
"Enter a year, and I will tell you if the Olympic Games were held that year."

This program rewrites the problem from the previous chapter using if-else statements.
Program Documentation (Example Solution)

Solution 3-1
#include <stdio.h>

int main(void)
{
    int month;

    printf("The Moonを入力して下さい。");
    scanf("%d", &month);

    switch (month) {
    case 1:
        printf("睦The Moon\n");
        break;
    case 2:
        printf("如The Moon\n");
        break;
    case 3:
        printf("弥生\n");
        break;
    case 4:
        printf("卯The Moon\n");
        break;
    case 5:
        printf("皐The Moon\n");
        break;
    case 6:
        printf("Water無The Moon\n");
        break;
    case 7:
        printf("文The Moon\n");
        break;
    case 8:
        printf("葉The Moon\n");
        break;
    case 9:
        printf("長The Moon\n");
        break;
    case 10:
        printf("神無The Moon\n");
        break;
    case 11:
        printf("霜The Moon\n");
        break;
    case 12:
        printf("師走\n");
        break;
    default:
        printf("そんなThe Moonはありません。\n");
        break;
    }

    return 0;
}

While an if statement is also correct, a switch statement is more suitable when dealing with numerous branches numbered sequentially.
Points will be deducted if the default statement is missing.
Please be sure to remember the break statement.
descriptive (answer)

Solution 4-1
switch文は、対応する番号のcase文までジャンプする機能しかないため、
You need to include a `break` statement at the end of each `case` statement to exit the entire `switch` statement.
However, you can concatenate the `case` statements directly only if there's a commonality in how each case is handled.



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