Algorithms
Algorithms
CSC 1203
1
Introduction
• A program is a set of instructions to execute or solve a
specific problem
• A program is written in one of the computer languages and
it specifies what a computer can do at a specified time
2
Steps in Writing a Program
• Description of the Problem: A programmer starts with a good
description of the problem statement.ie identifying and defining
what the program should do
• Mapping out the Program Logic: The programmer creates
algorithms where complex computation is required and puts
some mathematical and logical expressions to represent the
logic. Tools like program flow chart and pseudo code are used
• Program Coding: this is where the programming logic is
translated into any of the programming languages. The syntax
and semantics are abided by as the selected programming
language dictates.
3
Steps in Writing a Program
– Syntax: These are the rules governing the creation of program
statements
– Semantics: These are the rules governing the extracted
meanings of the program statements
• Program Testing: After the program is coded, it is
checked to establish whether it performs the
intended tasks
• Documentation: After program testing and
verification, documentation is made to guide the
users on how to use the program (User
Documentation). Another document is written to
guide software engineers on how to maintain the
program (Technical Documentation)
4
ALGORITHMS
• An algorithm is a procedure for solving a problem in terms of
the actions to be executed and the sequence in which actions
will be executed
• An algorithm is a well-defined set of steps that provides a
solution to a specific problem
• Characteristics:
– An algorithm should have zero or more input.
– An algorithm should exhibit at least one output.
– An algorithm should be finite.
– Each instruction in an algorithm should be defined clearly.
– Each instruction used in an algorithm should be basic and
easy to perform.
5
ALGORITHM TOOLS
• Pseudo codes and program flow charts
PSEUDO CODE
• Pseudo code is an artificial and informal language that helps
programmers develop computer algorithms
• It is English that has been formalized and abbreviated to look
like high-level computer programming languages
FLOW CHART
• A program flow chart is a graphical representation of a
computer algorithm, showing all the steps leading to a
solution of a given task, through the use of standard
symbols.
6
PSEUDO CODE TERMS
• READ, INPUT
– Input process
• PRINT, OUTPUT, WRITE
– Output process
• SET, LET, ASSIGN
– Computing process
• IF, CASE
– Branching / evaluation of a condition
• DO, LOOP, WHILE, REPEAT, FOR
– Iteration / repetition of a process
7
FLOW CHART SYMBOLS
• Oval: start / stop execution
8
Pseudocode & Algorithm
• Exercise 1: Write an algorithm to determine a student’s final
grade and indicate whether it is PASS or FAIL. The final grade
is calculated as the average of four marks.
Pseudocode:
– Input a set of 4 marks
– Calculate their average by summing and dividing by 4
– if average is below 50
Print “FAIL”
else
Print “PASS”
end if
9
Exercise 1
Flowchart
START
STOP
10
Exercise 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
• Input the length in feet (Lft)
• Calculate the length in cm (Lcm) by
multiplying Lft with 33
• Print length in cm (Lcm)
11
Exercise 2
Algorithm Flowchart
• Step 1: Input Lft
START
Lcm Lft x 30
Print
Lcm
STOP
12
Exercise 3
Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its
area.
Pseudocode
• Input the width (W) and Length (L) of a rectangle
• Calculate the area (A) by multiplying L with W
• Print A
13
Exercise 3
Flowchart
Algorithm START
Print
A
STOP
14
DECISION STRUCTURES
• The expression A>B is a logical expression
• it describes a condition we want to test
• if A>B is true (if A is greater than B) we take the
action on left
• print the value of A
• if A>B is false (if A is not greater than B) we take the
action on right
• print the value of B
15
DECISION STRUCTURES
Y is N
A>B
Print A Print B
16
IF–THEN–ELSE STRUCTURE
• The structure is as follows
If condition then
true alternative
else
false alternative
endif
17
IF–THEN–ELSE STRUCTURE
• The algorithm for the flowchart is as follows:
If A>B then
print A
else Y is N
A>B
print B
endif Print A Print B
18
Exercise 5
• Write an algorithm that reads two values, determines the
largest value and prints the largest value with an identifying
message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
19
Exercise 5
START
Input
VALUE1,VALUE2
Y is
N
VALUE1>VALUE2
Print
“The largest value is”, MAX
STOP
20
NESTED IF
• One of the alternatives within an IF–THEN–ELSE statement
– may involve further IF–THEN–ELSE statement
Bonus Schedule
OVERTIME – ABSENT Bonus Paid
>40 hours $50
>30 but 40 hours $40
>20 but 30 hours $30
>10 but 20 hours $20
10 hours $10
23