31.01.2019 Learnpascal

Since a new section is opening on the site on Sunday - problem solving, you and I should quickly learn the main part of mathematical operations, functions and procedures.

Let's understand what a function and a procedure are. This is a subroutine - a part of a program that performs a specific algorithm and can be accessed from various parts general program. What is the difference between a procedure and a function?

Procedures are mini-programs.

Procedures are used when multiple results need to be obtained within a subroutine. From the picture below you can see how the procedure works. There may be no input data at all, or there may be a hundred.

For example, a programmer wants to write 20 ampersands between blocks of output values ​​in his supercode. To make things easier for himself, he will write a simple subroutine.

Program superduper; var is a lot of letters; procedure ampersand; begin write("&&&&&&&&&&&&&&&&&&&&"); end; begin super complex code; ampersand; super complex code; ampersand; super complex code; ampersand; super complex code; ampersand; end.

Functions in Pascal are mega variables.

Functions differ from procedures in that after executing a function, one number, letter, string, etc. is placed in its place in the code. The set of built-in functions in the Pascal language is quite wide. For example, in order to calculate the square of a number, you can use the standard function sqr(x). As you probably already understood, sqr(x) requires only one actual parameter - a number.

Example: a:=sqr(4).

Note! Functions must be assigned! Just writing them in the text of the program, as procedures, will not achieve anything!

The structure of the function is shown in the picture below.

If a new unique function needs to be included in a program, it must be described in the same way as a procedure. We will talk in more detail about how to create your own procedures and functions in 10 lessons. Below you see a table of the main standard functions and procedures in Pascal.

Mathematical functions

NameArgument typeCalculation resultExample
Abs(x)Whole or Thing.Module xAbs(-6) = 6
Sqrt(x)RealRoot of xSqrt(25)=5
Sqr(x)Whole and Thing.Square xSqr(5)=25
Power(x, a)RealValue x aPower(5,3)=125
Frac(x)RealFractional part xFrac(5.67)=0.67
Sin(x)RealSine xSin(45)=0.8509
Cos(x)RealCosine xCos(45)=0.5253
Arctan(x)RealArctangent xArctan(5)=1.3734
Int(x)RealWhole part xInt(5.67)=5.0
Random(x)WholeRandom number(0..x-1)Random(5)=4
Succ(x)OrdinalNextSucc(10)=11
Pred(x)OrdinalPreviousPred('Z')='Y'

Mathematical procedures

Inc(x, a)WholeX:=X+AInc(5)=6
Dec(x, a)WholeX:=X-ADec(25,20)=5

Type Conversion

Trunc(x)RealWhole part xTrunc(5.67)=5
Round(x)RealRounding x to the nearest integerRound(5.67)=6
Important! If x = 5.5, then the result is 6, and if x = 6.5, then the result is also 6!?

Div and mod operations.

Sometimes we need to find the quotient or the remainder of a division. At such moments, operations such as div and mod come to our aid. Note that these operations are performed only on integers.

Div

In order to find the quotient of division, we use the div operation.

  • 25 div 20 = 1;
  • 20 div 25 = 0;
  • 39 div 5 = 7;
  • 158 div 3 = 52.

Mod

In order to find the remainder of division, we use the mod operation.

  • 25 mod 20 = 5;
  • 20 mod 25 = 0;
  • 39 mod 5 = 4;
  • 158 mod 3 = 2.

To finally understand what we are dealing with, let’s solve the following problem:

Task 1.Find the sum of the digits of a two-digit number.

Since this task is very simple, we will make do with a flowchart and a program.

