Lesson #9. Nested loops

Theory

Lection in pdf format

Labs and tasks

Nested Loops

Lab 1:

To do: Calculate the value of the function z(x,y) = xy for every x changing in the interval [2;8], and y changing in the interval [2;4].

Expected output:

z(x,y) = 2^2 = 4
z(x,y) = 2^3 = 8
z(x,y) = 2^4 = 16
z(x,y) = 3^2 = 9
z(x,y) = 3^3 = 27
z(x,y) = 3^4 = 81
z(x,y) = 4^2 = 16
z(x,y) = 4^3 = 64
z(x,y) = 4^4 = 256
z(x,y) = 5^2 = 25
z(x,y) = 5^3 = 125
z(x,y) = 5^4 = 625
... etc.

✍ Algorithm:

  • We must create two for loops (nested loop): one loop within the other. Variable x has to be modified in the outer loop; variable y has to be modified within the inner loop.
{0.4 points} Task 1, nested loops:

To do: Calculate a function z(x,y) = x+2y for every x varying in the interval [5;10] and y varying in the interval [1;5]. You should use nested for loops.

[Program name: task-01-nested_loops.pas]

Expected output:

z(x,y)= 5+2*1 = 7
z(x,y)= 5+2*2 = 9
z(x,y)= 5+2*3 = 11
z(x,y)= 5+2*4 = 13
z(x,y)= 5+2*5 = 15
z(x,y)= 6+2*1 = 8
z(x,y)= 6+2*2 = 10
z(x,y)= 6+2*3 = 12
z(x,y)= 6+2*4 = 14
z(x,y)= 6+2*5 = 16
z(x,y)= 7+2*1 = 9
z(x,y)= 7+2*2 = 11
z(x,y)= 7+2*3 = 13
z(x,y)= 7+2*4 = 15
z(x,y)= 7+2*5 = 17
z(x,y)= 8+2*1 = 10
z(x,y)= 8+2*2 = 12
z(x,y)= 8+2*3 = 14
z(x,y)= 8+2*4 = 16
z(x,y)= 8+2*5 = 18
z(x,y)= 9+2*1 = 11
z(x,y)= 9+2*2 = 13
z(x,y)= 9+2*3 = 15
z(x,y)= 9+2*4 = 17
z(x,y)= 9+2*5 = 19
z(x,y)= 10+2*1 = 12
z(x,y)= 10+2*2 = 14
z(x,y)= 10+2*3 = 16
z(x,y)= 10+2*4 = 18
z(x,y)= 10+2*5 = 20
{0.4 points} Task 2, nested loops:

To do: Calculate the function z(x,y) = x-y for every x varying in the interval [30;33] and y varying in the interval [1;5]. You should use nested for loops.

Expected output:

30-1=29
30-2=28
30-3=27
30-4=26
30-5=25
31-1=30
31-2=29
31-3=28
31-4=27
31-5=26
32-1=31
32-2=30
32-3=29
32-4=28
32-5=27
33-1=32
33-2=31
33-3=30
33-4=29
33-5=28

[Program name: task-02-nested_loops.pas]

{0.4 points}Task 3_0, nested loops:
To do: The program must display the rows with the following sequences as in the example below. Use nested loops: the outer for loop must iterate over the columns, the inner for loop must iterate over the sequences within each row.

Expected output:

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
5 5 5 5 5 

[Program name: task-3_0-nested_loops.pas]

{0.4 points} Task 3_1, nested loops:

To do: The program must display the rows with the following sequences as in the example below. Use nested loops: the outer for loop must iterate over the columns, the inner for loop must iterate over the sequences within each row.

Expected output:

0 0 0 0 0 
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 
7 7 7 7 7 
8 8 8 8 8 
9 9 9 9 9 

[Program name: task-3_1-nested_loops.pas]

{0.4 points} Task 4, nested loops:

To do: The program must display the rows with the following sequences as in the example below. Use nested loops and arbitrary step (=2).

Expected output:

1 1 1 1 1 1
3 3 3 3 3 3
5 5 5 5 5 5
7 7 7 7 7 7
9 9 9 9 9 9

[Program name: task-04-nested_loops.pas]

{0.7 points} Task 5, nested loops:

To do: Integers K > 0 and N ≥ 0 are given. K sequences of integers with N elements in each set are entered. The program has to output the number of odd elements in each sequence.

Note: It is better to use random function to generate the numbers of sequences. But in this case you shouldn’t forget to print out the elements.

var a:=random(10);

Expected output:

please, enter the quantity of the sequences:  
>>> 3
enter the quantity of numbers within the sequences 
>>> 5

