Practice Problem 19
Basics
Question 1-1
What do you call an array that can be freely created during program execution?
Question 1-2
What is the term for a large memory area used for long-term storage?
Program reading
There's a major problem with the following program.
Please briefly explain what the problem is.
Please briefly explain what the problem is.
Question 2-1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int* data;
data = (int*)malloc(sizeof(int) * 10);
return 0;
}
Program Manual
Question 3-1
Based on Exercise 16's problem:
Create a program that displays the names, ages, and genders of three people.
Store the data in a structure.
Use separate functions for data input and display. Modify this program to accept input for any number of people.
Note: Input ends when -1 is entered for age. ※ Since the array index is an int type, it only needs to handle up to the maximum value of int.
Create a program that displays the names, ages, and genders of three people.
Store the data in a structure.
Use separate functions for data input and display. Modify this program to accept input for any number of people.
Note: Input ends when -1 is entered for age. ※ Since the array index is an int type, it only needs to handle up to the maximum value of int.
explanatory
Question 4-1
Even though you can store data using a regular array, explain concisely why you would deliberately use a dynamic array.
Fundamentals (Answer Key)
Solution 1-1
Dynamic array
Solution 1-2
heap
Program Reading (Solution Example)
Solution 2-1
Because the dynamic array has not been freed (the free function was not called),
memory space may be left unused.
memory space may be left unused.
Program Documentation (Example 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-Woman):");
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, "Woman");
}
printf("Gender:%s\n", sex);
printf("\n");
}
Please note that the InputPeople and ShowPeople functions have not been modified.
Modifying the InputPeople function could make it easier to input data.
To reduce calls to the realloc function, increasing by 10 each time.
If a significantly larger volume of data is anticipated, doubling the capacity would be more efficient.
Note: Significant points will be deducted if the free function is not called.
Descriptive (answer example)
Solution 4-1
Because the number of elements can be freely determined during program execution,
memory can be handled efficiently.
memory can be handled efficiently.
About This Site
Learning C language through suffering (Kushi C) isThis 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.