Flowchart of program Sumoftwo; var Number, Num1, Num2, Sum: integer; begin write("Enter a two-digit number: "); read(Number); ( Let's take the number 25 ) Num1:= Number div 10; ( 25 div 10 = 2 ) Num2:= Number mod 10; ( 25 mod 10 = 5 ) Sum:= Num1 + Num2; ( 2 + 5 = 7 ) write("The sum of two numbers is ", Sum); end.

Task 2.Find the sum of the digits of a three-digit number.

A slightly more complicated version of the previous problem. The biggest difficulty is the second number.


The priority of div and mod is greater than the priority of + and -. Therefore, in this program you can do without parentheses. program Sumoftree; var Number, Sum: integer; begin write("Enter a three-digit number: "); read(Number); ( Let's take the number 255 ) Sum:= Number div 100 + Number mod 10 + Number div 10 mod 10; ( 255 div 100 + 255 mod 10 + 255 div 10 mod 10 = 12 ) write("The sum of three numbers is ", Sum); end.

That's all. In the next lesson we will begin to study the features of PascalABC.Net.




For example: 11 div 5 = 2 10 div 3 = 3 2 div 3 = div 4 = div -5 = div 5 = div -5 = 3 10 mod 5 = 0 11 mod 5 = 1 10 mod 3 = 1 14 mod 5 = 4 17 mod - 5 = mod 5 = mod -5 = -2


0 and b>0 is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note the mod operation can be used to find out whether an integer is a multiple of an integer " title=" Relationship between the operations div and mod The arguments of the operations div and mod are integers. For a>0 and b>0 the following is true: A mod b = a – (a div b)*b (a div b) *b + (a mod b) = a Note that the mod operation can be used to find out whether an integer is a multiple of an integer" class="link_thumb"> 6 !} Relationship between the div and mod operations The arguments of the div and mod operations are integers. For a>0 and b>0 the following is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note the mod operation can be used to find out whether integer a to integer b. Namely, a is a multiple of b if and only if a mod b = 0 0 and b>0 is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note the mod operation can be used to find out whether an integer is a multiple of an integer "> 0 and b>0 is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note the mod operation can be used to find out if an integer is a multiple a to the integer b. Namely, a is a multiple of b if and only if a mod b = 0"> 0 and b>0 is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note that the mod operation can be used to find out whether an integer is a multiple of the integer" title="Relationship between the div and mod operations The arguments of the div and mod operations are integers. For a>0 and b>0 is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note the mod operation can be used to find out whether an integer is a multiple of an integer"> title="Relationship between the div and mod operations The arguments of the div and mod operations are integers. For a>0 and b>0 the following is true: A mod b = a – (a div b)*b (a div b)*b + (a mod b) = a Note the mod operation can be used to find out whether whole as a whole"> !}


Determine the number of remaining drains if they were divided into 5 people write (Number of drains = "); readln (a); b:= a mod 5; writeln(Remaining, b, drain);


Relational operations Not equal to Less than = Equal to Greater than = Less than or equal to Greater than or equal to = Less than or equal to Greater than or equal to"> = Less than or equal to Greater than or equal to"> = Less than or equal to Greater than or equal to" title="Relational operations Not equal Less than = Equal to Greater = Less than or equal to Greater or equal to"> title="Relational operations Not equal to Less than = Equal to Greater than = Less than or equal to Greater than or equal to"> !}






Calculate the hypotenuse of a right triangle (length of legs - a and b) write ("a="); readln(a); write("b="); readln(b); c:= sqrt (sqr(a) + sqr(b)); writeln("c=", c:5:2);


Calculate the modulus of the difference between numbers a and b write ("a="); readln(a); write("b="); readln(b); c:= abs (a - b); writeln("module=", c);






Exponent and logarithm Exp (x) ln x (natural logarithm) Ln (x) e x (number exponent, e) Exp (b*Ln (a)) ab ab










Examples of using DIV and MOD: Operations DIV and MOD are often used to analyze numbers, for example to derive the digits that make up a number. Task: Enter a three-digit number from the keyboard. Determine the sum of its digits and output these digits in reverse order.


Let the variable a contain the value of a given number. We denote the digits of number a as follows: i - the number of hundreds; j – number of tens; k - number of units; s is the sum of these numbers. Program MyNumber; UsesCrt; Var a, i, j, k, s: integer; Begin clrscr; Writeln (Specify a 3-digit number); Readln(a); i:=a div 100; (number of hundreds) j:=a div 10 mod 10; (tens) k:=a mod 10; (number of units) s:=i+j+k; Writeln(Sum of digits of number,a,=,s); Writeln(k, j, i); Readln; End.


Standard functions Pascal's functionMathematical notation Name Abs(x)|X||X| Absolute value of the number X (modulus) Sqr(x)Х2Х2 Raising a number Exp(x)exex Exponent Sqrt (x) x Calculating the square root Exp(b*ln(a)) abab Raising a number Round (x) Rounds to the nearest integer Trunc (x) Cuts off the fractional part Sin(x)sinx Calculation of the sine Cos(x)сosx Calculation of the cosine



Answers 1.1.68 4.2.06 5.1.10 6.2.16 7.3.05 8.0.10


2. A three-digit number is given. The first number on the left was crossed out and added to the right. Print the resulting number. (For example,) 3. A three-digit number is given. Print the number obtained by rearranging the tens and units digits of the original number. (For example,)

Arithmetic operations of Pascal ABC language

PROGRAM STRUCTURE. DESCRIPTION OF VARIABLES IN PASCAL ABC

Goal of the work Study the structure of the program. Master the user's job of describing variables various types when working in an integrated PASCAL environment ABC 7.0.

SIGN EXPRESSION OPERATION
+ A+B ADDITION
_ A–B SUBTRACTION
* A*B MULTIPLICATION
/ A/B DIVISION
DIV A div B WHOLE DIVISION
MOD A mod B REMAINDER OF A WHOLE DIVISION
Function Function
Frac(x) Fractional part x
Int(x) Whole
Ln(x) Natural logarithm
Pi Constant value π
Abs(x) Absolute value (modulus of number)
Arctan(x) Arctanges x
Cos(x) Cosine x
Exp(x) e - expansion
Random Random number from 0 to 1
Random(n) Random number from 0 to n
Odd(x) True if x is odd False if x is even
Sin(x) Sine x – (in radians)
Sqr(x) Argument Square
Sqrt(x) Square root
Trunc(x) The nearest integer that does not exceed the modulo argument (cutting off the fractional part of the number x)
Round(x) Round to the nearest integer argument

Tasks 1

Calculate monthly payments m on a loan of s rubles for n years at interest p. Calculations are performed using the formulas:

Type the program text:

Do it yourself:

1. The lengths of three sides of a triangle are given a , b , c . Calculate the perimeter and area of ​​a triangle using Heron's formula

2. Calculate the value of the expression using the formula (all variables take real values):

1. Run the program and check its operation;

2. View the result of the program;

Standard language features Pascal programming are given in table. 1

Table 1:

Function name Operation to be performed
ABS(X) Calculates the modulus of the argument x, the type of x is real or integer, the type of the result is the same as the type of the argument
SQR(X) Calculates the square of the argument (x 2), the type of x is real or integer, the result matches the type of the argument
SQRT(X) Calculates the square root of the argument x (x>0); type x – real or integer, result type is real
SIN(X) Calculates the sine of the argument x (x – in radians); type x – real or integer, result type is real
COS(X) Calculates the cosine of the argument x (x – in radians); type x – real or integer, result type is real
ARCTG(X) Calculates the arctangent of the argument x (x – in radians); type x – real or integer, result type is real
EXP(X) Raising the number e=2.71828 to the power x (e x), type x – real or integer, result type real

These functions are contained in the memory of the Pascal programming environment and are routines for calculating the most commonly used functions using iterative methods.

Examples of drawing up a linear program

Example 1 Find the arithmetic mean of three numbers - two integers (X and Y) and one real (Z) and the square of the arithmetic mean.

Program:

Program middling;

X, Y: Integer;

Z, Midd, SqrMidd: ​​Real;

WriteLn("Enter two integers X and Y:");

ReadLn(X,Y);

WriteLn("Enter real number Z:");

ReadLn(Z);

Midd:=(X+Y+Z)/3;

SqrMidd:=SQR(Midd)

Writeln("Arithmetic mean = ",Midd);

Write("Square of the arithmetic mean = ",SqrMidd);

Program description

The title of the program indicates the name of the program - Midding (middle), then the word var opens the section for describing variables: X and Y are integers, Z is real. The word begin opens the main block of the program, in which:

▪ the WriteLn operator displays the text “Enter two integers X and Y:”;

▪ the ReadLn(X,Y) operator reads the values ​​of numbers entered from the keyboard and assigns them to the integer variables X and Y, respectively;

the ReadLn(Z) operator reads the value of a number entered from the keyboard and assigns it to the real variable Z;

▪ then the assignment operator calculates the average X,Y,Z value and assigns it to the Midd variable, then the square of this value is similarly calculated and assigned to the SqrMidd variable;

▪ the Writeln operator displays the text "Arithmetic mean = ",

calculated Midd value and moves the cursor to new line;

▪ the Write operator displays the text "Square of the arithmetic mean = " and the calculated value SqrMidd;

▪ end operator. closes the main block and ends the program.

Example 2 Calculate the area of ​​the circle S and the circumference L based on the given radius R.

Program

Program KRUG;

const P=3.14159

R,S,L:Real;

Read(R);(enter radius value)

L:=2*P*R;

S:=P*SQR(R);

Writeln(Circumference = ",L,"cm");

Write("Area of ​​circle = ",S,"sq.cm");

Control questions

1 What sections does any Pascal program consist of?

2 Format and purpose of the assignment operator.

3 Format and purpose of data entry operators.

4 Format and purpose of data output operators.

Exercise

Find the surface area of ​​a cube using the formula T=6a 2
Determine the distance traveled by a physical body in time t if the body moves at a constant speed v.
Calculate:
In a year there are approximately 3.156x10 7 seconds. Write a program that requests age in years and converts it into seconds.
Calculate:
Find the volume of the cylinder using the formula: V=pR 2 H
Find the distance from the point with coordinates (x,y) to the origin.
Weight m one molecule of water is approximately equal to 3.0x10 -23 g. A quart of water is approximately 950 grams. Write a program that asks for the amount of water in quarts and prints the number of molecules in that amount of water.
Find the volume of the cube using the formula V=a 3. (with and without using standard functions).
Calculate:
Write a program that requests the number of days and converts to weeks and days. For example, 18 days = 2 weeks and 4 days.
Find the diagonal and area of ​​a square
Calculate:
Find the lateral surface area of ​​the ball: T=4pR 2
Calculate:
Calculate:

Along with other programming languages, Pascal contains tools that allow you to create a subroutine (a kind of auxiliary algorithm) for the main program - Pascal procedures and functions. They are mainly used when some action or subalgorithm is repeated many times in a program, or when there is a need to use parts of previously compiled algorithms.

Subroutines are a kind of breaking up large programs into separate parts. It is convenient and efficient to split large programs into several subroutines, which simplifies the development of the main program code. To use a subalgorithm as a subroutine, you need to give it a name and describe the algorithm in accordance with the rules of the Pascal language.

Further, if there is a need to call a subalgorithm in the main program, then the name of a particular subalgorithm is mentioned in the required place in combination with a list of data (both input and output). This reference, as a rule, produces the execution of statements included in the subroutine and working with the specified data. After executing the subroutine used, the work of the main program continues, but starting with the command immediately following the subroutine call.

There are two types of subroutines in Pascal:

  • Functions
  • Procedures

Their description structure is quite similar to the structure of a Pascal program, i.e. Procedures and functions also include a header, a description section (description of constants, labels, types, functions and procedures themselves, variables, etc.), and an executable part (description of procedures): The structure of a function in the Pascal programming language is as follows way:

The structure of the procedure in Pascal is presented as follows:

As in the function description format, and in the procedure description format, the formal parameters in the header of functions and procedures are represented as follows:

var parameter name: type name;

Formal parameters are separated by commas; keyword var may be omitted in some cases. When parameters have the same type, the names of these parameters are listed separated by commas, indicating the name of the corresponding type at the end after the “:” sign.

When describing parameters, you can only use standard type names, which are defined using the type command. Procedures are called using a statement that has the following structure:

procedure name(list of actual parameters);

The list of actual parameters is indicated in parentheses (their listing is separated by “,”). When a procedure is called, the actual parameters act as formal parameters, which are found at the same place in the procedure header. As a result, input parameters are passed, and then the statements of the executable part are executed, and then returned to the calling block.

A function in Pascal is called in a similar way, but it is possible to call a function inside an expression, i.e. the function name can be in the conditions section of an if statement, to the right of an assignment statement, etc. To pass the output value of a function in the executable part to the calling block, before returning to the calling block, you need to write the command:

function name:=result;

When there is a need to call a procedure and function, you should be guided by the following rules

  1. number of formal parameters = number of actual parameters;
  2. actual and formal parameters must have the same order and type.

The note. The names of the actual and formal parameters can be the same - this does not cause any problems, since the corresponding parameters will in any case be different due to the fact that they are stored in different memory areas.