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

Practice Problem 20

Fundamentals

Question 1-1
What should a file containing only declarations be called?


Question 1-2
What do you call the files where the actual program is written?

Program Manual

Question 2-1

#include <stdio.h>
#include <string.h>

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

void InputPeople(People* data)
{
    printf("名前:");
    scanf("%s", data->name);
    printf("year齢:");
    scanf("%d", &data->age);
    printf("性別(1-男性、2-女性):");
    scanf("%d", &data->sex);
    printf("\n");
}

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

    printf("名前:%s\n", data.name);
    printf("year齢:%d\n", data.age);

    if (data.sex == 1) {
        strcpy(sex, "男性");
    } else {
        strcpy(sex, "女性");
    }

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

Please separate this program into header and source files.
descriptive

Question 3-1
すべてのfunctionを1つのSource fileに記述しても動作するのに、
わざわざminute割する理由を簡潔に述べよ。

Fundamentals (Answer Key)

Solution 1-1
header file


Solution 1-2
Source file

Program Documentation (Example Solution)

Include People.h
/* People.h */
#ifndef __PEOPLE_H__
#define __PEOPLE_H__

#include <stdio.h>
#include <string.h>

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

/* 個人データを入力する */
extern void InputPeople(People* data);

/* 個人データを表示する */
extern void ShowPeople(People data);

#endif


Solve 2-1 People.c
/* People.c */

#include "People.h"

void InputPeople(People* data)
{
    printf("名前:");
    scanf("%s", data->name);
    printf("year齢:");
    scanf("%d", &data->age);
    printf("性別(1-男性、2-女性):");
    scanf("%d", &data->sex);
    printf("\n");
}

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

    printf("名前:%s\n", data.name);
    printf("year齢:%d\n", data.age);

    if (data.sex == 1) {
        strcpy(sex, "男性");
    } else {
        strcpy(sex, "女性");
    }

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

There are various schools of thought on division.
Here, we used the approach of including it within a header file.
It is also possible to perform all #include directives in source files.
descriptive (answer)

Solution 4-1
minute割することでプログラムの見通しが良くなり、
Recyclingや多人数開発が容易になるから。



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