3. Labs and Tasks
Sample 1:
To do: Calculate the expression. The values of x, y and z are entered.
Expected output:
Input x 3 Input y 4 Input z 5 result = 1.77800712886037
[Program name: L1sample1.pas]
Algorithm (how to do):
{0.2}
Task 1:
To do: Calculate an average of two variables a and b (formula for calculation: a + b)/2). Values of variables are provided (a=5, b=6). You should do this task twice with different ways of assigning and output.
Expected output:
(5 + 6) / 2 = 5.5
[Program name: L1task00.pas and L1task01.pas]
Sample 2:
To do: The side of a square (variable name is side) is entered. Calculate its perimeter: P = 4·a. Use different methods of assigning, input and output.
Expected output:
please enter the side length of a square: 5.6 Perimeter P = 22.4[Program name: L1sample2.pas]
Algorithm (how to do):
1-st way:begin // Variable declaration to store the value of the side length var a := ReadReal('please enter the side length of a square:'); var p := 4 * a; // Perimeter calculation Print($'Perimeter P = {p}'); end.2-nd way:
begin PrintLn('please enter a side length of a square:'); // Variable declaration to store the value of the side length var a: real; readln(a); var p := 4 * a; // Perimeter calculation Print('Perimeter P = ', p); end.
{0.3}
Task 3:
To do: The side of the square (variable name is side) is entered. Calculate an area of the square: S = a². You should use different methods of assigning, input and output.
Note: To calculate square of a number you can use sqr() standard function, for example:
sqrX := sqr(x);
Expected output:
enter a side length of a square: 2.90 Area S = 8.41
[Program name: L1task03.pas]
{0.3}
Task 4:
To do: The sides of the rectangle are entered (a and b). Calculate an area of the rectangle (S = a*b) and its perimeter (P = 2*(a + b)).
Note: To specify a particular number of digits after the floating point you can use format expression of writeln function:
writeln('S = ', S:0:2); // :2 means the number of digits to output after floating point
Expected output:
Enter the values of two sides: 12 13 result: S = 156.00 P = 50.00
[Program name: L1task04.pas]
{0.4}
Task 5:
To do: A diameter of a circle (variable name is d) is entered. Calculate its length (formula L = π·d). The value of π is 3.14. Use different methods of assigning, input and output.
Note 1: π has a constant value. In PascalABC we can declare constant before the begin section of the program:
const pi = 3.14; begin // ... end.
Note 2: Make the program using the same style of coding as in sample 2.
Expected output:
please enter a diameter of a circle: 6.7 the length of a circle is: 21.038
[Program name: L1task05.pas]
Sample 3:
To do: Calculate hypotenuse and perimeter of a right-angled triangle; legs of the triangle are entered (square root of (a² + b²)).
Note: To calculate square root of a number you can use sqrt() standard function, for example:
sqrtX := sqrt(x);
Expected output:
Input the values of triangle legs: 3.0 6.0 hypotenuse = 6.70820393249937 perimeter = 15.7082039324994
[Program name: L1sample3.pas]
Algorithm:
Here is an example of right program which is clear for user:![]()
{0.4}
Task 6:
To do: A length of a cube side is entered (a). Calculate a volume of the cube (V = a³) and its surface area (S = 6·a²). Give the program log in the form of a comment.
Note: To specify a particular number of digits after the floating point you can use format expression of writeln function:
writeln('V = ', v:5:3)
5 means total number of signs to output the number,3 means the number of digits to output after floating point.
Expected output:
enter a cube side length: 9.000 V = 729.000 S = 486.000
[Program name: L1task06.pas]
{0.4} Task 7:
To do: Assign a value to integer variable x (x = 5). Calculate the value of the function:
y = 4(x-3)^6 - 7(x-3)^3 + 2
Note 1: To calculate the power of a number you can use the power(x:real, y:real) function. For example:
//2 in the power of 5 = powNumb = power (2,5);
Note 2: It is better to use an auxiliary variable for (x-3)^3.
Expected output:
for x = 5 we have y = 202
[Program name: L1task07.pas]
{0.4}
Task 8:
To do: Calculate a distance between two points with the given coordinates x₁ and x₂ on the number axis; the coordinates are entered. The formula is |x₂ − x₁|.
Note: To calculate the absolute value of a number you can use abs(x:real) standard function:
abs(x2 - x1);
Expected output:
x1 = 3.2 x2 = 2.5 the distance between two points: 0.7
[Program name: L1task08.pas]
{0.4} Task 9:
To do: Calculate a distance between two points on the plane; coordinates (x₁,y₁) and (x₂,y₂) are entered. The distance is calculated by the formula:
Note 1: Verify the correction of your program by using “simple” values that are easy to calculate. For example:
d((0, 0); (6, 0)) = 6; d((0, -4); (0, 1)) = 5; d((-1, 1); (2, 5)) = 5:
Note 2: Display the results of your program (log) in the form of a comment after the program code. It's easy to do by copying and pasting. You should use curly brackets for comments:
Expected output:
enter x1 of the first point: 0 enter y1 of the first point: 0 enter x2 of the second point: 6 enter y2 of the second point: 0 The distance equals 6
[Program name: L1task09.pas]
{0.3}
Task 10:
To do: The temperature in Celsius is entered, convert temperature to Fahrenheit. Celsius and Fahrenheit scales are related by the ratio:
(°F = °C × 9/5 + 32)
Expected output:
enter the temperature in Celsius 56 The temperature in Fahrenheit 132.8
[Program name: L1task10.pas]
{0.2}
Task 11:
To do: Swap the values of variables A and B and print out the new values to the console.
Expected output:
Enter A: 5.7 Enter B: 3 Result: A = 3, B = 5.7
[Program name: L1task11.pas]
{0.2}
Task 12:
To do: The values of variables A, B, C are entered. Swap their values to make A = B, B = C, C = A, and display the results.
Expected output:
A = 3.4 B = 2 C = 1.5 Result: A = 1.5, B = 3.4, C = 2
[Program name: L1task12.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.
BEGIN
Begin12.
The legs a and b of a right triangle are given. Find the hypotenuse c and the perimeter P of the triangle:
c = √(a² + b²), P = a + b + c.
Expected output:
<< a=4.90 << b=9.90 results: c=11.05 P=25.85
Begin17.
Three points A, B, C are given on the real axis. Find the length of AC, the length of BC, and the sum of these lengths.
Expected output:
<< A=-3.80 << B=3.40 << C=0.50 results: AC=4.30 BC=2.90 AC+BC=7.20
Begin23.
Variables A, B, C are given. Change values of the variables by moving the given value of A into the variable B, the given value of B into the variable C, and the given value of C into the variable A. Output the new values of A, B, C.
Expected output:
<< A=2.47 << B=1.41 << C=9.50 results: A=9.50 B=2.47 C=1.41
Begin27.
Given a number A, compute a power A⁸ using three multiplying operators for computing A², A⁴, A⁸ sequentially. Output all obtained powers of the number A.
Expected output:
<< A=3.20 results: A²=10.24 A⁴=104.86 A⁸=1095.12
Begin28.
Given a number A, compute a power A¹⁵ using five multiplying operators for computing A², A³, A⁵, A¹⁰, A¹⁵ sequentially. Output all obtained powers of the number A.
Expected output:
<< A=1.57 results: A²=2.46 A³=3.87 A⁵=9.54 A¹⁰=90.99 A¹⁵=867.95
Begin40.
Solve a system of linear equations
A₁·x + B₁·y = C₁, A₂·x + B₂·y = C₂
with given coefficients A₁, B₁, C₁, A₂, B₂, C₂ provided that the system has the only solution. Use the following formulas:
x = (C₁·B₂ − C₂·B₁)/D, y = (A₁·C₂ − A₂·C₁)/D, where D = A₁·B₂ − A₂·B₁.
Expected output:
<< A₁=-3.00 << B₁=-2.00 << C₁=4.00 << A₂=-1.00 << B₂=-4.00 << C₂=-2.00 results: x = -2.00 y = 1.00