learn through suffering C language learn through suffering 
C language

Practice Problem 9

Basics


Question 1-1
What is the colloquial name for the variable used to count repetitions in a for loop?


Question 1-2
What do we colloquially call a `for` loop that runs forever?

Program reading

What program is intended to display what?
Determine the answer based on the processing content and variable names.

Question 2-1
#include <stdio.h>

int main(void)
{
    int i, price;

    scanf("%d", &price);

    for (i = 1; i <= 9; i++) {
        printf("%d Price Down = %d\n", i, (int)(price * ((10.0 - i) / 10)));
    }

    return 0;
}

Program Manual


Question 3-1
Write a program that uses a for loop to display a multiplication table.

Hint: Use the %2d specifier to align tables.
Hint: You can use a for loop inside another for loop.

explanatory


Question 4-1
Briefly explain how a for loop with a constant number of iterations works.

Fundamentals (Answer Key)


Solution 1-1
Count variable or loop variable


Solution 1-2
Infinite loop

Program Reading (Solution Example)


Solution 2-1
A program that displays prices with discounts ranging from 1% to 9% when a fixed price is entered.

The discount rate is calculated by dividing 10.0 by i, and then dividing the result by 10.

Program Documentation (Example Solution)


Question 2-1
#include <stdio.h>

int main(void)
{
    int x, y;

    for (x = 1; x <= 9; x++) {
        for (y = 1; y <= 9; y++) {
            printf(" %2d ", x * y);
        }
        printf(" \n");
    }
    
    return 0;
}

printf(\n);
You can use `for` loops as well as other blocks (compound statements) in any level of nesting.
 However, please pay attention to code readability.

Descriptive (answer example)


Solution 4-1
Initially set the variable to 1, increment it by 1 each time, and terminate the loop when the variable's value exceeds the specified number of iterations. This achieves a loop with a fixed number of iterations.



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. line break
  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

Loading comment system...