Lesson #1. Introduction to PascalABC.NET
Be sure to read the requirements for labs.
[`task-01.pas`
] Examine the example, create a file with this program and run it.
// The program that outputs the greeting - this is a comment to the program program Hello; begin Writeln('Hello world'); end.
Definition of variable
In Pascal abc.net variables may be defined within the body of the program between begin and end. The principle of locality: the variable is defined immediately before its use.
In traditional pascal:
var n: integer; begin n:=1; // assignment statement
pascalabs.net:
1 method:
begin var n:integer; n:=1;2 method (canonical method when type is defined depending on the value):
begin var n:=1;
Arithmetic operations, assigning value to a variable and expressions
common method:
begin var a := 6; // Assigning value 6 a:= a + 2; // Increase by 2 a:= a - 2; // Reduction of 2 a:= a * 3; // Multiplication by 3 a:= a / 2; // division end.
short method:
begin var a := 6; // Assigning value 6 a+= 2; // Increase by 2 a-= 2; // Reduction of 2 a*= 3; // Multiplication by 3 a/= 2; // division end.
Data input
1.
begin var n:integer; read(n);
begin var n:real; read(n);2.
var n:=ReadInteger(); var n:=ReadReal();3.
(n1, n2) := (1, 2);4.
var(n1, n2) := readInteger2;
Data output:
1 method:
begin var n:integer; read(n); n: = n * n; writeln('n = ',n);2 method:
begin begin var n:integer; read(n); n: = n * n; print('n = ',n);
Tasks
Follow the rules:
- Save your files with names as it is given in tasks (e.g. task-04.pas).
- Give meaningfull names to your variables.
- Use comments to make the program clear.
- Give the task of the program in the form of a comment before the program code. For comments use curly brackets:
- Give the results of your program (log) as a comment after the program code. It’s easy to do just by copying. Use curly brackets to add comments :
Example
Here is an example of right program which is clear for user:
The resulting example:
Input x 3 Input y 4 Input z 5 The result = 1.77800712886037! Make the next program using the same style of coding:
[task-000.pas
] Calculate the value of the function y = 4(x-3)^6 - 7(x-3)^3 + 2 , if x=5
.
Note. Use the aúxiliary variable for (x-3)^3. Give the program log in the form of a comment.
[task-001.pas
] The side of the square (a) is entered. Find its perimeter: P = 4·a. Use different methods of assigning, input and output.begin Writeln('the side length of a square:'); // Variable declaration to store the value of the side length var a := ReadReal; // Perimeter calculation var P := 4 * a; Writeln('Perimeter P = ', P); end.
[task-002.pas
] The diameter of a circle (d) is given. Find its length: L = π·d. The value of π is 3.14. Use different methods of assigning, input and output.
[task-003.pas
] Two numbers a and b are given . Find their average: (a + b)/2. Use different methods of assigning, input and output.
!
[task-004.pas
] Calculate hypotenuse and perimeter of a right-angled triangle, legs of triangle is given (root of (a2 + b2)) .
Here is an example of right program which is clear for user:
Possible result:
Input the values of triangle legs: 3 6 hypotenuse = 6.70820393249937 perimeter = 15.7082039324994
! Make the next program using the same style of coding. Pay attention to the names of the variables (they have meaning names, that shows the variables’ task in the program):
[task-006.pas
] The length a
of a cube edge is given. Find the volume of the cube and its surface area. Give the program log in the form of a comment.! There is another way of the variables definition (the task about hypotenuse):
[extra-task-03.pas]
Modify previous program about the cube using this way of variable definition and clear (meaning) variables’ names.
[extra-task-04.pas]
Find the distance between two points on the plane; coordinates (x1
,y1
) and (x2
,y2
) are given. The distance is calculated by the formula:
Check: Check the correctness of your program with "simple" values that are easily to calculate. For example:
- d((0, 0); (6, 0)) = 6;
- d((0, -4); (0, 1)) = 5;
- d((-1, 1); (2, 5)) = 5.
Give the results of your program (log) in the form of a comment after the program code. It’s easy to do by simply copying. For comments use curly brackets:
[task-007.pas
] The temperature in Celsius is given, convert temperature to Fahrenheit. Celsius and Fahrenheit scales are related by the ratio:
The input/output operators and assignment operator
Examine the examples at the link. Make the programm when it's needed.
begin Writeln('Enter the lengths of the legs (two numbers separated by a space):'); // Variable declaration for storing the legs var a, b: real; // Input. This form is shorter than the ReadReal function, if there are several variables Read(a, b); var c := sqrt(a*a + b*b); Writeln('Hypotenuse c = ', c); //Calculation of the perimeter of a triangle. var P := a + b + c;
Calculation the value of an expression.
-
[
task-02.pas
] Cálculate the sine and cosine of the angle given in radians.Note. If necessary, use the help for mathematical functions: in menu
Help / Помощь/Справка
— «Справочник по языку» — «Системный модуль PABCSystem» — «Математические подпрограммы». -
{
2 points
} [task-03.pas
] Cálculate the value of the lógarithm of 1024 to base 2, using theln
, function that calculates the natural logarithm of a given number, and the formula:\[\log_a b = \frac{\ln b}{\ln a}\]
-
[
task-04.pas
] Cálculate the value of the expression:\[\frac{x + \sin x}{y - \sin z} + \ln{(x + \sin x)}\]
-
[
Check. Check the correctness of your program on "simple" values that are easily counted manually. For example::task-05.pas
]Find the distance between two points with given coórdinates \((x_1, y_1)\) и \((x_2, y_2)\) on the plane. The distance is cálculated by the formula \(d=\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\).- d((0, 0); (6, 0)) = 6;
- d((0, -4); (0, 1)) = 5;
- d((-1, 1); (2, 5)) = 5.
Give the results (log) of your program in the output window PascalABC.NET in the form of a comment after the program code. It’s easy to do by simply copying.
-
{
2.5 points
} [task-06.pas
] Calculate the value of the function \(y = 4(x-3)^6 - 7(x-3)^3 + 2\) for several givenx
(that is, run the program several times for different values ofx
).Note. Use the aúxiliary variable for \((x-3)^3\). Give the program log in the form of a comment.
-
[
task-7.pas
] The temperature in Celsius is given, convert temperature to Fahrenheit. Celsius and Fahrenheit scales are related by the ratio: -
{
Mark: 3
} [task-8.pas
] The radii of two concentric circles are given. Calculate the area of the ring enclosed between them.
\[t^{\circ}C = \frac{5}{9}(t^{\circ}F-32)\]
Проверка. Check here WolframAlpha. Use the following query : convert 5 Celsius to Fahrenheit
. Give the log of the programm.
Extra tasks
-
[
task-9.pas
] The length of the cube edgea
is given. Find the volume of the cube and its surface area. Give the program log in the form of a comment. -
[
task-10.pas
] The perimeter of the réctángle and the length of its diágonal are known. Find the sides of the rectangle. Give the program log in the form of a comment.