0% found this document useful (0 votes)
2 views

Lect3 Algorithms and Flowchart Ppt

The document provides an overview of algorithms and flowcharts, emphasizing the importance of problem analysis in programming. It describes the phases of problem-solving, the attributes of effective algorithms, and methods for specifying algorithms using pseudocode and flowcharts. Additionally, it includes examples and exercises to illustrate the concepts of algorithm design and flowchart creation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lect3 Algorithms and Flowchart Ppt

The document provides an overview of algorithms and flowcharts, emphasizing the importance of problem analysis in programming. It describes the phases of problem-solving, the attributes of effective algorithms, and methods for specifying algorithms using pseudocode and flowcharts. Additionally, it includes examples and exercises to illustrate the concepts of algorithm design and flowchart creation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

ALGORITHMS AND

FLOWCHARTS

Lecture 1: Problem Analysis

By: Alhaji A. Asomah


ALGORITHMS
 A typical programming task can be divided into
two phases:
 Problem solving phase
 make an ordered sequence of steps that describe
solution to problem
 this sequence of steps is called an algorithm
 Implementation phase
 implement the program in some programming
language
ALGORITHMS
 It is a list of instructions specifying a precise description
of a step-by-step process that terminates after a finite
number of steps for solving an algorithm problem
producing the correct answer in the end.
 It is a recipe for solving problems.
 A finite set of an instruction that specifies a sequence of
operations to be carried out in order to solve a specific
problem.
 An unambiguous procedure specifying a finite number of
steps to be taken.
ALGORITHMS
 In the problem-solving phase of computer programming,
you will be designing algorithms.
 This means that you will have to be conscious of the
strategies you use to solve problems in order to apply
them to programming problems.
 These algorithms can be designed th r ough the use of
