Russian  ©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2023

 Main Tasks Examples PT for MPI-2

 Overview Input-output operations Task groups Begin Integer Boolean If Case For While Series Proc Func Minmax Array Matrix String File Text Param Recur Dynamic Dynamic (obj) Tree Tree (obj)

# Procedures and functions

## Procedures with numeric parameters

Proc1. Write a procedure PowerA3(A, B) that computes the third degree of a real number A and assigns the result to a real variable B (A is an input parameter, B is an output parameter). Using this procedure, find the third degree of five given real numbers.

Proc2. Write a procedure PowerA234(A, B, C, D) that computes the second, the third, and the fourth degrees of a real number A and assigns the results to real variables B, C, and D respectively (A is an input parameter, BC, D are output parameters). Using this procedure, find the second, the third, and the fourth degrees of five given real numbers.

Proc3. Write a procedure Mean(XYAMeanGMean) that computes the arithmetical mean AMean = (X+Y)/2 and the geometrical mean GMean = (X·Y)1/2 of two positive numbers X and Y (X and Y are input parameters, AMean and GMean are output parameters; all parameters are the real-valued ones). Using this procedure, find the arithmetical mean and the geometrical mean of pairs (AB), (AC), (AD) provided that real numbers AB, CD are given.

Proc4°. Write a procedure TrianglePS(a, P, S) that computes the perimeter P = 3·a and the area S = a2·(3)1/2/4 of an equilateral triangle with the side a (a is an input parameter, P and S are output parameters, all parameters are the real-valued ones). Using this procedure, find the perimeters and the areas of three triangles with the given lengths of the sides.

Proc5. Write a procedure RectPS(x1y1x2y2PS) that computes the perimeter P and the area S of a rectangle whose opposite vertices have coordinates (x1y1) and (x2y2) and sides are parallel to coordinate axes (x1, y1, x2, y2 are input parameters, P and S are output parameters, all parameters are the real-valued ones). Using this procedure, find the perimeters and the areas of three rectangles with the given opposite vertices.

Proc6. Write a procedure DigitCountSum(K, C, S) that finds the amount C of digits in the decimal representation of a positive integer K and also the sum S of its digits (K is an input parameter, C and S are output parameters, all parameters are the integer ones). Using this procedure, find the amount and the sum of digits for each of five given integers.

Proc7. Write a procedure InvDigits(K) that inverts the order of digits of a positive integer K (K is an input and output integer parameter). Using this procedure, invert the order of digits for each of five given integers.

Proc8°. Write a procedure AddRightDigit(D, K) that adds a digit D to the right side of the decimal representation of a positive integer K (D is an input integer parameter with the value in the range 0 to 9, K is an input and output integer parameter). Having input an integer K and two one-digit numbers D1, D2 and using two calls of this procedure, sequentially add the given digits D1, D2 to the right side of the given K and output the result of each adding.

Proc9. Write a procedure AddLeftDigit(D, K) that adds a digit D to the left side of the decimal representation of a positive integer K (D is an input integer parameter with the value in the range 0 to 9, K is an input and output integer parameter). Having input an integer K and two one-digit numbers D1, D2 and using two calls of this procedure, sequentially add the given digits D1, D2 to the left side of the given K and output the result of each adding.

Proc10°. Write a procedure Swap(X, Y) that exchanges the values of variables X and Y (X and Y are input and output real-valued parameters). Having input integers A, B, CD and using three calls of this procedure, sequentially exchange the values of the pairs A and B, C and D, B and C. Output the new values of AB, CD.

Proc11. Write a procedure Minmax(X, Y) that exchanges, if necessary, the values of two variables X and Y so that X ≤ Y (X and Y are input and output real-valued parameters). Using four calls of this procedure, find the minimal value and the maximal value among given real numbers AB, CD.

Proc12. Write a procedure SortInc3(A, BC) that interchanges, if necessary, the values of three variables A, BC so that A ≤ B ≤ C (AB, C are input and output real-valued parameters). Using this procedure, sort each of two given triples of real numbers (A1, B1, C1) and (A2, B2, C2) in ascending order.

