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

Practice Problem 6

Fundamentals

Question 1-1
What function is used to input numbers from the keyboard?


Question 1-2
What symbol do you put before variable names when using the functions mentioned above?

Program reading
What is the purpose of this program, i.e., what does it calculate?
Determine the answer based on the processing content and variable names.

Question 2-1
#include <stdio.h>

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

    scanf("%d,%d", &side, &high);
    square = side * high / 2;
    printf("%d\n", square);

    return 0;
}

Program Manual

Question 3-1
"When you enter the original price, it will calculate the prices with 10%, 30%, 50%, and 80% discounts."Create a program to list items.While it's preferable to display the result as a whole number, a decimal value is also acceptable.

descriptive

Question 4-1
In reality, the scanf function is rarely used in programs where reliability is required.
Explain it briefly, why?

Fundamentals (Answer Key)

Solution 1-1
scanf function

While I haven't introduced them yet, there are many other similar functions available.

Solution 1-2
&

Program Reading (Solution Example)

Solution 2-1
三角形の面積を計算するプログラムであることが、
"It can be understood from variable names (side, height, area) and the process (side x height ÷ 2)."

If you can somehow figure it out, even without a detailed explanation, that's considered correct.
It might have been a rather difficult problem.
 However, to continue developing programs, it is essential to read programs written by others.
 Let's strive to understand not just the program's processing, but also its purpose!
Program Documentation (Example Solution)

Question 3-1
#include <stdio.h>

int main(void)
{
    int price;

    printf("定価を入力して下さい : ");
    scanf("%d", &price);

    printf("1割引 = %d円\n", (int)(price * 0.9));
    printf("3割引 = %d円\n", (int)(price * 0.7));
    printf("5割引 = %d円\n", (int)(price * 0.5));
    printf("8割引 = %d円\n", (int)(price * 0.2));

    return 0;
}


Execution results
定価を入力してください : 198
1割引 = 178円
3割引 = 138円
5割引 = 99円
8割引 = 39円

"※ Here, we are representing a 10% discount as 0.9, but if you want to explicitly state it as a 10% discount,"
"(int)(price * (1 - 0.1)) will likely apply a 10% discount."
"You can create variables for each discount and assign values to them."
"The result may be a real number."
Depending on the calculation method, the results may vary slightly, but if the method is correct, the answer is considered correct.
"Be careful not to forget the & when assigning variables with the scanf function."
descriptive (answer)

Solution 4-1
The `scanf` function cannot check for input errors.



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