Day 8

This is the Day 8 page for computer programming course 101, an introduction to computer programming. Day 8 is teaches finding and correcting errors in your programs.

Below are the topics and course materials we are learning today.

Finding Mistakes

Every programmer makes mistakes, but a good programmer can find and correct his mistakes. The routines below each have one or more mistakes. In programming parlance mistakes can be categorized into one of two groups: errors or bugs.

Errors are mistakes the compiler can find. When you compile a program the compiler will check for errors and attempt to help you fix the error by showing you its location and telling you about the error so that you can correct it.

Bugs are mistakes that are a bit harder to track down. Sometimes a bug isn't something you created, but rather a problem with operating system or code libraries you are using. This can make them harder to find. If the compiler things you might be introducing a bug it will generate a hint to help you find a bug sooner rather than later.

Try to find the mistake in the
Add
procedure below. Do you think it's an error or a bug?

Hint: You can copy the listing below into a test program to see how the compiler reacts to the mistake.
procedure Add(A, B Integer);
var
  S: string;
begin
  S := A + B;
  WriteLn(S);
end;
The
Max
procedure has a problem. Can you spot it?
procedure Max(A, B: string): Integer;
begin
  if A > B then
    Result := A
  else    
    Result := B;
end;
What is wrong with the
Swap
procedure?
procedure Swap(var A, B: Char);
var
  C: string;
begin
  C := A;
  A := B;
  B := C;
end;
The
StrOf
function repeats
C
a
Len
number of times, but what is the problem?
function StrOf(C: Char; Len: Integer): string;
begin
  SetLength(Result, Len);
  for I := 1 to Length(Result) do
    Result[I] := C;
end;
The
StrReverse
function reverses the value of
S
and returns it to the user, but what going wrong with it?
function StrReverse(S: string): Integer;
var
  I: Integer;
begin
  for I := 1 to Length(S) do
    Result := S[I] + Result;
end;
The
IsOdd
function returns
True
if
N
is an odd number, otherwise it returns
False
. Can you find the problem?
procedure IsOdd(N: Integer): Boolean;
begin
  Result := N and 1 := 1;
end;
We can calculate the distance around a circle using the
Circumference
function, but does it have a bug or an error?
procedure Circumference(Radius: Double): Double;
const
  PI = '3.1415927';
begin
  Result := Radius * 2 * Pi;
end;
We can calculate the distance to a point with our
Distance
function, but what's wrong?
function Distance(X, Y Double): Double;
begin
  Sqrt(X * X + Y * Y);
end;
To secure our program we require a password. The
CheckPassword
function does this for use, will it work?
function CheckPassword(const Password: string) Boolean;
begin
  Result := Password = 'oursecret'
end;
We want a
Countdown
in our program. Does the procedure look correct?
procedure Countdown(Value: Integer);
begin
  while Value > '0' do
  begin
    WriteLn('Launching in ... ', Value);
    Value := Value - 1;
    Sleep(1000);
  end;
end;
We want a way to
DetermineAge
but it has a few bugs. Can you fix the bugs?
function DetermineAge(Age: Integer): string;
begin
  if Age > 0 then
    WriteLn('You are a baby')
  else if Age > 3 then
    WriteLn('You are a toddler')
  else if Age > 8 then
    WriteLn('You are a child')
  else if Age > 13 then
    WriteLn('You are a teenager')
  else if Age > 20 then
    WriteLn('You are an adult');
end;
The
StrUpperCase
isn't working correctly. Why?
procedure StrUpperCase(S: string): string;
begin
  for I := 1 to Length(S) do
    if S[1] in ['a'..'z'] then
      S[1] := Chr(Ord(S[1] - $20));
end;
The
Factorial
function adds up a number, but it has a problem. What is it?
procedure Factorial(Value: Integer): Result;
begin
  while Value > 0 do
  begin
    Result := Result + Value;
    Value := Value - 1;
  end;
end;
Our
StrRepeat
has a few problems. Can you find and fix them?
procedure StrRepeat(S: string; Count: string): string;
begin
  while Count := 0 do
  begin
    Result := Result + S;
    Count := Count + 1;
  end;
end;

Homework

Your homework for today is to fix any routines above which you couldn't fix in today's session.

  • You must copy the routines you couldn't fix into a source code file
  • The file folder should be compressed with 7z and the name must be course101-day008.7z
  • The compressed project file must be uploaded to your cloud file account

See also