Czech version
logolink

< Back to the list of lessons

Basic Programming Constructions II

AlgortimyContent of the lesson:

  • Procedure Prumer which writes the arithmetic mean of array
  • Procedure Kladne which writes only positive numbers from array
  • Procedure Vydel which computes quotient of two numbers and writes the result
  • Procedure Porovnej which compares three numbers and writes the result

Procedure Prumer which writes the arithmetic mean of array

To compute the arithmetic mean of all items you will need two variables. Soucet - the total sum of all items inside the array, prum - variable to store the final value of arithmetic mean. When inserting items into the array you should also add them to the variable soucet (do not forget to also add the current value - soucet:=soucet+p[i]). When the array if full you can compute the arithmetic mean - the variable Soucet should be divided by the number of items inside the array ( prum:=soucet/n ).

Algorithm to compute the arithmetic mean of array
 program Prumer;
 {$APPTYPE CONSOLE}
 uses
   SysUtils;
 const n=10;
 type pole=array [1..n]of integer;
 var p:pole;
     i,soucet:integer;
     prum:real;
 begin
  soucet:=0;
  prum:=0;
  for i:=1 to n do
   begin
    writeln('Enter ',i,'. item:');
    readln(p[i]);
    soucet:=soucet+p[i];
   end;
  prum:=soucet/n;
  writeln;
  writeln('Arithmetic mean is: ',prum:2:2);
  writeln;
  writeln('Press ENTER to exit.');
  readln;
 end.

Procedure Kladne which writes only positive numbers from array

To write positive numbers only you can use FOR cycle to go through the whole array and write only those values which are positive ( if(p[i]>=0)then ). We will use another variable pocet because we want to write all values into one line. This variable will indicate the number of positive values which were found. If the value of pocet is 0 (pocet=0) then we will use this command: write('Positive values are: ',p[i]). In case we used the writeln command, all other items would be written on the following line.

If the program writes the second, third, ... number (pocet>0) then only colon and the current value are written - you will separate all values using this way.

Algorithm for writing positive numbers only
 program Kladne;
 {$APPTYPE CONSOLE}
 uses
   SysUtils;
 const n=10;
 type pole=array [1..n]of integer;
 var p:pole;
    i,pocet:integer;
 begin
  pocet:=0;
  for i:=1 to n do
   begin
    writeln('Enter ',i,'. item:');
    readln(p[i]);
   end;
  for i:=1 to n do
   begin
    if(p[i]>=0)then
     begin
      if(pocet=0)then
       write('Positive values are: ',p[i])
      else
       write(', ',p[i]);
    pocet:=pocet+1;
     end;
   end;
  if (pocet=0) then
   begin
    writeln;
    writeln('No value is positive');writeln;
   end;
  writeln;writeln;
  writeln('Press ENTER to exit');
  readln;
 end.

Procedure Vydel which computes quotient of two numbers and writes the result

Our task is to create a program which can compute quotient of two numbers. To assign the numbers you should use the function readln which waits until the enter key is pressed and then saves the value from console inside the variable written in brackets. You should realize that division by zero is not acceptable so you have to check values. You can use while cycle which will ask the user to enter a different number (not zero). User will not get behind this check until he inserts a valid number.

After inserting values, the quotient of a and b is saved into the variable výsledek which has to be of the real data type. When computing a quotient of two numbers you might not get an integer result.

In case you wrote vysledek when writing the variable to console and not vysledek:2:2 the program would write a strange result: vysledek =5.00000000000000E+0000 (a=10, b=2), but you know that the result should be 5. The notation means 5*10^0 or 5. The first number behind the variable sets the format of output number to contain at least 2 valid digits. The second one sets the amount of decimal places.

Algorithm for computing quotient of two numbers
 program Vydel;
 {$APPTYPE CONSOLE}
 uses
  SysUtils;
 var a,b:integer;
     vysledek:real;
 begin
  writeln('Enter number a:');
  readln(a);
  writeln('Enter number b:');
  readln(b);
  if (b=0) then
   begin
    while (b=0) do
     begin
      writeln('Division by zero is not acceptable, enter another number:');
      readln(b);
     end;
     vysledek:=a/b;
     writeln('The quotient of a and b is: ',a,'/',b,' = ',vysledek:2:2);
    end
  else
   begin
    vysledek:=a/b;
    writeln('The quotient of a and b is: ',a,'/',b,' = ',vysledek:2:2);
   end;
  writeln;
  writeln('Press ENTER to exit');
  readln;
 end.

Procedure Porovnej which compares three numbers and writes the result

The principle of this procedure is similar to Bubblesort (which is mentioned in the previous lesson) but we will use FOR cycle instead of WHILE cycle and sort the values from the highest one to the lowest one. We have to compare three numbers so set the value of const to 3. To sort all items you need the FOR cycles to run only twice (n-1 times). To write the result we can use a FOR cycle again.

Algorithm to compare three numbers
 program Porovnej;
 {$APPTYPE CONSOLE}
 uses
   SysUtils;
 const n=3;
 type pole=array [1..n]of integer;
 var p:pole;
     i,k,pom:integer;
 begin
  for i:=1 to n do
   begin
    writeln('Enter ',i,'. item:');
    readln(p[i]);
   end;
  for k:=1 to n-1 do
   begin
    for i:=1 to n-1 do
     begin
      if p[i+1]>p[i] then
       begin
        pom:=p[i];
        p[i]:=p[i+1];
        p[i+1]:=pom;
       end;
     end;
   end;
  writeln;
  for i:=1 to n do
   begin
    if (i=1)then
     write('Compared values from the highest one to the lowest one: ', p[i])
    else
     write(', ',p[i]);
   end;
  writeln;writeln;
  writeln('Press ENTER to exit');
  readln;
 end.
webdesign, xhtml, css, php - Mgr. Michal Mikláš