Vous êtes sur la page 1sur 5

PROBLEM SOLVING

Declaration of variables & constants

In order for the computer to manipulate an item of data, its data type must be clearly defined. This is
referred to as declaration of variables or declaration of constants.

1. Declaring Variables.

SYNTAX

VAR
Identifier: Data Type

Example:

VAR
name: string

Example:

VAR
name : string
salary : real
age : integer

N.B You don’t have to write the identifier twice.


Multiple variables of the similar data type can be declared on the same line.

Example:

VAR
age, x, y : integer

2. Declaring Constants

Syntax

CONST
Identifier = Value
Example:

CONST
Tax: 0.25

Any scenario that involves a value that will not change, that value can be declared as a constant.

*DIV is for integers.


* / can be used for real.

Write an algorithm, to input two integer values. The algorithm must then calculate and display
their sum and average.

Decompose

IPO Table

Input Processing Output


Num_1 Sum Sum
Num_2 Average Average

START
VAR
num_1, num_2: integer
sum, average: real
READ num_1
READ num_2
LET sum = num_1 + num_2
LET average = sum/2
DISPLAY sum
DISPLAY average
END

Or

START
VAR
num_1, num_2: integer
sum, average : real
PRINT “Enter value 1”
READ num_1
PRINT “Enter value 2”
READ num_2
LET sum = num_1 + num_2
LET average = sum/2
DISPLAY sum
DISPLAY average
END

Representing an algorithm using a flow chart

A flow chart is a graphical representation of an algorithm. The following symbols are required to
draw up a flow chart:

Terminating Symbol

Input/output Symbol

Processing

Decision and Loop Selection

Page Connector
Line Connector

Flow Line

NB. The decision symbol is the only flowchart symbol which can have two or more flow lines
entering or leaving.
Example:

START

Input num_1
Input num_2

Sum = num_1 + num_2

Average =sum/2

Display sum

Display average
Write an algorithm to input two integers, the algorithm must then display the larger of the two,
assuming they are not equal.

START
VAR
A,B : integers
PRINT “Enter values for A & B”
READ A
READ B
IF A > B THEN
PRINT A

ELSE
PRINT B
ENDIF
END

Write an algorithm that will print the first ten odd numbers.

START
VAR
x, y : integers
Y=1
FOR x = 1 TO 11 DO
PRINT Y
LET Y= 1 + 2
ENDFOR
END

Vous aimerez peut-être aussi