Proc13. Write a procedure SortDec3(A, BC) that interchanges, if necessary, the values of three variables A, BC so that A ≥ B ≥ C (AB, C are input and output real-valued parameters). Using this procedure, sort each of two given triples of real numbers (A1, B1, C1) and (A2, B2, C2) in descending order.

Proc14. Write a procedure ShiftRight3(A, BC) that performs a right cyclic shift by assigning the initial values of A, BC to variables B, CA respectively (A, BC are input and output real-valued parameters). Using this procedure, perform the right cyclic shift for each of two given triples of real numbers: (A1, B1, C1) and (A2, B2, C2).

Proc15. Write a procedure ShiftLeft3(A, BC) that performs a left cyclic shift by assigning the initial values of A, BC to variables C, AB respectively (A, BC are input and output real-valued parameters). Using this procedure, perform the left cyclic shift for each of two given triples of real numbers: (A1, B1, C1) and (A2, B2, C2).

## Functions with numeric parameters

Proc16. Write an integer function Sign(X) that returns the following value:

−1,    if X < 0;        0,    if X = 0;        1,    if X > 0

(X is a real-valued parameter). Using this function, evaluate an expression Sign(A) + Sign(B) for given real numbers A and B.

Proc17. Write an integer function RootCount(A, BC) that returns the amount of roots of the quadratic equation A·x2 + B·x + C = 0 (AB, C are real-valued parameters, A ≠ 0). Using this function, find the amount of roots for each of three quadratic equations with given coefficients. Note that the amount of roots is determined by the value of a discriminant:

D = B2 − 4·A·C.

Proc18. Write a real-valued function CircleS(R) that returns the area of a circle of radius R (R is a real number). Using this function, find the areas of three circles of given radiuses. Note that the area of a circle of radius R can be found by formula S = π·R2. Use 3.14 for a value of π.

Proc19. Write a real-valued function RingS(R1, R2) that returns the area of a ring bounded by a concentric circles of radiuses R1 and R2 (R1 and R2 are real numbers, R1 > R2). Using this function, find the areas of three rings with given outer and inner radiuses. Note that the area of a circle of radius R can be found by formula S = π·R2. Use 3.14 for a value of π.

Proc20°. Write a real-valued function TriangleP(a, h) that returns the perimeter of an isosceles triangle with given base a and altitude h (a and h are real numbers). Using this function, find the perimeters of three triangles with given bases and altitudes. Note that the leg b of an isosceles triangle can be found by the Pythagorean theorem:

b2 = (a/2)2 + h2.

Proc21°. Write an integer function SumRange(A, B) that returns a sum of all integers in the range A to B inclusively (A and B are integers). In the case of A > B the function returns 0. Using this function, find a sum of all integers in the range A to B and in the range B to C provided that integers A, BC are given.

Proc22. Write a real-valued function Calc(A, B, Op) that performs an arithmetic operation over nonzero real numbers A and B and returns the result value. An arithmetic operation is determined by integer parameter Op as follows: 1 — subtraction, 2 — multiplication, 3 — division, and addition otherwise. Having input real numbers A, B and integers N1, N2N3 and using this function, perform over given A, B three operations determined by given N1, N2N3. Output the result value of each operation.

Proc23. Write an integer function Quarter(x, y) that returns the number of a coordinate quarter containing a point with nonzero real-valued coordinates (xy). Using this function, find the numbers of coordinate quarters containing each of three points with given nonzero coordinates.

Proc24. Write a logical function Even(K) that returns True, if an integer parameter K is an even number, and False otherwise. Using this function, find the amount of even numbers in a given sequence of 10 integers.

Proc25°. Write a logical function IsSquare(K) that returns True, if an positive integer parameter K is a square of some integer, and False otherwise. Using this function, find the amount of squares in a given sequence of 10 positive integers.

