- ..clause 1..: Basic knowledge
- ..clause 2..: Program readout
- Memo
- ..clause 4..: Description type
- ..clause 5..: Basic knowledge(example solution)
- ..clause 6..: Program readout(example solution)
- Memo(example solution)
- ..clause 8..: Description type(example solution)
[ 1-1 ]
What is called the array that can be freely made during program execution?
Example solution
[ 1-2 ]
Is it used for the long term, and what is called a big memory area of the size?
Example solution
[ 2-1 ]
There is a problem in the transplant in the following program.
Explain what problem it is concisely.
#include <stdio.h> #include <stdlib.h> int main() { int i; int *data; data = (int *)malloc(sizeof(int) * 10); return 0; }
Example solution
[ 3-1 ]
Exercise 16
..".. make the program that inputs the name, the age, and sex of three and displays it.
However, data is assumed to be a memory with the structure.
Moreover, the data input and the display are assumed to do making a function respectively special. 」
Remodel even the people how many based on the problem to input it.
If -1 is input to the age, it is assumed the end of input.
- Because the array number is int type, it only has to be able to treat even the maximum value of the int type.
Example solution
[ 4-1 ]
To store data if the array is used
Explain it dares to use a dynamic array concisely why.
[ 1-1 ]
Dynamic array
Problem
[ 1-2 ]
Heap
Problem
[ 2-1 ]
Because it has not liberated a dynamic array
There is a possibility that the memory area uselessly remains.
Problem
[ 3-1 ]
- Though neither InputPeople nor the ShowPeople function have changed#include <stdio.h> #include <string.h> #include <stdlib.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 ("Sex (one-..man.. two-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); Strcpy (sex "Man"); } else { Strcpy (sex "Woman"); } Printf ("Sex: %s\n" sex); printf("\n"); }
If the InputPeople function is changed, it is possible to input easily and to do.
- To decrease the call of the realloc function, ten has been increased.
- When the free function is not called, it is a great demerit mark.
Problem
[ 4-1 ]
Because it is freely decided while programming executing the number of elements
Because the memory can be efficiently treated.