Content of the lesson:
We want to create a procedure which will be able to compare two inserted numbers and will return true value of the variable AjeVetsiNezB (of boolean data type) in case that the first value is higher than the other one. Otherwise it will return false.
You can get 3 possible results: a = b , a < b or a > b. You should compare the values using the operator IF (condition) THEN and ELSE. The beginning and the end should be defined using the BEGIN and END commands. If the condition is not valid, the ELSE part is executed. You can see that there is no condition a > b. It is not defined because when none of the options a = b and a < b is valid, then the option a > b must be automatically valid.
program JeVetsi;
{$APPTYPE CONSOLE}
uses
SysUtils;
var a,b:integer;
AjeVetsiNezB:boolean;
begin
writeln('Enter
number a:');
readln(a);
writeln('Enter
number b:');
readln(b);
if (a = b)then
begin
AjeVetsiNezB:=false;
writeln;
writeln(a,' = ',b,' =>
The value of A is equal to the value of B' );
end
else
begin
if (a < b) then
begin
AjeVetsiNezB:=false;
writeln;
writeln(a,' < ',b,' =>
The value of A is lower than the value of B');
end
else
begin
AjeVetsiNezB:=true;
writeln;
writeln(a,' > ',b,' =>
The value of A is greater than the value of B');
end;
end;
writeln;
writeln('To
end the program press ENTER.');
readln;
end.
In case you want to specify what should happen in case that the condition is not valid, you can extend the IF-THEN command using the ELSE part which is executed when the condition is not valid.
If you want to sort an array, you have to create one at first. The constant n sets the number of items inside the array. If you want to adjust the number of items, you can easily change the value of n. Inside the type declaration you can name your array as pole and define it to contain 1 to n integer items.
To fill the array you can use a simple FOR cycle which will fill the array using inserted values from user.
const n=10;
type pole=array [1..n]of integer;
var p:pole;
for i:=1 to n do
begin
writeln('Enter
the ',i,'. value of array:');
readln(p[i]);
end;
We will sort numbers by browsing the array from the left to the right end and swapping items in case that the previous one is greater than the following one. Using the same procedure we will browse the array from right to left end to speed up the algorithm. After the first pass we have the lowest value at the beginning of the array (left end) and the highest value at the end of the array (right end) so it is unnecessary to browse these values again (we declare a new variable named k - you have to set its value to 1 before the while cycle, otherwise the program would not function properly). We will browse the array until all items are sorted - until the value of variable serazene is TRUE.
In case that the following item is lower than the previous one, we swap these values. We save the first value inside the variable pom to be able to save the second value inside the first variable and then we save the value of variable pom inside the second variable.
k:=1;
serazene:=false;
WHILE (serazene=false) DO
begin
serazene:=true;
FOR i:=(0+k) to (n-k) DO
begin
IF (p[i]>p[i+1]) THEN
begin
pom:=p[i];
p[i]:=p[i+1];
p[i+1]:=pom;
serazene:=false;
end;
end;
FOR i:=(n-k) downto (1+k) DO
begin
IF (p[i] < p[i-1]) then
begin
pom:=p[i];
p[i]:=p[i-1];
p[i-1]:=pom;
serazene:=false;
end;
end;
k:=k+1;
end;
In every pass the value of k is increased by 1 - already sorted numbers will be ignored. At the beginning of the WHILE cycle you have to set the value of variable serazene to TRUE because you assume that the array is sorted. In case that items are not sorted and any of IFs is valid, the variable serazene is set back to FALSE and the WHILE cycle is repeated.
program Setrid;
{$APPTYPE CONSOLE}
uses
SysUtils;
const n=10;
type pole=array [1..n]of integer;
var p:pole;
i,k,pom:integer;
serazene:boolean;
begin
for i:=1 to n do
begin
writeln('Enter
the ',i,'. value of array:');
readln(p[i]);
end;
k:=1;
serazene:=false;
WHILE (serazene=false) DO
begin
serazene:=true;
FOR i:=(0+k) to (n-k) DO
begin
IF (p[i] > p[i+1]) THEN
begin
pom:=p[i];
p[i]:=p[i+1];
p[i+1]:=pom;
serazene:=false;
end;
end;
FOR i:=(n-k) downto (1+k) DO
begin
IF (p[i] < p[i-1]) then
begin
pom:=p[i];
p[i]:=p[i-1];
p[i-1]:=pom;
serazene:=false;
end;
end;
k:=k+1;
end;
writeln;
for i:=1 to n do
begin
if (i=1) then
write('Sorted
values from the lowest one to the biggest one: ', p[i])
else
write(', ',p[i]);
end;
writeln;writeln;
writeln('To
end the program press ENTER.');
readln;
end.
You can find the maximum value while inserting values inside the array - the first inserted value should be inserted inside the variable max because it is empty at the beginning so there is nothing to compare the first inserted value to. We assume that the first value is the biggest one.
Then if you find a value which is higher than the value of max you should save it inside this variable.
program Maximum;
{$APPTYPE CONSOLE}
uses
SysUtils;
const n=10;
type pole=array [1..n]of integer;
var p:pole;
i,max:integer;
begin
for i:=1 to n do
begin
writeln('Enter
the ',i,'. value:');
readln(p[i]);
if (i=1) then
max:=p[1]
else
begin
if (p[i]>max) then
max:=p[i];
end;
end;
writeln;
writeln('The
maximum value is: ',max);
writeln;
writeln('To
end the program press ENTER.');
readln;
end.