learn through suffering C language learn through suffering 
C language

Practice Problem 15

Basics


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
Create a program that finds and displays the maximum and minimum values from multiple numbers entered within the range 0 to 100.

If -1 is entered, treat it as the end of input.
However, the maximum and minimum values must be calculated within a single function outside the main function.
Also, set the array size to store the entered numbers to 10 elements.
If more than 10 numbers are entered, it is acceptable for an error to occur.

Hint: If -1 is present in the array, it can be determined that data input has ended.
Hint: To find the minimum value, repeatedly compare each number with the variable storing the maximum value.

explanatory


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
The third argument of the sum function is declared as a pointer-type variable,
and passing an address to it allows the contents of the variable to be directly overwritten.

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("%dth number:", i + 1);
        scanf("%d", &array[i]);
        i++;
    } while (array[i - 1] != -1);

    maxmin(array, &max, &min);

    printf("maximum value %d : minimum value %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 example)


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