12.1 C++ Structures and functions
You can create data structures that allow you to group variables together and treat them as a unit. These grouped data types are called structures.

A data structure is any organized way of storing data in a computer. Even a simple variable is a data structure. But the term structures refers to a specific data structure made by grouping other data structures.

In a database program, data is stored in records. Each record is made up of data called fields. With C++ you can create a record by grouping the variables and arrays necessary for the fields into a single structure. The variables in the structure can be of mixed types.

A structure must be declared. Because a structure is made up of more than one variable, a special syntax is used to access the individual variables of a structure. Example:

struct inventory_item
  {
    char item_ID[11];
    char description[31];
    int quantity_on_hand;
    int recorder_point;
    float cost;
    float retail_price;
  };
The struct keyword identifies the declaration as a structure. The identifier associated with the structure is inventory_item. The variables in the structure are called members. The members of the structure are placed within braces. Within the braces the variables and arrays are declared using the regular syntax.

Once you have declared a structure, you must declare a variable that is of the structure's type. You can create as many variables as you want of the new type which was created with the keyword struct. You can also create arrays of structure variables. Example:

//creates a variable of type inventory_item inventory_item todays_special;

//declare an entire array named toys of type inventory_item inventory_item toys[100];

To access a member of a structure, use the name of the variable, a dot operator (period), then the name of the member you need to access. Example:

                 member
                /
todays_special.cost = 47.80;
  \      /    \ 
    \   /      \dot operator
      |
 structure identifier
A structure can include enumerated data types and even other structures as members. When a structure appears within a structure the resulting data structure is called a nested structure. Accessing the nested structure requires that two periods be used. Example:

          nested structure identifier
               |
current_donor.bp.systolic = 130;
\----------/     \------/
main structure     member
   identifier     identifier
12.1 Questions:

T/F (1-5)

1. Structures allow variables to be grouped to form a new data type.

2. The variables in a structure must be of the same type.

3. A structure cannot be part of another structure.

4. A structure can include an enumerated data type.

5. A structure is a good way to represent the x and y coordinates that define a point on a line.

6. What is the term for the variables in a structure?

Problem 12.1.2

Write a program that uses point and line structures to calculate the midpoint of a given line. Have the program ask the user for the points that define the line, then use the formula below to calculate the midpoint of the line and output the coordinates of the midpoint.

((x^1 + x^2)/2,(y^1 + Y^2)/2)