MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Exercise 19

fundamental knowledge

Q1-1
What do you call an array that can be freely created during program execution?


Q 1-2
What do you call a memory area of large size that is used over a long period of time?

program read-only
The following program has a major problem.
Briefly explain what kind of problem it is.

Q2-1
 #include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int* data;

    data = (int*)malloc(sizeof(int) * 10);

    return 0;
}

program writing

Q3-1
Exercise 16.
"Create a program that will enter and display the names, ages, and genders of three people.
The data should be stored in a structure.
In addition, dedicated functions should be created for data input and display, respectively."
Based on the problem "How many people can be inputted?
The input is terminated when -1 is entered for age.
Since the array number is an int type, it should be able to handle up to the maximum value of the int type.

descriptive expression

Q4-1
You can store data using regular arrays, but you can't use arrays to store data.
Briefly explain why you dare to use dynamic arrays.

Basic Knowledge (sample answers)

Solution 1-1
dynamic array


Solution 1-2
heap

Program reading (example solution)

Solution 2-1
Because the dynamic array is not deallocated (because you forgot to call the free function)
Memory space may remain wasted.

Program writing (example of solution)

Solution 3-1
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[256];
    int age;
    int sex;
} People;

void InputPeople(People* data);
void ShowPeople(People data);

int main(void)
{
    int i, count, datasize;
    People* data;

    datasize = 10;
    data = (People*)malloc(sizeof(People) * datasize);

    count = 0;

    while (1) {
        InputPeople(&data[count]);
        if (data[count].age == -1)
            break;
        count++;

        if (count >= datasize) {
            datasize += 10;
            data = (People*)realloc(data, sizeof(People) * datasize);
        }
    }

    for (i = 0; i < count; i++) {
        ShowPeople(data[i]);
    }

    free(data);

    return 0;
}

void InputPeople(People* data)
{
    printf("Name:");
    scanf("%s", data->name);
    printf("Age:");
    scanf("%d", &data->age);
    printf("Gender (1-male, 2-female):"); scanf("%d", &data->age); printf("%d", &data->age); scanf("%d", &data->age)
    scanf("%d", &data->sex);
    printf("\n");
}

void ShowPeople(People data)
{
    char sex[16];

    printf("Name:%s\n", data.name);
    printf("Age:%d\n", data.age);

    if (data.sex == 1) {
        strcpy(sex, "male");
    } else {
        strcpy(sex, "female");
    }

    printf("Gender:%s\n", sex);
    printf("\n");
}

*InputPeople and ShowPeople functions have not been changed, but
The InputPeople function can be changed to make it easier to input data.
*Increasing by 10 to reduce the number of calls to the realloc function.
If larger amounts of data are expected, it is more efficient to double the number of data.
*Major points are deducted if the FREEFREE function is not called.
Short Answer Type (Sample Answers)

Solution 4-1
The number of elements can be freely determined during program execution.
Because memory can be handled efficiently.



About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box