Pascal Worked Example: Area of a Square

From SwinBrain

Contents

Problem

Write a program to calculate the area of a square.

Analysis

Inputs: the length of a side of the square.

Thought: Only need one value as this is a square.
Thought: What type of values can be entered? This needs to be clarified with the client. For the moment we will assume that this can be any Real value.

Calculation: The area of a square is equal to the square of the length of one of its sides.

Thought: Calculating the square of a number sounds like something that is likely to exist in the library of functions associated with the language. We could calculate this value as area := length * length, however it will be easier if we use the built in functions. Searching the rtl.pdf that comes with the Free Pascal Compiler we can find the function Sqr (Sqr (1061) Calculate the square of a value).

Algorithm

This is a simple problem and its algorithm can be kept in your mind, however it is good practice to write this down.

In pseudo code:

  1. Print program header informing user of the intent of the program.
  2. Ask user to enter the length of a side of the square.
  3. Read the value entered by the user and store in length.
  4. Calculate area and store.
  5. Print area to console.

Thought: Do we need to calculate the area of just one square or does the user want to be able to calculate the area of a number of squares? This needs to be clarified with the user. For this exercise we will assume that the client just want to print the area of a single square, so the program can end after the steps above.

Implementation

Implement program skeleton:

  1. program CalculateAreaOfSquare;
  2. { This program calculates the area of a
  3. square and prints it to the console. }
  4. var
  5. begin
  6. end.

Add the variables identified as input. Add an additional variable to store the area. As the length is a real value, the area will also be a real value.

  1. program CalculateAreaOfSquare;
  2. { This program calculates the area of a
  3. square and prints it to the console. }
  4. var
  5. length: real;
  6. area: real;
  7. begin
  8. end.

Implement the steps of the algorithm. This can be done in the order that they are defined.

  1. program CalculateAreaOfSquare;
  2. { This program calculates the area of a
  3. square and prints it to the console. }
  4. var
  5. length: real;
  6. area: real;
  7. begin
  8. //Print program header informing user of the intent of the program.
  9. Writeln('----------------------------------');
  10. Writeln('- Calculate the Area of a Square -');
  11. Writeln('----------------------------------');
  12. //Ask user to enter the length of a side of the square.
  13. Write('Please enter the length of a side of the sqaure: ');
  14. //Read the value entered by the user and store in length.
  15. Readln(length);
  16. //Calculate area and store.
  17. area := Sqr(length);
  18. //Print area to console - format area value to 10 spaces with two decimal places.
  19. Writeln('The area of the square is ', area:10:2);
  20. end.

Run the program and test it to ensure that it works correctly.

Thought: are the prompts what the user intended? You should show the program to the client and verify these details with them. For a simple program this is not a big problem but it is something that you need to keep in mind.

Important Points: Notice the layout of the program... Divide the logic areas in the code into separate areas and indent things inside of compound statements. The expression area:10:2 is used to format the output to 10 spaces, with two decimal places.

[edit]Sample Code » C | C++ | .NET | Pascal | Perl | T-Sql | XHTML | Python | others to come