Simple types data

Data types

general characteristics relational data model

The fundamentals of the relational data model were first outlined in an article by E. Codd in 1970. This work served as the impetus for a large number of articles and books in which the relational model received further development. The most common interpretation of the relational data model belongs to K. Date. According to Date, the relational model consists of three parts:

  • Structural part.
  • The whole part.
  • Manipulation part.

Structural part describes what objects are considered by the relational model. It is postulated that the only data structure used in the relational model is normalized n-ary relations.

Integral part describes a special kind of constraint that must be satisfied for any relationship in any relational database. This integrity of entities And foreign key integrity .

Manipulation part describes two equivalent ways to manipulate relational data - relational algebra And relational calculus .

This chapter examines the structural part of the relational model.

Any data used in programming has its own data types.

Important! The relational model requires that the types of data used be simple.

To clarify this statement, let's consider what types of data are usually considered in programming. Typically, data types are divided into three groups:

Simple, or atomic, data types have no internal structure. This type of data is called scalars . Simple data types include the following types:

  • Logical.
  • String.
  • Numerical.

Various programming languages ​​can expand and refine this list by adding types such as:

  • Whole.
  • Real.
  • Date of.
  • Time.
  • Monetary.
  • Enumerable.
  • Interval.
  • Etc.…

Of course, the concept of atomicity is quite relative. Thus, the string data type can be considered as a one-dimensional array of characters, and whole type data - as a set of bits. The only important thing is that when switching to such low level gets lost semantics (meaning) of data . If a string expressing, for example, the last name of an employee is decomposed into an array of characters, then the meaning of such a string as a single whole is lost.

Structured Data Types are intended for specifying complex data structures. Structured data types are constructed from constituent elements called components, which in turn may have structure. The following data types can be considered as structured data types:

  • Arrays
  • Records (Structures)

From a mathematical point of view, an array is a function with a finite domain. For example, consider a finite set natural numbers

called an index set. Display

from set to set of real numbers defines one-dimensional real array. The value of this function for some index value is called the array element corresponding to . Multidimensional arrays can be defined similarly.

A record (or structure) is a tuple of some Cartesian product of sets. Indeed, a record is a named, ordered set of elements, each of which belongs to a type. So the entry is an element of the set . By declaring new record types based on existing types, the user can construct arbitrarily complex data types.

What structured data types have in common is that they have internal structure , used at the same level of abstraction, as the data types themselves.

Let us explain this as follows. When working with arrays or records, you can manipulate the array or record both as a single whole (create, delete, copy entire arrays or records) and element by element. For structured data types there are special functions - type constructors, which allow you to create arrays or records from elements of simpler types.

When working with simple data types, for example, numeric ones, we manipulate them as indivisible whole objects. To "see" that a numeric data type is actually complex (a collection of bits), we need to move to a lower level of abstraction. At the program code level, this will look like assembly inserts into the code in the language high level or the use of special bitwise operations.

The structural algorithmization method is one of the systematic methods for developing algorithms. It is based on a visual representation of algorithms in the form of sequences of control structural fragments.

Each algorithm consists of elementary steps that can be combined into certain algorithmic structures: linear (sequential), branching , cyclical .

Definition 1

Linear is an algorithm design implemented as a sequence of actions (steps), with each action (step) performed only 1 time, after each action (step) the action (step) is increased by 1 until the value is greater than the final parameter of the algorithm .

Linear processes are represented using linear algorithms. Algorithms of this type are used to describe a generalized solution to problems in the form of sequences of modules.

Definition 2

Branching (branching) call an algorithmic design that provides a choice between 2 solutions depending on the values ​​of the input data.

There are two types of branches: incomplete (if something) And complete (if-then-else). Using full branching, you can organize 2 branches in the algorithm ( That or otherwise), each of which will lead to a common point of their merger, the algorithm will be executed regardless of which path the solution took. In the presence of incomplete branching, some actions of the algorithm are assumed on only one branch ( That), since the second one is missing, there is no need to perform any action for one of the check results; control will immediately pass to the merge point. There are 4 basic variants of the branching structure:

  1. Incomplete branching type "if-then ", under which all actions will be carried out if the condition is true.
  2. Full type branching "if - then - otherwise" , in which 2 actions will be performed depending on the truth of the condition.
  3. Branching with type selection "That" , in which action 1 will be performed under condition 1, action 2 under condition 2, etc.
  4. Branching with type selection "otherwise" , under which, under condition 1, action 1 will be performed, under condition 2, action 2, etc., and otherwise all other actions will be performed.

