View
218
Download
1
Category
Tags:
Preview:
Citation preview
INTRODUCTION TO ALGORITHMS
PROGRAMMING
Objectives
• Give a definition of the term algorithm
• Describe the various parts of the pseudocode
algorithm or algorithm structure.
• Understand the commands used to permit input,
output and processing of data in pseudocode.
• Write out a complete pseudocode algorithm using
sequential structures
• List the Do’s and Don’ts of writing pseudocode
What is an Algorithm?
An algorithm is a sequence of precise
instructions for solving a problem.
Pseudocode Algorithm Properties
A pseudocode algorithm must have the
following properties:
It must be:
• Precise
• Unambiguous
• Logically sequenced
• Give correct solutions in all cases
• Eventually end.
Pseudocode Algorithm Structure
Every pseudocode algorithm should have the following sections in the stated order
• Header : Algorithm’s name or title
• Declaration : A brief description of algorithm and
variables used. i.e. a statement of the purpose, as well
as stating variables.
• Body : Sequence of steps
• Terminator : An end statement
Algorithm Student data {Header}{This algorithm displays a student’s name and age on
the screen.} Name string {Declaration}Age integerStart
Display “Enter your name’Accept NameDisplay “Enter your age:” {Body}Accept AgeDisplay “Hello”, NameDisplay “You are”, Age, “years old”
Stop {Terminator}
Pseudocode Algorithm Header
• The algorithm header is usually followed by comments.
• Comments can be used to remind you of what is taking place.
• They can be used to explain or clarify any part of an algorithm.
• A comment or lack of comment has no effect on the logic of the algorithm. It is solely for documentation purposes.
• Comments are written by enclosing them in braces. A Comment extends from the left brace to the right brace, and may span one or more lines.
Declaring the Variables
• The next step is to declare the variables.
• All variables must be declared before they
can be used in the algorithm.
• The variables are followed by the data type
of the variable.
• Variable declarations must appear after the
program heading and a comment about
what the program does, but before the word
start of the main routine.
Body of the Pseudocode Algorithm
• The main executable code for the algorithm is in the body usually referred to as the main routine or simply main.
• The body consists of the main routine. The body consists of the part between Start and Stop.
• All algorithms end with a period. When we run a program it starts executing with the first statement in the main and ends when it reaches the last statement.
• The Body of the algorithm is also comprised of various structures. A structure can be sequence, a selection or a loop.
Body of the Pseudocode Algorithm
• The main routine of an algorithm or main
body of an algorithm consists of, prompt,
input, processing and output statements
that make up the various structures.
Prompt
• When developing and algorithm prompts must be made.
• Prompts are messages that appear on the screen notifying the user of what data has to be entered.
• A prompt is needed for each input statement.
• Prompt statements begin with the command PRINT followed by the string enclosed in quotes.– E.G. Print ‘Enter two numbers’
Input
• Each prompt is followed by a read.
• The command that allows the input of data is
the word Read followed by one or more
variables, used to represent the data being
entered and stored.
• When more than one variable is used a
comma is placed between each variable.
– Read num1, num2,num3
PRINTING INFORMATION OR OUTPUT
• Output can be sent to the screen when
developing an algorithm for solving a
problem.
• The command used is the word Print. Print
can be used to output the value of a variable
or to output a constant.
– Print ‘Total Revenue is ‘ TotalRevenue
PROCESSING INVOLVING CALCULATIONS
• Calculations can be done by using the
mathematical operations for addition,
subtraction, multiplication or division.
• The results of a calculation must be stored in
a variable for use.
• The result of the calculation is stored in the
variable to the left of the
PROCESSING INVOLVING CALCULATIONS
• This variable is assigned the results of the
calculation.
• Any statement, that assigns a value to a
variable, is called and assignment
statement.
• The value being assigned could be a
constant a variable a calculation or
expression.
Pseudocode Writing Do’s And Don’ts
Do’s
• Use the assignment symbol in assignment
statements not the equal sign.
• Use meaningful variable names
• Use indentation to show logic and scope
• Insert Comments to clarify meaning of blocks
of code.
Pseudocode Writing Do’s And Don’ts
Don’ts
• Do not use language specific statements in
pseudocode e.g. writeln, readln
• Do not attempt to write Pascal code before writing
the algorithm.
Template Provided By
www.animationfactory.com
500,000 Downloadable PowerPoint Templates, Animated Clip Art, Backgrounds
and Videos
Recommended