# 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:

- Print program header informing user of the intent of the program.
- Ask user to enter the length of a side of the square.
- Read the value entered by the user and store in length.
- Calculate area and store.
- 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:

program CalculateAreaOfSquare; { This program calculates the area of a square and prints it to the console. } var begin 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.

program CalculateAreaOfSquare; { This program calculates the area of a square and prints it to the console. } var length: real; area: real; begin end.

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

program CalculateAreaOfSquare; { This program calculates the area of a square and prints it to the console. } var length: real; area: real; begin //Print program header informing user of the intent of the program. Writeln('----------------------------------'); Writeln('- Calculate the Area of a Square -'); Writeln('----------------------------------'); //Ask user to enter the length of a side of the square. Write('Please enter the length of a side of the sqaure: '); //Read the value entered by the user and store in length. Readln(length); //Calculate area and store. area := Sqr(length); //Print area to console - format area value to 10 spaces with two decimal places. Writeln('The area of the square is ', area:10:2); 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.