flowcharts or pseudocode
ALGORITHMS
Every computer solution you write must have the following
attributes:
1. Finiteness: It implies that the computer solution should
be able to produce the required results or output after a
finite number of steps.
2. Definiteness: It means all statements in a computer
solution should be precise and well defined such that it
will not give room for ambiguity or confusion
3. Generality: It implies that a computer solution should
be capable of solving problems of a particular type or
class and not just a particular set of inputs.
ALGORITHMS
4. Effectiveness : By effectiveness, we mean that all
operations involved in a computer solution should be
basic and capable of being performed mechanically in a
finite number of steps
5. Have input and output capabilities: It implies that a
computer solution should have a precise initial input
data from a specified set that can be processed
internally to generate the expected output.
METHODS OF SPECIFYING
ALGORITHM
 Pseudocode - Pseudocode (which means fake
code, because it's not really programming code)
specifies the steps required to accomplish the
task.
 Flowchart - a traditional graphical tool with
standardized symbols. Shows the sequence of
steps in an algorithm.
Steps in Problem Solving
 First produce a general algorithm (one can use
pseudocode)
 Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
 Pseudocode is an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.
Pseudocode & Algorithm
 Example 1: Write an algorithm to
determine a student’s final grade and
indicate whether it is passing or failing.
The final grade is calculated as the
average of four marks.
Pseudocode & Algorithm
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”
Pseudocode & Algorithm
 Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
STRUCTURE OF COMPUTER
SOLUTION
 Every computer solution should have the following
structure but not necessarily in the order presented
below. This is because, computer solutions solve
different problems
 Some computer solutions may or may not require all the
steps listed below:
1. The input step: This is used to define various inouts that
are required by the computer solution. The input data can
be read from a file, entered from a keyboard or generated
through an assignment statement.
2. The assignment step: This step is used to define the
various mathematical expressions that are required to
solve a problem at stake
STRUCTURE OF COMPUTER
SOLUTION
3. The decision step: This step is used to define the
decisions that are to be taken in obtaining the
required output. Each decision involves selecting one
of the possible alternatives at a time
4. The repetitive/iterative/looping step: This is used
to define statements that are required to be executed
a number of times
5. The output step: This step defines the various
outputs that are expected to be obtained from a
computer solution
VARIABLES
 Values that you may want a computer to process are
normally kept in names known as variables.
 We shall use one word or at most two words separated
by the underscore.
 We would avoid the use of special characters,
punctuation marks, and monetary symbols in a variable
name
 For eg. If we want to accept values representing the age,
gross pay, tax and net pay, we can declare variables
such as age, grosspay, tax and netpay
HOW TO WRITE SIMPLE
COMPUTER PROGRAMS
 We will consider a few problems and see how computer
solutions are written.
 Since we have not yet learnt any programming
language, we will be using pseudocodes
 We shall try and write all our statements as close as
possible to mathematical expressions where necessary
 Non-mathematical statements should be as short as
possible
HOW TO WRITE SIMPLE
COMPUTER PROGRAMS
 As indicated earlier, a computer solution usually consists
of input, output, decision making, repetitive and
assignment statements. The following defines how to
make use of these
 For inputs: We shall try to use the following terminologies
when we want to indicate that a computer user would
have to enter a value (values) for processing: READ,
INPUT, ACCEPT, ENTER and GET
 For example: INPUT age means that a user should enter
a value to be stored in a variable called age
HOW TO WRITE SIMPLE
COMPUTER PROGRAMS
 For output, we shall use any of the following
terminologies when we want to indicate that a computer
solution should give results: WRITE, DISPLAY and
PRINT.
 For example, WRITE age means that we want the
computer to display to the screen a value stored in a
variable called age
HOW TO WRITE SIMPLE
COMPUTER PROGRAMS
 For decision making: Whenever we need to make a
decision, we shall use IF statement with the following
structure:
 IF condition THEN statement (s) ELSE statement (s)
 The statement can be spread over several lines.
 Example: If we need to compare the value in variable
A and B and write the larger of the two, then we shall
write a statement as shown below:
IF A > B
WRITE A
ELSE
WRITE B
HOW TO WRITE SIMPLE
COMPUTER PROGRAMS
 For repetitive/ iterative/looping: We shall use DO,
WHILE and FOR in most cases.
 To mark the end of a DO, for example, we shall write
or use END OF DO
RULES FOR NAMING
VAIRABLES
1. Always capitalize the initial word
2. Have only one statement per line.
3. Indent to show hierarchy, improve readability, and show
nested constructs.
4. Always end multiline sections using any of the END
keywords (END OF IF, END OF WHILE, etc.).
5. Keep your statements programming language
independent.
6. Keep it simple, concise, and readable.
Finding sum of three numbers
 Let us assume we want to write a computer solution for
finding the sum of three numbers.
 To solve this problem, we will need three variable
names to store each of the three numbers.
 Let us use the names x, y and z to represent the
numbers we want to find their sum.

The steps needed to solve the problem are as follows:


1. Accept the three numbers into the three variables x, y and z
2. Add x, y, z and store the results in sum
3. Write out sum
Finding sum of three numbers
 We can write the above statements in a more compact
way or a manner close to programming language as
follows

1. INPUT x, y, z
2. sum = x+y+z
3. WRITE sum
4. STOP
EXERCISE 1
 Write a computer solution using pseudocode to read two
numbers and multiply them together and print out their
product.
 Write a computer solution using pseudocode to accept
four marks from a person, and find the average of the
marks. If the average is less than 40, the computer
should output “FAIL” else the computer should output
“PASS”
 Write a computer solution using pseudocode to accept a
temperature reading in Fahrenheit, convert it to degrees
Celsius and display the results to the computer screen
 Write pseudo code that performs the following: Ask a
EXERCISE 2
 Write a computer solution using pseudocode to accept
four marks from a person, and find the average of the
marks. If the average is less than 40, the computer
should output “FAIL” else the computer should output
“PASS”
EXERCISE 3
 Write a computer solution using pseudocode to accept a
temperature reading in Fahrenheit, convert it to degrees
Celsius and display the results to the computer screen
EXERCISE 4
 Write pseudo code that performs the following: Ask a
user to enter a number. If the number is between 0 and
10, write the word blue. If the number is between 10 and
20, write the word red. if the number is between 20 and
30, write the word green. If it is any other number, write
that it is not a correct color option.
Pseudocode to Find the Average
of N Numbers
 Write a computer solution in pseudocode to find the
average of N numbers.
Pseudocode to Find the Average
of N Numbers
 The entire pseudocode for finding the average of N
numbers is shown below
1. SET a counter COUNTER to 0
2. Set an accumulator SUM to 0
3. Read number of students, N
4. While COUNTER <=N
5. READ a student’s age, AGE
6. ADD age to SUM
7. Increase COUNTER by 1
8. END OF WHILE
9. Computer average, AVERAGE = SUM divided by N
10. PRINT AVERAGE
11. STOP
FLOWCHARTS
The Flowchart
 (Dictionary) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
 (Technical) A graphical representation of the sequence of
operations in an information system or program.
 Information system flowcharts show how data flows from
source documents through the computer to final
distribution to users.
 Program flowcharts show the sequence of instructions in a
single program or subroutine. Different symbols are used
to draw each type of flowchart.
The Flowchart
A Flowchart
 shows logic of an algorithm
 emphasizes individual steps and their
interconnections
 e.g. control flow from one action to the next
Flowchart Symbols
Basic
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
Example 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 30
 Print length in cm (LCM)
Example 2
Flowchart
Algorithm START
 Step 1: Input Lft
Input
 Step 2: Lcm  Lft x 30 Lft

 Step 3: Print Lcm Lcm  Lft x 30

Print
Lcm

STOP
Example 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
Example 3
Algorithm START

 Step 1: Input W,L Input


W, L
 Step 2: AL x W
 Step 3: Print A ALxW

Print
A

STOP
Flowcharts
 Flowcharts is a graph used to depict or
show a step by step solution using
symbols which represent a task.
 The symbols used consist of geometrical
shapes that are connected by flow lines.
 It is an alternative to pseudocoding;
whereas a pseudocode description is
verbal, a flowchart is graphical in nature.
Principles of Programming - NI July
2005 38
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.

Process symbol - shows an instruction other than


input, output or selection.

Input-output symbol - shows an input or an output


operation.

Disk storage I/O symbol - indicates input from or output to


disk storage.

Printer output symbol - shows hardcopy printer


output.

Principles of Programming - NI July


2005 39
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.

Off-page connector - provides continuation of a


logical path on another page.

On-page connector - provides continuation


of logical path at another point in the same
page.

Flow lines - indicate the logical sequence of


execution steps in the algorithm.

Principles of Programming - NI July


2005 40
Flowchart – sequence control structure

Statement 1

Statement 2

Statement 3

Principles of Programming - NI July


2005 41
Flowchart – selection control structure

No Yes
Condition

else- then-
statement(s) statement(s)

Principles of Programming - NI July


2005 42
Flowchart – selection control structure

43
Flowchart – repetition control structure

yes Loop
Condition
Statement(s)

no

Principles of Programming - NI July


2005 44
Flowchart – example 1
Write an algorithm, using pseudocode and a flowchart to
input two numbers and find sum of these numbers.

45
Flowchart – example 1.1
Write an algorithm
using flowchart to
find the average of
three numbers

46
Flowchart – example 2
Write an algorithm, using pseudocode and a flowchart to
calculate age.
Begin

Read birth date

Calculate
Age = current year – birth date

Display
age

End
47
Flowchart – example 3
Write an algorithm, using pseudocode and a flowchart to
check age and display retired or young.
Begin

Read age

YES NO
Age > 60?

print “Retired” print “Still Young”

End
48
Flowchart – example 4
Begin

sum = 0
current_number = 1

NO
current_number <= 10? print sum

YES
End
sum = sum + current_number
current_number = current_number + 1

49
Example 5
 Program: Determine the direction of a numbered
NYC street, even is eastbound other westbound

Get street
If street is even Then
Display Eastbound
Else
Display Westbound
End If
Example 6
 Write an algorithm in pseudocode and draw a
flowchart for a program that will read the two
sides of a rectangle and calculate its area.
Example 7
 Write an algorithm that reads two values,
determines the largest value and prints the
largest value with an identifying message.
Example 8
 Write an algorithm in pseudocode and draw a
flowchart to convert the length in feet to
centimeter.

 Tip: 1 foot = 30.48cm


Example 9
 Write an algorithm using pseudocode and
flowchart to determine a student’s final grade
and indicate he or she passed or failed. The
final grade is calculated as the average of four
marks. If the final grade is below 40, print fail
else print pass.
Example 10
 Write an algorithm and draw a flowchart that
will calculate the roots of a quadratic equation
ax 2  bx  c 0
 Hint: d = sqrt ( b 2  4ac ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
Exercises: Algorithm &
Flowchart
1.) Create an algorithm and a flowchart that
will accept/read two numbers and then
display the bigger number.
Exercises: Algorithm &
Flowchart
2.) Create an algorithm and a flowchart that
will compute the area of a circle.
Exercises: Algorithm &
Flowchart
3.) Create an algorithm and a flowchart that
will compute the sum of two numbers. If
the sum is below or equal to twenty, two
numbers will be entered again. If the sum
is above 20, it will display the sum.
Lab Activity: Algorithm &
Flowchart
4) Create an algorithm and a flowchart that
will output the largest number among the
three numbers.
Assignment 1

1. Create an algorithm and a flowchart that will output


for g.c.d.
2. Create an algorithm and a flowchart that will output
the factorial of a given number.
3. Create an algorithm and a flowchart that will output
the Fibonacci series up to a given number.
4. Create an algorithm and a flowchart that will output
all the prime numbers between 2 numbers.

You might also like