Below are block diagrams of branching algorithms.

Definition 3

Cyclic (or cycle) is an algorithm design in which a certain group of consecutive actions (steps) is performed several times depending on the conditions of the problem and input data.

Definition 4

Such a group of repeated actions at each step of the cycle is called body of the loop .

Any cyclic structure contains elements of a branching structure of the algorithm.

There are 3 types cyclic algorithms:

  • loop with parameter (arithmetic loop);
  • loop with precondition;
  • a loop with a postcondition (the last two are called iterative).

Arithmetic loop

In a loop of this type the number of steps is uniquely determined by the rule for changing the parameter, specified using its initial and final values, as well as the step of its change. That is, at each step of the cycle, the value of the parameter changes according to the step of the cycle until it reaches a value equal to the final value of the parameter.

Loop with precondition

In this loop, the number of steps is not determined in advance; it depends on the input data. In this cyclic structure, the value is first checked conditional expression(conditions) before executing the next step of the cycle. If the conditional expression is true, the body of the loop will be executed. After which the condition will be checked again. These actions will be repeated until the value of the conditional expression becomes false, then the loop will end.

The peculiarity of this type of loop is that if the value of the conditional expression is initially false, the body of the loop will not be executed at all.

Loop with postcondition

In this cyclic construction, as in the previous one, the number of repetitions of the loop body is not determined in advance; it will depend on the input parameters. Distinctive feature of a loop with a precondition is that the body of the loop with a postcondition will in any case be executed at least once and only after that the condition will be checked. In this construction, the body of the loop is executed until the value of the conditional expression is false. Once it becomes true, command execution will stop.

In real problems, as a rule, there are any number of cycles.

Below are block diagrams of cyclic algorithms.

Data types: simple and structured

Real data processed by the program includes integer and real numbers, logical values ​​and symbols. They belong to simple data types and are called basic. All data processed by a computer is stored in its memory cells, each of which has its own address. In programming languages, there are variables that allow you to ignore the addresses of memory cells and access them using a name (identifier).

Definition 5

Variable is a named object (memory cell) that changes its value.

The variable name indicates the value, but the address and method of storing it remain hidden from the programmer. In addition to the name and value, variables have their own type, which helps determine what type of information is in memory.

The variable type is specified by:

  • the method used for recording information into memory cells;
  • the required amount of memory to store it.

For each type, the amount of memory is determined so that any value from the allowable range of values ​​for this type can be placed in it.

Definition 6

Variables that are present in the program throughout the entire period of its operation are called static .

Definition 7

Variables that are created and destroyed at different stages of program execution are called dynamic .Definition 10

Array They call an ordered set of quantities of the same type that have a common name and serial numbers of elements (indices).

The elements of an array are stored in the computer's memory nearby, unlike single elements. Arrays are distinguished by the number of element indices.

A one-dimensional array is characterized by the presence of only one index for each element. Examples of one-dimensional arrays are geometric and arithmetic sequences, which define finite series of numbers.

Definition 11

The number of elements in an array is called dimension .

For a one-dimensional array, its dimension is written next to the name in parentheses.

Elements of a one-dimensional array are entered element by element, in the order necessary to solve a specific problem. If it is necessary to enter the entire array, the elements are entered in ascending index order.

3.2.1 Simple and structured data types. Data structures - records, arrays, lists.

Variables

During programming, it is usually necessary to remember a certain amount of data (intermediate results, events that occurred, input data, output data, etc.). These values ​​must be kept in memory. To do this, a location in memory is declared that is used to store data and this declared location is called a variable. Since the data that is stored can be very different, when declaring a variable, the type of data that will be stored in this variable (variable type) is also declared.