Proc26. Write a logical function IsPower5(K) that returns True, if an positive integer parameter K is equal to 5 raised to some integer power, and False otherwise. Using this function, find the amount of powers of base 5 in a given sequence of 10 positive integers.

Proc27. Write a logical function IsPowerN(K, N) that returns True, if an positive integer parameter K is equal to N (> 1) raised to some integer power, and False otherwise. Having input an integer N (> 1) and a sequence of 10 positive integers and using this function, find the amount of powers of base N in the given sequence.

Proc28. Write a logical function IsPrime(N) that returns True, if an integer parameter N (> 1) is a prime number, and False otherwise. Using this function, find the amount of prime numbers in a given sequence of 10 integers greater than 1. Note that an integer (> 1) is called a prime number if it has not positive divisors except 1 and itself.

Proc29. Write an integer function DigitCount(K) that returns the amount of digits in the decimal representation of a positive integer K. Using this function, find the amount of digits for each of five given positive integers.

Proc30. Write an integer function DigitN(K, N) that returns the N-th digit in the decimal representation of a positive integer K provided that the digits are numbered from right to left. If the amount of digits is less than N then the function returns −1. Using this function, output sequentially 1st, 2nd, 3rd, 4th, 5th digit for each of five given positive integers K1, K2, …, K5.

Proc31. Write a logical function IsPalindrome(K) that returns True, if the decimal representation of a positive parameter K is a palindrome (i. e., it is read equally both from left to right and from right to left), and False otherwise. Using this function, find the amount of palindromes in a given sequence of 10 positive integers.

Proc32. Write a real-valued function DegToRad(D) that converts the angle value D in degrees into the one in radians (D is a real number, 0 ≤ D < 360). Note that 180° = π radians and use 3.14 for a value of π. Using this function, convert five given angles from degrees into radians.

Proc33. Write a real-valued function RadToDeg(R) that converts the angle value R in radians into the one in degrees (R is a real number, 0 ≤ R < 2·π). Note that 180° = π radians and use 3.14 for a value of π. Using this function, convert five given angles from radians into degrees.

Proc34. Write a real-valued function Fact(N) that returns a factorial of a positive integer N: N! = 1·2·…·N (the real return type allows to avoid the integer overflow during the calculation of the factorials for large values of the parameter N). Using this function, find the factorials of five given integers.

Proc35. Write a real-valued function Fact2(N) that returns a double factorial N!!:

N!! = 1·3·5·…·N,    if N is an odd number;
N!! = 2·4·6·…·N    otherwise

(N is a positive integer; the real return type allows to avoid the integer overflow during the calculation of the double factorials for large values of N). Using this function, find the double factorials of five given integers.

Proc36. Write an integer function Fib(N) that returns the value of N-th term of the sequence of the Fibonacci numbers. The Fibonacci numbers FK are defined as follows:

F1 = 1,        F2 = 1,        FK = FK−2 + FK−1,    K = 3, 4, … .

Using this function, find five Fibonacci numbers with given order numbers N1, N2, …, N5.

Proc37. Write a real-valued function Power1(A, B) that returns the power AB calculated by the formula AB = exp(B·ln(A)) (A and B are real-valued parameters). In the case of zero-valued or negative parameter A the function returns 0. Having input real numbers PA, BC and using this function, find the powers AP, BP, CP.

Proc38. Write a real-valued function Power2(A, N) that returns the power AN calculated by the following formulas:

A0 = 1;
AN = A·A·…·A    (N factors),    if N > 0;
AN = 1/(A·A·…·A)    (|N| factors),    if N < 0

(A is a real-valued parameter, N is an integer parameter). Having input a real number A and integers K, LM and using this function, find the powers AK, AL, AM.

Proc39. Using the Power1 and Power2 functions (see Proc37 and Proc38), write a real-valued function Power3(A, B) that returns the power AB calculated as follows (A and B are real-valued parameters): if B has a zero-valued fractional part then the function Power2(A, N) is called (an integer variable N is equal to B), otherwise the function Power1(A, B) is called. Having input real numbers P, A, BC and using the Power3 function, find the powers AP, BP, CP.