the sequence #1 
4 9 3 3 7 result: 4 odd elements 
the sequence #2 
6 9 8 2 9 result: 2 odd elements 
the sequence #3 
5 1 7 3 6 result: 4 odd elements 

[Program name: task-05-nested_loops.pas]

{0.7 points} Task 6, nested loops:

To do: Integers K > 0 and N ≥ 0 are given. K sequences of integers with N elements in each set are entered. The program has to output a minimum and maximum elements in each sequence.

Note 1: It is better to use random function to generate the numbers of sequences. But in this case don’t forget to print out the elements.

var a:=random(10);

Note 2: It is better to use the following constants as the initial values of the minimum and maximum variables:

min:=integer.MaxValue;
max:=integer.MinValue;

Expected output:

please, enter the quantity of the sequences:  
>>>3
enter the quantity of numbers within the sequences 
>>>5

the sequence #1 
4 9 5 4 8 min =4, max=9
 
the sequence #2 
6 9 2 3 4 min =2, max=9 

the sequence #3 
9 1 4 8 7 min =1, max=9 

[Program name: task-06-nested_loops.pas]

Boolean type (logical)

Lab 3, boolean type:

To do: The values of n and k are entered. n numbers are entered. The program should output if there is a number k among them. (the result must be of boolean type)

Expected output:

how many numbers (n)?  
>>>5
what number must we find (k)? 
>>>3
enter numbers, please 
>>>1  >>>3  >>>6  >>>4  >>>7
there is '3' number:  True

✍ Algorithm:

    begin
      var n := ReadInteger('how many numbers?');
      assert (n>0);
      var exists := false;
      var k := ReadInteger('what number must we find?');
      print('enter numbers, please');
      loop n do
      begin
        var x := ReadInteger;
        if x = k then
          exists := true;
      end;
      print ($'there is {k} number: ', exists)
    end.

Break and continue

Lab 4, break:

To do: The values of n and k are entered. n numbers of the sequence are entered. The program should output if there is a number k among them?

✍ Algorithm:

    begin
      var n := ReadInteger('how many numbers?');
      assert (n>0);
      var exists := false;
      var k := ReadInteger('what number must we find?');
      print('enter numbers, please');
      loop n do
      begin
        var x := ReadInteger;
        if x = k then
          begin
            exists := true;
            break;
          end;
      end;
      print ($'there is {k} number: ', exists)
    end.
Task 7, boolean type:

To do: Integers K > 0 and N ≥ 0 are given. K sequences of integers with N elements in each set are entered. The program has to output if there is a number 2 among the numbers of sequences and output the total quantity of 2 number.

Expected output:

please, enter the quantity of the sequences: 
>>> 3
enter the quantity of numbers within the sequences:
>>> 2

input the sequence #1:
>>> 1 >>> 5 

input the sequence #2:
>>> 2 >>> 5 

input the sequence #3:
>>> 5 >>> 7 

there is a number '2': true
total quantity of '2': 1

[Program name: task-07-bool.pas]


The Infinite Loop

Examples:

