# Lesson #5. Minimum and Maximum. If statemens (continuation)

Дата изменения: 26 сентября 2020

## Theory

### Search for minimum and maximum

• Which of the two variables is greater (maximum):
• var (x,y) := ReadInteger2; var Max: integer; if x>y then Max := x else Max := y;
• Which of the two variables is greater (maximum) and which one is less (minimum):
• var (x,y) := ReadInteger2; var Min,Max: integer; if x>y then begin // don't forget about compound statement! Max := x; Min := y; end else begin Max := x; Min := y; end;
It is very important here not to forget about compound statement begin..end.

### Random function

• Random function is often used in Pascal.
• To generate numbers from 0 to n not including the value of n itself (integers in the interval [0,N)), you must write:
• var genNumb:=random (n);
• To generate numbers in the range [a,b], you must write:
• var a:=readinteger(); var b:=readinteger(); var genNumb:=random (a,b);

### Nested If statements. Using Assert function

Example:

To do: A Point (x,y) on а coordinate plane is given ( ≠ 0, ≠ 0). Output a number of quarter: ✍ Решение:

var (x,y) := ReadInteger2; var quater: integer; Assert((x<>0) and (y<>0)); // must return True if x>0 then if y>0 then quarter := 1 else quarter := 4; else if y>0 then quarter := 2 else quarter := 3;

### Maximum and Minimum

{0.3 points} Task 1:
To do: Three integers are given. Find the maximum (the greatest) number among the three entered numbers.

The resulting example:

please enter three integer numbers
>>> 6   >>> 1   >>> 9
The maximum number is 9


{0.3 points} Task 2:
To do: Two real numbers are given. Find a maximum (the greatest) and minimum (the least) number among them. Output max and min.

The resulting example:

please enter three integer numbers
>>> 3    >>> 55   >>> 8
Maximum number: 55  Minimum number: 3


{0.5 points} Task 3:
To do: A two-digit number is given. Find the minimum and the maximum among its digits and swap the digits in the number.

The resulting example:

please enter two-digit number
>>> 74
max = 7, min = 4, swapped = 47


{1 point} Task 4:
To do: The integers A, B and k are given. At first output the larger of A and B, then the lesser of them. Print True if the difference between the numbers does not exceed the value of k and print False otherwise. Check the correctness of your program with at least three input data sets, give the log of the program in the form of a comment.

The resulting example:

Please enter three integer numbers
>>> 3   >>> 5    >>> 2
5 3 True
---
>>> 44   >>> 20   >>> 9
44 20 False


{1 point} Task 5:
To do: A three-digit integer is given. Find the maximum (the greatest) digit and minimum minNum (the least) digit among the three digits of the given number and swap them. Output the numbers.

The resulting example:

please enter three integer numbers
>>> 16   >>> 55   >>> 4
After swaping maximum and minimum we have: 16 4 55


### If statements (continuation) and random function

{0.4 points} Task 6:
To do: For a given real x find the value of the following function f. The resulting example:

please enter a real number
>>> -4
The result of function f is 4
---
>>> 1.5
The result of function f is 2.25


{0.3 points} Task 7:
To do: An integer is given. Use random function to generate this number. Display its description in the following form: “negative even number” or “negative odd number” or “zero number” or “positive odd number” or “positive even number”. Check the correctness of your program, provide a log of the program in the form of a comment.

The resulting example:

generated number:
-4
negative even number
---
generated number:
-3
negative odd number


{0.4 points} Task 8:
To do: The numbers X and Y are given. Print True if the point with coordinates (X, Y) lies in the fourth coordinate quarter and print False otherwise. Do not use conditional if statement.

The resulting example:

Please enter the values of x and y
>>> 8   >>> -4
True
---
Please enter the values of x and y
>>> -9  >>> 3
False


{0.4 points} Task 9:
To do: The coordinates X and Y of the chessboard field are given (integers lying in the range of 1–8). Use random function to generate these numbers. Considering that the bottom left cell of the board (1, 1) is black, output True if the field of X and Y is white and print False otherwise.

The resulting example:

Please enter the values of x and y between 1 and 8 inclusive
2 7
False
---
Please enter the values of x and y between 1 and 8 inclusive
3 5
True


{0.3 points} Task 10:
To do: Three integer numbers are given. Use random function to generate these numbers. Print true if none of these numbers are positive and False otherwise. Check the correctness of your program with at least three input data sets.

The resulting example:

generated numbers:
-9 -6 -49
True
---
generated numbers:
0 77 -5
False


{0.3 points} Task 11:
To do: Integers x, y are given. Calculate the value of the function:. A snippet of code:

begin var x, y: integer; // arguments of f function Write('Input integers x, y: '); Readln(x, y);   var f: integer; // TODO: set the value of the function f(x, y) to f variable   WritelnFormat('f({0}, {1}) = {2}', x, y, f); end.

The resulting example:

Input integers x, y:
>>> 7    >>> -5
f (7, -5) =  70


$${}$$ 