Proc40°. Write a real-valued function Exp1(x, ε) (x and ε are real numbers, ε > 0) that returns an approximate value of the function exp(x) defined as follows:

exp(x) = 1 + x + x2/(2!) + x3/(3!) + … + xn/(n!) + …

(n! = 1·2·…·n). Stop adding new terms to the sum when the value of the next term will be less than ε. Using this function, find the approximate values of the function exp(x) at a given point x for six given ε.

Proc41. Write a real-valued function Sin1(x, ε) (x and ε are real numbers, ε > 0) that returns an approximate value of the function sin(x) defined as follows:

sin(x) = x − x3/(3!) + x5/(5!) − … + (−1)n·xn+1/((2·n+1)!) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function sin(x) at a given point x for six given ε.

Proc42. Write a real-valued function Cos1(x, ε) (x and ε are real numbers, ε > 0) that returns an approximate value of the function cos(x) defined as follows:

cos(x) = 1 − x2/(2!) + x4/(4!) − … + (−1)n·xn/((2·n)!) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function cos(x) at a given point x for six given ε.

Proc43. Write a real-valued function Ln1(x, ε) (x and ε are real numbers, |x| < 1, ε > 0) that returns an approximate value of the function ln(1 + x) defined as follows:

ln(1 + x) = x − x2/2 + x3/3 − … + (−1)n·xn+1/(n+1) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function ln(1 + x) at a given point x for six given ε.

Proc44. Write a real-valued function Atan1(x, ε) (x and ε are real numbers, |x| < 1, ε > 0) that returns an approximate value of the function atan(x) defined as follows:

atan(x) = x − x3/3 + x5/5 − … + (−1)n·xn+1/(2·n+1) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function atan(x) at a given point x for six given ε.

Proc45. Write a real-valued function Power4(x, a, ε) (x, a, ε are real numbers, |x| < 1, a, ε > 0) that returns an approximate value of the function (1 + x)a defined as:

(1 + x)a = 1 + a·x + a·(a−1)·x2/(2!) + … + a·(a−1)·…·(an+1)·xn/(n!) + … .

Stop adding new terms to the sum when the absolute value of the next term will be less than ε. Using this function, find the approximate values of the function (1 + x)a at a given point x for a given exponent a and six given ε.

Proc46. Write an integer function GCD2(A, B) that returns the greatest common divisor (GCD) of two positive integers A and B. Use the Euclidean algorithm:

GCD(A, B) = GCD(B, A mod B),    if B ≠ 0;        GCD(A, 0) = A,

where "mod" denotes the operator of taking the remainder after integer division. Using this function, find the greatest common divisor for each of pairs (AB), (AC), (AD) provided that integers A, B, CD are given.

Proc47. Using the GCD2 function from the task Proc46, write a procedure Frac1(a, b, p, q), that simplifies a fraction a/b to the irreducible form p/q (a and b are input integer parameters, p and q are output integer parameters). The sign of a resulting fraction p/q is assigned to its numerator, so q > 0. Using this procedure, find the numerator and the denominator for each of irreducible fractions a/b + c/d, a/b + e/f, a/b + g/h provided that integers a, b, c, d, e, f, gh are given.

Proc48. Taking into account that the least common multiple of two positive integers A and B equals A·(B/GCD(AB)), where GCD(AB) is the greatest common divisor of A and B, and using the GCD2 function from the task Proc46, write an integer function LCM2(A, B) that returns the least common multiple of A and B. Using this function, find the least common multiple for each of pairs (AB), (AC), (AD) provided that integers A, B, CD are given.

Proc49. Taking into account the formula GCD(A, B, C) = GCD(GCD(A, B), C) and using the GCD2 function from the task Proc46, write an integer function GCD3(ABC) that returns the greatest common divisor of three positive integers A, BC. Using this function, find the greatest common divisor for each of triples (ABC), (ACD), (BCD) provided that integers A, B, CD are given.

