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

Practice Problem 15

Fundamentals

Question 1-1
What is the term for the memory address assigned to a variable?


Question 1-2
What is the term for the method of assigning the above numbers to variables and handling them?

Program reading
In the following program, the return value of the sum function is of type void, yet...
Please briefly explain why calculations can be returned.

Question 2-1
#include <stdio.h>

void sum(int, int, int *);

int main(void)
{
    int value;
    sum(50, 100, &value);
    printf("%d\n", value);
    return 0;
}

void sum(int min, int max, int *ans)
{
    *ans = (min + max) * (max - min + 1) / 2;
    return;
}

Program Manual

Question 3-1
0~100の範囲で入力された複数のnumericsの中から、
Write a program to find and display the maximum and minimum values.

If -1 is entered, it is determined as the end of input.
"However, the maximum and minimum values should be determined within a function other than the main function."
また、入力されたMemorizing numbers.arrayの要素数は10とし、
If further input is entered, we will accept any resulting errors.

Hint: You can determine the end of the data if there's a -1 in the array.
Hint: To find the minimum value, you can repeat the comparison with a variable that stores the maximum value.

descriptive

Question 4-1
In short, what is a pointer?

Fundamentals (Answer Key)

Solution 1-1
address


Solution 1-2
Pointer

Program Reading (Solution Example)

Solution 2-1
sumfunctionの3つ目のargumentがPointer typeのvariableとして宣言されており、
"Because passing an address allows you to directly modify the contents of the variable."

Program Documentation (Example Solution)

Solution 3-1
#include <stdio.h>

void maxmin(int array[], int *max, int *min);

int main(void)
{
    int i = 0, array[10], max, min;

    do
    {
        printf("%d 番目の数:", i + 1);
        scanf("%d", &array[i]);
        i++;
    } while (array[i - 1] != -1);

    maxmin(array, &max, &min);

    printf("最大値 %d : 最小値 %d\n", max, min);

    return 0;
}

void maxmin(int array[], int *max, int *min)
{
    int i = 0;

    *max = 0;
    *min = 100;

    while (array[i] != -1)
    {
        if (array[i] > *max) *max = array[i];
        if (array[i] < *min) *min = array[i];
        i++;
    }
}

That might have been a bit challenging.
 But if I could write programs this smoothly,
 I'm at a level that just barely cuts it as a professional programmer.
 "Keep practicing and try again!"
descriptive (answer)

Solution 4-1
A shortcut variable for a variable.



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