Lecture 7: Math |
In
the previous lectures (see aula 5 and aula
6) we have learned how to show the results of our calculations on the
screen. With write and writeln we can make our program
have output. In many cases, we would also like our program to have
input.
The user enters his name, or enters numbers that our program has to process.
Or even more simple, we want that the users can control the program. For
example, we want that the user can stop the program with a simple stroke
of the escape key.
Read(a, b, i); Note that these variables do not have to be of the same type. In the above example they could be of type real, boolean and integer respectively. As an example PROGRAM ReadExample; VAR n1, n2: integer; begin
When run, the program will display the message
The difference between Read and ReadLn is that the
ReadLn
statement discards all other values on the same line, while
Read
doesn't. In the above example, if we had replaced the Read instructions
with ReadLn instructions, the program would have assigned 128
to n1, then discard the rest of the text the user typed and consequently
wait for more input to assign to n2.
|
|
|
|
|
|
|
|
|
|
|
These four operators, when used for calulations, need two operands.
In PASCAL one is placed on the left side and one on the right side. As
examples:
|
|
|
|
|
|
These expressions will result in values that can be assigned to variables as we have seen in lecture 6. As an example:
c := 3 * a;
Note again, on the left side of the assignment symbol := we have a variable
and on the left side we put our expression resulting in a new value for
the variable.
The operators shown in the previous section are used for floating
point calculations. For integer calculations (used for types like byte,
word, integer, longint, etc), the division operator is not used. Instead,
we have two new operators that also have an equivalent in modern mathematics.
Imagine the calculation of, for example, 7/3. As we have learned in primary
school, this is equal to 2 with a remainder of 1 to be divided
by 3:
|
|
||
|
|
|
|
|
|
In PASCAL exist two operators Div and Mod that reproduce
these results. Example:
|
|
|
|
|
|
These replace the floating point operator /. The other three operators
(*, +, -) are the same for integer numbers.
VAR x, y, sum, diff, divis: real;
begin
WriteLn('Give the value of the first
variable x:');
ReadLn(x);
Writeln('Give the value of the second
variable y:');
ReadLn(y);
sum := x + y;
Writeln('The sum of ', x:0:4, ' and
', y:0:4, ' is ', sum:0:4);
diff := x - y;
Writeln('The difference between ',
x:0:4, ' and ', y:0:4, ' is ', diff:0:4);
divis := x / y;
Writeln(x:0:4, ' divided by ', y:0:4,
' is ', divis:0:4);
end.
will produce, when running:
Give the value of the first variable x:
3.4
Give the value of the second variable y:
1.8
The sum of 3.4000 and 1.8000 is 5.2000
The difference between 3.4000 and 1.8000
is 1.6000
3.4000 divided by 1.8000 is 1.8889
Note the format specifiers in the writeln statement (:0:4), as described in lecture 6.
PROGRAM IntegerCalculations;
VAR x, y, modd, divv: integer;
begin
WriteLn('Give the value of the first
variable x:');
ReadLn(x);
Writeln('Give the value of the second
variable y:');
ReadLn(y);
sum := x + y;
Writeln('The sum of ', x, ' and ',
y, ' is ', sum);
diff := x - y;
Writeln('The difference between ',
x, ' and ', y, ' is ', diff);
divv := x Div y;
modd := x Mod y;
Writeln(x, ' divided by ', y, ' is
', divis, ' plus ', modd, '/', y);
end.
will produce, when running:
Give the value of the first variable x:
13
Give the value of the second variable y:
5
The sum of 13 and 5 is 18
The difference between 13 and 5 is 8
13 divided by 5 is 2 plus 3/5