Repeating in Pascal (second part)

first part -- second part

After theoretical start we will have some examples of use. Let us calculate sum of numbers from 1 to 100. We will start with zero, and add the counter, which will go from 1 to 100.

var
  counter : Integer;
  sum : Integer;

begin
  sum := 0;
  for counter := 1 to 100 do begin
    sum := sum + counter
  end;
  WriteLn('The sum of numbers from 1 to 100 is ', sum)
end

Now let us display characters of a given string, one after another. And in addition to splitting the string in pieces, let us also join them in the reverse order. We will make a reverse string by adding characters to its beginning. To find the length of string we will use function "Length".

We will call our string "ztring", because the word "string" is already reserved in Pascal language. ;-)

var
  ztring : String;
  counter : Integer;
  character : Char;
  ztring_reverse : String;

begin
  ztring := 'Hello world';
  ztring_reverse := '';
  for counter := 1 to Length(ztring) do begin
    character := ztring[counter];
    WriteLn('On position number ', counter, ' there is character ', character);
    ztring_reverse := character + ztring_reverse
  end;
  WriteLn('The reverse string is ', ztring_reverse)
end

Finally a complex example. We will ask five numbers from the user. Then we will write them all... in one line. To do this, we will instead of command "WriteLn" use only "Write"; only at the end we will finish the line by "WriteLn". Then we will write the smallest and greatest number, and a sum of all numbers. (Is it getting too complex? Do not fear.)

We will initialize the sum by zero; but how should we initialize the smallest and greatest number? We can initialize them by the first of these five numbers (it does not matter which one we will use). Next we will compare the smallest number so far, and the greatest number so far, with the following numbers. If we find number smaller than the smallest one, or greater than the greatest one, we will remember the new number.

var
  numbers : Array[1..5] of Integer;
  counter : Integer;
  smallest : Integer;
  greatest : Integer;
  sum : Integer;

begin
  for counter := 1 to 5 do begin
    Write('Enter the number: ');
    ReadLn(numbers[counter])
  end;

  smallest := numbers[1];
  greatest := numbers[1];
  sum := 0;
  Write('You have entered numbers:');

  for counter := 1 to 5 do begin
    Write(' ', numbers[counter]);
    if numbers[counter] < smallest then begin
      smallest := numbers[counter]
    end;
    if numbers[counter] > greatest then begin
      greatest := numbers[counter]
    end;
    sum := sum + numbers[counter]
  end;

  WriteLn('.');
  WriteLn('The smallest one is ', smallest);
  WriteLn('The greatest one is ', greatest);
  WriteLn('The sum is ', sum)
end

On this last example I see a few things which could be changed. We six times repeat "numbers[counter]", which means the number at the position of the counter. The program would be nicer to read, though a bit longer, if we would create a new variable "number", and at the beginning of the cycle we would assign it the value "numbers[counter]". Then we would just work with "number" variable.

We initialized variables "smallest" and "greatest" variables by the first of five numbers. And then we go from the first to the fifth comparing... but comparing the first one is useless. What if we would repeat only from the second to the fifth number? But the first number is also used elsewhere; we need to put it into variable "sum", and also to write it in the first "Write" command.

What? Well, this is your homework. You did not expect me to program everything for you, did you? ;-)

Comments: 0


Google