Simple types

A variable of a simple type has keyword one value (often read as a number) is hidden and can be directly accessed. The most famous simple types are: signed integer, unsigned integer, fractional number (with comma), character, boolean value. They may differ slightly in different languages.

Structured types

In the case of structured types, several joint values ​​are grouped under one keyword, such as the coordinates of a point or the first and last name of a person. In this form, the data set is easier to transfer at once. At the same time, you have to use or change data within the structure one at a time.

Arrays

An array is a collection of data of the same type that have the same name and are separated from each other by an index. Arrays greatly facilitate the processing of data of the same type. Ease of processing results from the fact that during program execution you can simply change the index and thus access the required variable more easily. Retrieving the value of a variable from an array using an ordinal number is a fairly fast task for a computer.

Arrays can be one-dimensional (row, row), two-dimensional (table, matrix), three-dimensional (cube), etc.

Example (C#, Java)

int mass = newint ; //create an array to store ten integers

mass=1; //value 1 is written at index 0

Additional reading: http://enos.itcollege.ee/~jpoial/java/i200loeng4.html

Posts

Records are used to store different types of data that together form a related set. For example, a person’s record is formed from the following data: first name (text), last name (text), gender (boolean value, 0 - female, 1 - male), weight (fractional number). These data form one whole when describing one person, however, they themselves are of very different types.

Example (C#)

structinimene {

publicstring eesnimi;

publicstring perenimi;

publicbool sex;

public float weight;

With this entry, we can create a variable kasutaja(user) and assign the user the values ​​of first name, last name, gender and weight:

inimene kasutaja;

kasutaja.eesnimi ="Jaan" ;

kasutaja.perenimi ="Mets" ;

kasutaja.sex = 1;

kasutaja.weight = 80.0;

Lists and trees

Currently, lists are often used to store data. If each element of a list points to the element following it, then it is a linked list; the end of such a list is indicated by an empty element (null). A linked list, where each element points only to the next one, is called a unidirectional list. A linked list, where each element points to the next and previous elements, is called bidirectional. Linked list where the first and first are missing last elements, and each element points to the next one, is called a circular list. The length of a linked list is determined by the number of its elements. The first element of the list is the head and the remaining elements are the tail.

A stack is a linked list in which the last element added is read first (LIFO - Last In First Out).

A queue is a linked list in which the element added first is read first (FIFO - First In First Out).

Additional reading: http://www.cs.tlu.ee/~inga/alg_andm/linked_list_C_2011.pdf

A tree is a data structure in which data is placed in the form of a tree, consists of vertices (English Node) and arcs (English Edges) that connect the vertices (pointers). The vertices that are connected by arcs to the vertex located above are called children, and the vertex located above in this case is the parent. The topmost vertex is the root. A vertex that has no children is called a leaf.

Moving from the top to the parent, and from there to the next parent, etc. we reach the root. Ancestors are all vertices located on the path from the vertex in question to the root. Tree height is determined by the longest path from the leaf to the root.

In the case of an ordered tree, the root and vertices directly connected to it are defined as First level nodes (children of the root), and vertices connected directly to vertices of the first level are second level vertices (children of first level vertices) and etc.; The order of children from left to right is also important.

Additional reading: http://www.cs.tlu.ee/~inga/alg_andm/tree_gen_2011.pdf

A binary tree is a tree in which each parent can have one child, two children, or no children at all, and the order of the children is important.

A binary search tree is a binary tree that is ordered. To the left of the vertex there is always a smaller number and to the right there is always a larger one.

When searching through such a tree, the sought value is compared with the root, and if the sought value is equal to the root, then it exists and is found. If the desired value is not equal to the root, then the comparison operation continues further, respectively, comparing the desired one with a set of vertices located on the right or left until they reach the leaves. If the sought value is equal to the value of one of the vertices, then the sought element is found and exists, but if such a vertex is not found, then the sought element does not exist in this tree. This search method is many times faster than a full traversal of an array or linked list.

A B tree is a search tree in which the number of children at each node is in the range from (t-1) to (2t-1), where t is any constant.

A B*-tree is a B-tree in which the vertices are filled to 2/3 by first filling two child vertices by redistributing the keys and then splitting them into 3 vertices.

Due to this, the B-tree allows you to keep the tree depth less than that of a binary tree. By limiting the padding, it is also possible at intermediate levels to keep the amount of memory used within well-defined limits and at the same time be able to immediately add data to the appropriate location.

Parameter name Meaning
Article topic: Structured Data Types
Rubric (thematic category) Programming

Data structured type consist of data of other types. Variables of these types can only have one value at a time. Structured data types include:

o Strings;

o Arrays;

o Sets;

o Records;

o Files;

o Classes.

Strings (string types): represented by three physical and one general types.

Data type ShortString represent a string, which is actually an array of 256 elements - array. The zero byte of this array indicates the length of the string. Line - ϶ᴛᴏ sequence of code table characters.

Type data AnsiString And Wide String are dynamic arrays, the maximum length of which is actually limited by the size of the computer's main memory. Data type AnsiString m are encoded in code ANSI, but like Wide String- in code Unicode.

The common type is String, which can match the type ShortString or AnsiString, which is determined by the compiler directive $H.

Since strings are actually arrays, to access an individual character in a string, you can specify the name of the string variable and the number (position) of this character in square brackets.

String type description format:

Type<имя типа> = string[max string length];

Otherwise: var<имя переменной, ... >: string[max string length];

If the maximum allowed line length is not specified, the default length is 255 characters. When used in expressions, the string consists of apostrophes. String data can be used as constants. It is not allowed to use string variables as a selector in a statement Case.

Example: const Addresses = 'ul. Korolenko, 5’;

type Stroka = string;

var Str: Stroka; St1: string; St2, St3: string;

Arrays: array - ϶ᴛᴏ an ordered indexed collection of elements of the same type that have a common name. Elements of arrays can be data of any type, including structural ones. Each element of the array is uniquely identified by the name of the array and an index (the number of this element in the array) or indices if the array is multidimensional. To refer to an individual element of an array, indicate the name of this array and the element number(s) enclosed in square brackets, for example, arr1 or arr2.

The number of index positions determines the size of the array (one-dimensional, two-dimensional, etc.), and the size of the array is not limited. In mathematics, the analogue of a one-dimensional array is a vector, and two-dimensional array- matrix. Array element indexes must be of ordinal type.

There are arrays static and dynamic . Static array is an array, the boundaries of the indices and, accordingly, the dimensions of which are specified when declaring, ᴛ.ᴇ. they are known before the program is compiled. Format for describing a static array type:

Type<имя типа> = Aggau [<тип индексов>] of<тип элементов >;

Otherwise: var<имя переменной, ...>: Aggau[<тип индексов>] of <тип элементов >;

Example.
Posted on ref.rf
type Matrix = a ggау of integer;

Znak = array of char;

Day =(Mon, Tue, Wed, Thu, Fri, Sat, Sun);

var m1, m2: Matrix; a: Znak;

Week: array of Day; r: array of real;

Dynamic array is an array for which, when declared, only the type of its elements is indicated, and the size of the array is determined during program execution. Dynamic array type description format:

Type<имя типа> = Aggau of <тип элементов >;

The size of a dynamic array is set during program execution using the procedure SetLength (var S; NewLength:integer), which for a dynamic array S sets the new size to NewLength. You can perform operations with a dynamic array and its elements only after setting the dimensions of this array.

After setting the size of a dynamic array, the functions are used to determine its length, minimum and maximum element numbers Length(), Low() And High() respectively. The numbering of dynamic array elements starts from zero, therefore the function Low() it always returns zero.

Example.
Posted on ref.rf
Var n: integer;

m: array of real;

SetLength(m, 100);

for n:=0 to 99 do m[n]:=n;

SetLength(m, 200);

After describing a dynamic array consisting of real numbers, the size of this array is determined to be 100 elements. Each element is assigned a value equal to its number in the array. Since the numbering of array elements starts from zero, the number of the last one is not 100, but 99. After the loop, the size of the array increases to two hundred.

To describe the type multidimensional dynamic array (for example, two-dimensional) the following construction is used:

Type<имя типа> = Aggau of Aggau of<тип элементов >;

Actions on an array are usually performed element by element, incl. input and output operations. Element-by-element processing of arrays is usually done using loops. The array as a whole (as a single object) can only participate in relational operations and in the assignment operator, and the arrays must be completely identical in structure, that is, have indexes of the same types and elements of the same types.

Sets: many is a collection of elements selected from a predefined set of values. All elements of a set are of ordinal type; the number of set elements cannot exceed 256. Format, plural type descriptions:

Type<имя типа> = Set of<тип элементов >;

A variable of multiple type can contain from zero to maximum number elements of its set. Multiple values ​​are enclosed in square brackets. An empty set is denoted by . The operations allowed on sets are given in the table.

However, there is an operation in(membership test), which determines whether an expression of ordinal type (the first operand) belongs to a set (the second operand). The result of the operation will be like boolean and make a difference True if the value belongs to a set.

Posts: z records combine a fixed number of data elements of other types. Individual elements records have names and are called fields . The field name must be unique within the record. Distinguish fixed and variant records . Fixed entry consists of a finite number of fields, its declaration has the following format:

Type<имя типа> = record;

<имя поля­_1>: <Тип поля>;

<имя поля_ n >: <Тип поля>;

Variant entry, like fixed, has a finite number of fields, but provides the opportunity to interpret the memory areas occupied by the fields differently. All recording options are located in one memory location and allow you to access them by different names. Note that the term “variant record” has nothing in common with the term “variant type” ( variant). Variant entry declaration format:

Type<имя типа> = record;

Case<Признак>: <Тип признака> of;

<вариант_1>: (<описание варианта_1>)

<вариант_ n >: (<описание варианта_ n >);

To refer to a specific field, it is essential to specify the record name and field name, separated by a dot. However, the field name is compound. You can perform the same operations with a field as with a variable of this type.

Example.
Posted on ref.rf
var Man: record;

Man.Name:=’Ivanov M.A.’;

Man .Salary:=5000;

The Man variable is a fixed record that contains the Name, Salary, and Note fields, each field having its own type.

Files: File is a named sequence of elements of the same type placed on external device, most often, on disk. The file has much in common with a one-dimensional dynamic array, but is located not in the operating system, but in external memory, and does not require prior indication of size.

To perform operations with a specific file located on disk, the program usually uses the so-called file variable (logical file). A file variable, after its description, is associated with a certain file, so that operations performed on it lead to corresponding changes in this file. After all operations are completed, the connection between the file variable and the file is broken. Now the file variable can be associated with another file of the same type.

Taking into account the dependence on the type of elements, they distinguish text, typed and untyped files . Text file contains strings of variable length characters, typed file constitute elements of the specified type (except file), in untyped file there are elements whose type is not specified. The description of a file variable intended to work with a file must correspond to the type of file elements.

Example.
Posted on ref.rf
var f1: TextFile;

f2: File of integer;

f3: File of real;

here the f1 variable is intended to work with text files, the f2 and f3 variables are for typed files containing integer and real numbers, respectively, and the f4 variable is for untyped files.

Structured data types - concept and types. Classification and features of the category "Structured data types" 2017, 2018.

Structured types are characterized by the multiplicity of elements that form this type, i.e. have several components. Each component, in turn, can belong to a structured type, i.e. Nesting of types is allowed.

Arrays represent a formal union of several objects of the same type (numbers, symbols, strings, etc.), considered as a single whole. All array components are data of the same type.

General view of an array definition:

Type A = array [array index type] of [array component type]

For example, M1=array of real;

Strings is an array of characters, but the number of characters in a line can vary. The string is treated as a chain of characters of arbitrary length. The maximum number of characters is no more than 255. Each character in the line has its own index (number).

Record is a data structure consisting of a fixed number of components called record fields. Unlike an array, the components of a record (fields) can be various types. Records allow you to combine values ​​of different types.

Month: (Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct, Nov, Dec);

Year: 2000..2050;

Sets– these are sets of similar objects that are logically connected to each other. The number of elements included in a set can vary from 0 to 256. It is the inconstancy of their elements that sets sets differ from arrays and records.

Digits = Set of 1..5;

File– named area of ​​external memory. A file contains components of the same type other than files (i.e., you cannot create a "file of files"). The file length is not specified and is limited only by the capacity of external memory devices.

F: File of Integer;

We will become more familiar with structured types as we further study the language.

      1. Pointer (reference type)

Contains the address of a memory byte that contains a data value of a certain type. This type is also called reference type. The description uses the ^ character and a type identifier. For example, P=^integer;

Using pointers is a flexible control tool dynamic memory and provides the ability to process large-scale data arrays.

    1. Constants

Constant is a quantity whose value does not change during program execution.

    Numerical constants are used to write numbers. The following types are distinguished:

Whole numbers: written with a + or - sign, or without a sign, according to the usual arithmetic rules: -10 +5 5

Real numbers can be written in one of two forms:

regular entry : 2.5 -3.14 2. - please note that whole part separated from the fractional symbol by a dot;

exponential form: in this notation, a real number is represented as m*10 p, where m is mantissa or number base, 0.1≤|m|≤1, p – order numbers, this is an integer constant. Indeed, any real number can be represented in exponential form:

153.5 -0.1535*10 3

99.005 0.99005*10 2

All IBM-compatible computers store real numbers as a combination of mantissa and exponent, allowing operations on them to be simplified using special arithmetic that handles mantissa and exponent separately. To programmatically write a number in exponential form, instead of “multiply by 10 to the power”, use the notation E or e(Latin):

153.5 -0.1535*10 3 -0.1535E3 or -1.535E02

99.005 0.99005*10 2 0.99005E+2 or 9.9005e+01

Without taking special measures, a Pascal program will display real numbers on the screen and printer in exactly this form. In addition, this form is convenient for writing very small and very large numbers:

Since the size of memory allocated for the mantissa and order is limited, then real numbers are always represented in computer memory with some error. For example, the simplest real fraction 2/3 gives 0.666666 in decimal representation... and, regardless of the size of memory allocated to store the number, it is impossible to store All its signs in the fractional part. One of the typical programming problems is taking into account possible errors when working with real numbers.

Hexadecimal numbers consist of hexadecimal digits preceded by a $ sign. The range of hexadecimal numbers is from $00000000 to $FFFFFFFF.

In addition to numerical constants, there are other types of constants:

    brain teaser constants.

They serve to check the truth or falsity of certain conditions in the program and can only accept one of two values: function word true stands for truth and false- lie;

    Character constants.

Can take on the value of any printable character and are written as the character enclosed in apostrophes("single quotes"):

In the latter case, the value of the character constant is equal to the space character. If you want to write the apostrophe symbol itself as a character constant, it is doubled inside outer apostrophes: """"

Character constants also include constants of the form #X, where X is a numeric value from 0 to 255 inclusive, representing a decimal ASCII-code symbol. Tables of ASCII codes used by the DOS and Windows operating systems are given in Appendix 1. For example, the value #65 would correspond to the Latin character code "A".

    String constants.

These are any sequences of characters enclosed in apostrophes. As a rule, string constants are used to record prompts for data input issued by the program, display diagnostic messages, etc.:

"Enter X value:"

If it is necessary to write the apostrophe character itself in a string constant, this is done in the same way as for character constants.

Constants in Turbo Pascal can be named. Unnamed constants are used, for example, when displaying the text of messages in the previous example. Named Constants are described in the program description section by an operator of the following form:

const Name1=Value1;

Name2=Value2;

NameN=ValueN;

Here, the const keyword indicates the beginning of the section for named constant declarations. It is clear that it is often more convenient to refer to a constant by name than to rewrite its numeric or string value each time. Example of a constants section:

const e=2.7182818285;

lang="Turbo Pascal 7.1";

This describes a numeric constant e with the value of the base of the natural logarithm and a string constant named lang containing the string "Turbo Pascal 7.1".

Each name given by the programmer must be unique within one program. If we include this section in our program, we will no longer be able to create other objects named e and lang in it.