Содержание:
Theory
Lection # 7 in pdf format
Labs and tasks
How to use arbitrary step in for loop & loop
To do: Output all 2-digit odd numbers from 11 to 21.
Expected output:
11 13 15 17 19 21
✍ Algorithm:
- Solution 1. With
loop
- Solution 2. With
for
loop
for var i:=11 to 21 step(2) do print(i) |
{0.4 points}
Task 1:To do: Output the sequence 3 5 7 9 … 21 (from 3 to 21 with a step = 2). Make it twice: with
loop
and for
loop.
The fragment of the program:
begin println('the result with a loop'); var ...; loop ...; ... println('the result with a FOR loop'); for var ... ... end. |
Expected output:
the result with a loop 3 5 7 9 11 13 15 17 19 21 the result with a FOR loop 3 5 7 9 11 13 15 17 19 21
[Program name: L7-task-01.pas
]
{0.4 points}
Task 2:To do: Output the sequence of integers: 20 18 16 … 2 (from 20 downto 2 with a step = 2). Make it twice: with
loop
and for
loop.
Expected output:
the result with a loop 20 18 16 14 12 10 8 6 4 2 the result with a FOR loop 20 18 16 14 12 10 8 6 4 2
[Program name: L7-task-02.pas
]
{0.4 points}
Task 3:To do: Output the sequence of integers: 15 12 9 6 3 0 (from 15 downto 0 with a step = 3). Make it twice: with
loop
and for
loop.
Expected output:
the result with a loop 15 12 9 6 3 0 the result with a FOR loop 15 12 9 6 3 0
[Program name: L7-task-03.pas
]
{0.4 points}
Task 4:To do: Output the sequence of integers: 5 10 15 20 … 100 (from 5 to 100). Make it twice: with
loop
and for
loop.
[Program name: L7-task-04.pas
]
Functions z(x)
To do: Calculate a value of the function z(x) = x3
for all x
varying on the interval [1, 7] with a step = 2. Make it twice: with loop
and for
loop.
Expected output:
the result with a loop 1*1*1 = 1 3*3*3 = 27 5*5*5 = 125 7*7*7 = 343 the result with a FOR loop 1*1*1 = 1 3*3*3 = 27 5*5*5 = 125 7*7*7 = 343
✍ Algorithm:
- Solution:
begin var z, x: integer; print('the result with a loop'); x := 1; loop 4 do begin z := x * x * x; print($'{x}^3 = {z}'); x := x + 2; end; end. |
begin println('FOR loop SOLUTION 1'); for var x := 1 to 7 step(2) do begin var z := x * x * x; print($'{x}^3 = {z}'); end; end. |
begin print('FOR loop SOLUTION 2'); for var i := 0 to 3 do begin var x := 1 + i * 2; var z := x * x * x; print($'{x}^3 = {z}'); end; end. |
{0.5 points}
Task 5:To do: Calculate a value of the function
z(x) = x2
for all x
varying on the interval [3, 12] with a step = 3. Make it twice: with loop
and for
loop.
Expected output:
the result with a loop 3*3 = 9 6*6 = 36 9*9 = 81 12*12 = 144 the result with a FOR loop 3*3 = 9 6*6 = 36 9*9 = 81 12*12 = 144
[Program name: L7-task-05.pas
]
{0.5 points}
Task 6:To do: Calculate a value of the function
z(x) = √x
for all x
varying on the interval [5, 25] with a step = 5. Make it twice: with loop
and for
loop.
Note: To calculate the square root of a number in pascal there is a standard function sqrt(x)
, for example:
var result:=sqrt(2); // the result = 1.4142135623731 |
Expected output:
the result with a loop sqrt(5) = 2.23606797749979 sqrt(10) = 3.16227766016838 sqrt(15) = 3.87298334620742 sqrt(20) = 4.47213595499958 sqrt(25) = 5 the result with a FOR loop sqrt(5) = 2.23606797749979 sqrt(10) = 3.16227766016838 sqrt(15) = 3.87298334620742 sqrt(20) = 4.47213595499958 sqrt(25) = 5
[Program name: L7-task-06.pas
]
{0.5 points}
Task 7:To do: Calculate a value of the function
z(x) = 2x-2
for all x
varying on the interval [5, 20] with a step = 3. Make it twice: with loop
and for
loop.
Expected output:
2*5-2 = 8 2*8-2 = 14 2*11-2 = 20 2*14-2 = 26 2*17-2 = 32 2*20-2 = 38
[Program name: L7-task-07.pas
]
{0.5 points}
Task 8:To do: Calculate a value of the function
z(x) = sin(x)
for all x
varying on the interval [1, 5].
Note 1: To calculate the sin() in pascal there is a standard function sin(x)
, for example:
var result:=sin(2); // the result = 0.909297426825682 |
Note 2: To output the floating point number with a particular number of digits:
writelnFormat('{0:f2}',3.1415); // output: 3.14 |
Expected output:
'the rezult of z(x) : ' 0.84 0.91 0.14 -0.76 -0.96
[Program name: L7-task-08.pas
]
How to use real step in for & loop
To do: Output the sequence: 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0.
✍ Algorithm:
- Solution 1. With
loop
- Solution 2. With
for
loop
{0.4 points}
Task 9:To do: Output the sequence 0.1 0.3 0.5 0.7 0.9 1.1. Make it twice: with
loop
and for
loop.
Expected output:
the result with a loop 0.1 0.3 0.5 0.7 0.9 1.1 the result with a FOR loop 0.1 0.3 0.5 0.7 0.9 1.1
[Program name: L7-task-09.pas
]
{0.4 points}
Task 10:To do: Output the sequence: 0.0 0.5 1.0 1.5 2.0 2.5. Make it twice: with
loop
and for
loop.
Expected output:
the result with a loop 0.0 0.5 1.0 1.5 2.0 2.5 the result with a FOR loop 0.0 0.5 1.0 1.5 2.0 2.5
[Program name: L7-task-10.pas
]
{0.4 points}
Task 11:To do: A real number is given— the price of 1 kg of candy. The program has to output the prices of 1, 2, …, 10 kg of candy. Use
for
loop.
Note. Use friendly variable names (it’s better to use writelnFormat
statement).
Expected output:
'enter a price of 1 kg of candy, please: ' >>> 150.5 the cost of 1 kg = 150.5 the cost of 2 kg = 301 the cost of 3 kg = 451.5 ... the cost of 10 kg = 1505
[Program name: L7-task-11.pas
]
{0.4 points}
Task 12:To do: A real number is given— the price of 1 kg of candy. The program has to output the prices of 1.2, 1.4, 1.6 1.8, 2 kg of candy.
Note. Use friendly variable names (it’s better to use writelnFormat
statement).
Expected output:
'enter a price of 1 kg of candy, please: ' >>> 100.2 the cost of 1.2 kg = 120.24 the cost of 1.4 kg = 140.28 ... the cost of 2 kg = 200.4
[Program name: L7-task-12.pas
]
{0.4 points}
Task 13:To do: Two integers
A
and B
are given. Print the squares of all integers between A
and B
in ascending order and including these numbers themselves.
Expected output:
'enter two integers, please: A = , B = ' >>> 1 >>> -2 4, 1, 0, 1
'enter two integers, please: A = , B = ' >>> 2 >>> 3 4, 9
'enter two integers, please: A = , B = ' >>> 2 >>> 2 4
[Program name: L7-task-13.pas
]
{0.4 points}
Task 14:To do: Two integers
A
and B
are given. Print the square roots of all integers between A
and B
in ascending order and including these numbers themselves.
Expected output:
'enter two integers, please: A = , B = ' >>> 1 >>> 5 sqrt(1) = 1 sqrt(2) = 1.4142135623731 sqrt(3) = 1.73205080756888 sqrt(4) = 2 sqrt(5) = 2.23606797749979
[Program name: L7-task-14.pas
]
Extra tasks
To do: Using one loop, calculate the value of the sum given by the following formula (N> 0):
Expected output:
Please input real number X >>> 3 Please input integer N > 0 >>> 5 sum of the sequence = 10.2857142857143
✍ Algorithm:
begin var X := ReadReal('Please input real number X '); var N := ReadInteger('Please input integer N > 0'); Assert(N > 0, 'N<=0'); var sum:=0.0; sum := 0.0; var num1:=-x; var num2:=x; var counter:=1; for var i:=1 to n do begin counter*=i; sum+=(num1+num2)/(i+counter); num1*=-x; num2*=x; end; writeln($'sum of the sequence = {sum}'); end. |
{0.5 points}
Extra task 1:To do: Using one loop, calculate the value of the sum given by the following formula (N> 0):
Expected output:
Please input real number X >>> 5 Please input integer N > 0 >>> 2 sum of the sequence = 2.78693528693529
[Program name: L7-extask-1.pas
]
{0.5 points}
Extra task 2:To do: Using one loop, calculate the value of the sum given by the following formula (N> 0):
Expected output:
Please input real number X >>> 5 Please input integer N > 0 >>> 5 sum of the sequence = -491.5
[Program name: L7-extask-2.pas
]