{# 1}
while true do
  begin
  ...
  end;
end.
{# 2}
repeat
  ...
until false;
{# 3}
var x:=1;
while x>0 do
  begin
   ...
  end;
end.
{# 4}
var x:=1;
repeat
  ...
until x<=0;
end.
Lab 5, infinite loops:

To do: A sequence of integers is given. The last number of the sequence is 0 (if 0 is entered the input of the numbers of the set is finished). The program must output the number of positive elements among the sequence.

Expected output:

please, enter the sequence, print 0 if you want to stop: 
>>>1 >>>-5 >>>9 >>>5 >>>0
positive:  3
please, enter the sequence, print 0 if you want to stop: 
>>>1 >>>-2 >>>-5 >>>-9 >>>11 >>>0
positive:  2 

✍ Algorithm:

    Solution 1:

    begin
      print('please, enter the sequence, print 0 if you want to stop:');
      var count:=0;
      var x:=readinteger;
      while x<>0 do
      begin
        if x>0 then
          count+=1;
        x:=readinteger;
      end;
      print($'positive = {count}')
    end.

    Solution 2:

    begin
      print('please, enter the sequence, print 0 if you want to stop:');
      var count := 0;
      while true do
      begin
        var x := readinteger;
        if x = 0 then
          break
        else if x > 0 then
          count += 1;
      end;
      print($'positive = {count}')
    end.
{0.4 points} Task 8, infinite loops:

To do: A sequence of integers is given. The last number of the sequence is 0 (if 0 is entered the input of the numbers of the set is finished). The program must output the minimum and maximum of them. Use infinite loop.

Expected output:

please, enter the sequence, print 0 if you want to stop: 
>>>1 >>>-2 >>>-5 >>>-9 >>>11 >>>0
min = -9  max = 11

[Program name: task-08-infinite_loops.pas]

{0.7 points} Task 9, infinite loops:

To do: Integer K is given (K > 0) and the set of K sequences. The last number of all the sequences is 0 (if 0 is entered the input of the numbers is finished). The program has to output the number of elements in each sequence, and also output the total number of the elements in all the sequences together.

Expected output:

please, enter the quantity of the sequences 
>>> 3
input the sequence #1 
>>>1  >>>5  >>>8  >>>0
the seq has 3 elements 

input the sequence #2 
>>>7  >>>2  >>>0
the seq has 2 elements 

input the sequence #3 
>>>6  >>>0
the seq has 1 elements
 
Total number of elements:  6 

[Program name: task-09-infinite_loops.pas]

Working with digits of natural number

Lab 6:

To do: Natural number m is given. Calculate sum of its digits
Solution: div / mod operations split the number on the series of its digits

Expected output:

m:
>>> 425
result: 11

✍ Algorithm:

    begin
      var m := ReadInteger;
      Assert(m > 0);
      var s := 0;
      while m > 0 do
      begin
        s += m mod 10; // sum of digits
        m := m div 10; // discard the number to the right
      end;
    end.
{0.7 points} Task 10, digits:

To do: An integer is given (variable a). Find the number of its digits and their product.

Note: since entered number can be a negative number, you can use the abs function, to have an absolute value of a number:

abs(m)

Expected output:

N:
>>> 1205 
Count = 4, product = 0
--
N:
>>> -111 
Count = 3, product = -1

[Program name: task-10-loops.pas]

{0.7 points} Task 11, digits:

To do: Natural number m is given. How many even digits in its decimal representation does it have?

Expected output:

m:
>>> 745
even: 1

[Program name: task-11-loops.pas]

More about Sequences in while loop

Lab 7, sequences:

To do: A sequence of integers is given. The last number of all the sequences is 0 (if 0 is entered the input of the numbers is finished). The program has to print 0 in the case when the sequence forms a non-increasing sequence of numbers, otherwise the program has to print the number 1.

Expected output:

please, enter the sequence, print 0 if you want to stop: 
>>>1 >>>5 >>>9 >>>5 >>>0
output: 0 (non-increasing sequence)
+++++++++++++++++++++++++++++++++++++++++++++++++
please, enter the sequence, print 0 if you want to stop: 
>>>1 >>>2 >>>5 >>>9 >>>11 >>>0
output: 1 (increasing sequence)

✍ Algorithm:

    Solution 1:

    Solution 2:
{0.7 points} (complex) Task 12, loops:

To do: Integer K is given (K > 0) and the set of K sequences. The last number of all the sequences is 0 (if 0 is entered the input of the numbers is finished). The program has to print true in the case when the sequence contains the element which is less than its neighbor on the left, otherwise the program has to print the number false.

Expected output:

'please, enter the quantity of the sequences:' 3

'input the sequence #1:'
1 5 3 0
>>> output: true 

'input the sequence #2:'
2 6 0
>>> output: false

'input the sequence #3:'
5 7 4 0
>>> output: true 

[Program name: task-12-loops.pas]

{1.0 points} (complex) Task 13, loops:

To do: A sequence is called sawtoothed if each of its elements is either greater than each of its neighbors, or less than them. A sequence of integers containing at least two elements is given. The last number of the sequence is 0. Check if the given sequence is sawtoothed. Output true or false.

Note 1: A sequence of two elements is considered a sawtoothed if these elements are different.

Note 2: To specify if the sequence is either sawtoothed or not, you must use a variable of boolean type:

var flag := true;

Note 3: You do not need to check that the sequence contains at least two elements.

Expected output:

enter the sequence:
>>> 2  >>> 9  >>> 1  >>> 7  >>> 5  >>> 0
sawtoothed: True 
---
enter the sequence:
>>> 1  >>> 2  >>> 3  >>> 2  >>> 5  >>> 0
sawtoothed: False 

[Program name: task-13-loops.pas]

{0.4 points} Task 14:

To do: An integer N is given and a set of N numbers (they are input). Find a serial number (position) of the first minimum among the numbers and serial number of the last maximum among the numbers.

Expected output:

enter 10 numbers:
>>>2  >>>15  >>>3  >>>8  >>>1  >>>2  >>>9  >>>1  >>>15  >>>11   
serial numb of min is 5, serial numb of max is 9

[Program name: task-14.pas]

{0.3 points} Task 15, loops:

To do: n is entered. Print the Fibonacci sequence of n elements.

Expected output:

enter n:
>>> 8
result: 1 1 2 3 5 8 13 21 

[Program name: task-15-fib.pas]