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

Practice Problem 10

Fundamentals

Question 1-1
What is the name for a loop where the condition is checked first, like in a while statement?


Question 1-2
What is the term for a loop where the condition is checked after execution, like a do-while loop?

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 = 0;
    double money = 10000;

    while (money < 15000) {
        year++;
        money *= 1.01;
    }

    printf("%d , %f\n", year, money);

    return 0;
}

Program Manual

Question 3-1
I want to create a program that takes test scores as input.
ただし、テストの点数は0~100までしかないので、
"If anything other than the expected input is entered, prompt the user to re-enter it."

descriptive

Question 4-1
Briefly explain why do-while loops are well-suited for input validation.

Fundamentals (Answer Key)

Solution 1-1
Preliminary screening


Solution 1-2
Post-assessment

Program Reading (Solution Example)

Solution 2-1
Gold利year1%の銀行に10000円を預けた場合に、
A program that displays how many years it will take to reach 15,000 yen.

Program Documentation (Example Solution)

Solution 3-1
#include <stdio.h>

int main(void)
{

    int score;

    do {
        printf("点数を入力して下さい:");
        scanf("%d", &score);
    } while (score < 0 || score > 100);

    printf("入力された点数 %d\n", score);

    return 0;
}


"If you want to display a message when re-entering data, do the following."
The initial input is distinguished by checking if the variable 'score' is equal to 0.
"If a score of 0 is entered, the loop will exit, so the conditions won't overlap."

Answer 3-1 Solution 2
#include <stdio.h>

int main(void)
{

    int score = 0;

    do {
        if (score != 0)
            printf("点数は 0~100の範囲で入力して下さい。\n");
        printf("点数を入力して下さい:");
        scanf("%d", &score);
    } while (score < 0 || score > 100);

    printf("入力された点数 %d\n", score);

    return 0;
}

問題
descriptive (answer)

Solution 4-1
do-while loopはPost-assessmentループなので、初めに必ず1回はrunされ、
To ensure that no input is missed.



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