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

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 := y;
      Min := x;
    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);

    To generate Tuple of two elements (Random2), or three elements (Random3):

    var (a, b, c) := Random3(10.0, 20.0); // range [10, 20)
    write(a:0:2,' ',b:0:2,' ', c:0:2) // output: 14.73 18.63 19.72

    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 quarter: integer;
      Assert((x<>0) and (y<>0), 'incorrect input'); // 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;

    Tasks

    Maximum and Minimum

    {0.3 points} Task 1:
    To do: Three integers are entered (they should be in the range from 0 to 10). Find the maximum (the greatest) number among the three entered numbers. Check entered values using assert function.

    Expected output:

    please enter three integer numbers
    >>> 6   >>> 1   >>> 9
    The maximum number is 9 
    
    please enter three integer numbers
    >>> 1   >>> 2   >>> 13
    Incorrect input! must be in the range from 0 to 10
    

    [Program name: L5task01.pas]

    {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.0    >>> 55.5   
    Maximum number: 55.5  Minimum number: 3.0 
    

    [Program name: L5task02.pas]

    {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. Check entered values using assert function (it should be two-digit number).

    Expected output:

    please enter two-digit number
    >>> 74    
    max = 7, min = 4, swapped = 47
    
    please enter two-digit number
    >>> 7    
    Incorrect input! should be two-digit number
    

    [Program name: L5task03.pas]

    {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.

    expected output:

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

    [Program name: L5task04.pas]

    {1 point} Task 5:
    To do: A three-digit integer is given. Find the maximum (the greatest) digit and minimum (the least) digit among the three digits of the given number and swap them. Output the numbers. Check entered values using assert function (it should be three-digit number).

    The resulting example:

    please enter three-digit integer 
    >>> 167
    After swaping maximum and minimum we have: 761
    
    please enter three-digit integer 
    >>> 7    
    Incorrect input! should be three-digit number
    

    [Program name: L5task05.pas]

    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
    ---
    please enter a real number
    >>> 1.5
    The result of function f is 2.25
    

    [Program name: L5task06.pas]

    {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 
    

    [Program name: L5task07.pas]

    {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 
    

    [Program name: L5task08.pas]

    {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 
    

    [Program name: L5task09.pas]

    {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
    

    [Program name: L5task10.pas]

    {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 
    

    [Program name: L5task11.pas]

    Tasks for self-solving

    Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

    BOOLEAN

    1. Boolean4. Given two integers A and B, verify the following proposition: «The inequalities A > 2 and B ≤ 3 both are fulfilled».
      Expected output:
      << A = 1 B = 2
      results:
      false
      
    2. Boolean5. Given two integers A and B, verify the following proposition: "The inequality A ≥ 0 is fulfilled or the inequality B < −2 is fulfilled".
      Expected output:
      << A = 0 B = -3
      results:
      true
      
    3. Boolean6. Given three integers A, B, C, verify the following proposition: "The double inequality A < B < C is fulfilled".
      Expected output:
      << A = 50 B = 66 c = 73
      results:
      true
      
    4. Boolean7. Given three integers A, B, C, verify the following proposition: "The number B is between A and C".
    5. Boolean17. Given a positive integer, verify the following proposition: "The integer is a three-digit odd number".
    6. Boolean18. Verify the following proposition: "Among three given integers there is at least one pair of equal ones".
      Expected output:
      << -1   -5   3
      results:
      false
      

    IF

    1. If4. Three integers are given. Find the amount of positive integers in the input data.
      Expected output:
      << -10   -7   -11
      results:
      0
      
    2. If6. Given two real numbers, output the larger value of them.
    3. If13. Given three real numbers, output the value between the minimum and the maximum.
    4. If15. Given three real numbers, output the sum of two largest values.
    5. If24. Given a real independent variable x, find the value of a real function f defined as:
      f(x) = 	
        2·sin(x), if x > 0,
        6 − x, if x ≤ 0.
      
      Expected output:
      << x = 6.05
      results:
      -0.46
      
    6. If26. Given a real independent variable x, find the value of a real function f defined as:
       	 	−x,	if x ≤ 0,
      f(x)	 = 	x2,	if 0 < x < 2,
       	 	4,	if x ≥ 2.
      
      Expected output:
      << x = 1.05
      results:
      1.10