Proc50. Write a procedure TimeToHMS(T, H, M, S) that converts a time interval T (in seconds) into the "hours H, minutes M, seconds S" format (T is an input integer parameter, HM, S are output integer parameters). Using this procedure, find the amount of hours, minutes and seconds for each of five given time intervals T1, T2, …, T5.

Proc51. Write a procedure IncTime(HMST) that increases a time interval in hours H, minutes M, seconds S on T seconds (H, M, S are input and output positive integer parameters, T is an input positive integer parameter). Having input hours H, minutes M, seconds S (as integers) and an integer T and using the IncTime procedure, increase the given time interval on T seconds and output new values of H, MS.

Proc52. Write a logical function IsLeapYear(Y) that returns True if a year Y (a positive integer parameter) is a leap year, and False otherwise. Output the return values of this function for five given values of the parameter Y. Note that a year is a leap year if it is divisible by 4 except for years that are divisible by 100 and are not divisible by 400.

Proc53. Using the IsLeapYear function from the task Proc52, write an integer function MonthDays(M, Y) that returns the amount of days for M-th month of year Y (M and Y are integers, 1 ≤ M ≤ 12, Y > 0). Output the return value of this function for a given year Y and each of given months M1, M2M3.

Proc54. Using the MonthDays function from the task Proc53, write a procedure PrevDate(D, M, Y) that changes a correct date, represented at the "day D, month number M, year Y" format, to a previous one (D, MY are input and output integer parameters). Apply this procedure to three given dates and output resulting previous ones.

Proc55. Using the MonthDays function from the task Proc53, write a procedure NextDate(D, M, Y) that changes a correct date, represented at the "day D, month number M, year Y" format, to a next one (D, MY are input and output integer parameters). Apply this procedure to three given dates and output resulting next ones.

Proc56. Write a real-valued function Leng(xA, yA, xB, yB) that returns the length of a segment AB with given coordinates of its endpoints:

|AB| = ((xA − xB)2 + (yA − yB)2)1/2

(xA, yA, xB, yB are real-valued parameters). Using this function, find the lengths of segments AB, ACAD provided that coordinates of points AB, CD are given.

Proc57. Using the Leng function from the task Proc56, write a real-valued function Perim(xA, yA, xB, yB, xC, yC) that returns the perimeter of a triangle ABC with given coordinates of its vertices (xA, yA, xB, yB, xC, yC are real-valued parameters). Using the Perim function, find the perimeters of triangles ABC, ABDACD provided that coordinates of points AB, CD are given.

Proc58. Using the Leng and Perim functions from the tasks Proc56 and Proc57, write a real-valued function Area(xA, yA, xB, yB, xC, yC) that returns the area of a triangle ABC:

SABC = (p·(p−|AB|)·(p−|AC|)·(p−|BC|))1/2,

where p is the half-perimeter. Using the Area function, find the areas of triangles ABC, ABDACD provided that coordinates of points ABCD are given.

Proc59. Using the Leng and Area functions from the tasks Proc56 and Proc58, write a real-valued function Dist(xP, yP, xA, yA, xB, yB) that returns the distance D(P, AB) between a point P and a line AB:

D(P, AB) = 2·SPAB/|AB|,

where SPAB is the area of the triangle PAB. Using this function, find the distance between a point P and each of lines AB, ACBC provided that coordinates of points PA, BC are given.

Proc60. Using the Dist function from the task Proc59, write a procedure Alts(xAyAxByBxCyChAhBhC) that evaluates the altitudes hA, hBhC drawn from the vertices A, BC of a triangle ABC (the coordinates of vertices are input parameters, the values of altitudes are output parameters). Using this procedure, evaluate the altitudes of each of triangles ABC, ABDACD provided that the coordinates of points AB, CD are given. Designed byM. E. Abramyan and V. N. Braguilevsky Last revised:01.01.2023