0% found this document useful (0 votes)
23 views44 pages

8.0 Algorithms and Flow Charts

The document discusses algorithms and flowcharts. It begins by defining an algorithm as a finite sequence of unambiguous instructions that will solve a problem within a finite time and using a finite amount of data. It then discusses testing algorithms through desk checking, walk-throughs and inspections. The rest of the document discusses problem solving techniques like understanding the problem, devising a plan, carrying out the plan and reviewing. It also discusses concepts like divide and conquer. Finally, it discusses flowcharts as a way to represent algorithms graphically and provides examples of flowcharts.

Uploaded by

carlidaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views44 pages

8.0 Algorithms and Flow Charts

The document discusses algorithms and flowcharts. It begins by defining an algorithm as a finite sequence of unambiguous instructions that will solve a problem within a finite time and using a finite amount of data. It then discusses testing algorithms through desk checking, walk-throughs and inspections. The rest of the document discusses problem solving techniques like understanding the problem, devising a plan, carrying out the plan and reviewing. It also discusses concepts like divide and conquer. Finally, it discusses flowcharts as a way to represent algorithms graphically and provides examples of flowcharts.

Uploaded by

carlidaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Algorithm and Flowcharts

Introduction
 A typical programming task can be divided into two phases:

 Problem solving phase

 produce an ordered sequence of steps that describe solution of problem

 this sequence of steps is called an algorithm

 Implementation phase

 implement the program in some programming language

2 Algorithms and Flowcharts 12/15/2023


Introduction…
 Algorithm: “a finite sequence of unambiguous, executable steps

or instructions, which, if followed would ultimately terminate and


give the solution of the problem”.

 Algorithm: A set of unambiguous instructions for solving a

problem or sub-problem in a finite amount of time using a finite


amount of data

Why must instructions be unambiguous?

Why must time and data be finite?


3 Algorithms and Flowcharts 12/15/2023
Testing an Algorithm
Desk checking
 Working through a design at a desk with a pencil and paper

Walk-through
 Manual simulation of the design by team members, taking sample

data values and simulating the design using the sample data

Inspection
 One person (not the designer) reads the design (handed out in

advance) line by line while the others point out errors

4 Algorithms and Flowcharts 12/15/2023


Problem Solving

How do you solve problems?

Understand the problem

Devise a plan

Carry out the plan

Look back

5 Algorithms and Flowcharts 12/15/2023


Problem Solving…
 Analysis and Specification Phase

Analyze

Specify

 Algorithm Development Phase

Develop algorithm

Test algorithm

 Implementation Phase

Code algorithm

Test algorithm

 Maintenance Phase

Use

Maintain
6 Algorithms and Flowcharts 12/15/2023
Problem Solving…

7 Algorithms and Flowcharts 12/15/2023


Problem Solving…
Ask questions!

 What do I know about the problem?

 What is the information that I have to process in order to find

the solution?

 What does the solution look like?

 What sort of special cases exist?

 How will I recognize that I have found

the solution?
8 Algorithms and Flowcharts 12/15/2023
Problem Solving…
Ask questions! Never reinvent the wheel!
 Similar problems come up again and again in different guises

 A good programmer recognizes a task or subtask that has been

solved before and plugs in the solution

Can you think of two similar problems?

9 Algorithms and Flowcharts 12/15/2023


Problem Solving…
Divide and Conquer!

Break up a large problem into smaller units and solve each smaller
problem

 Applies the concept of abstraction

 The divide-and-conquer approach can be applied over and over

again until each subtask is manageable

10 Algorithms and Flowcharts 12/15/2023


Problem Solving…
 First produce a general algorithm

 Refine the algorithm successively to get step by step detailed

algorithm that is very close to a computer language (pseudocode)

11 Algorithms and Flowcharts 12/15/2023


Problem Solving…
 Example: 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.

Algorithm:
 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”

12 Algorithms and Flowcharts 12/15/2023


Problem Solving…

 Detailed Algorithm (Pseudocode)

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
13 Algorithms and Flowcharts 12/15/2023
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.
14 Algorithms and Flowcharts 12/15/2023
Flowchart…
A Flowchart
 shows logic of an algorithm

 emphasizes individual steps and their interconnections

 e.g. control flow from one action to the next

15 Algorithms and Flowcharts 12/15/2023


Flowchart Symbols
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

16 Algorithms and Flowcharts 12/15/2023


Example: Flowchart

17 Algorithms and Flowcharts 12/15/2023


Example 1
 Write an algorithm and draw a flowchart to convert the length in

feet to centimeter.

Algorithm:
 Input the length in feet (Lft)

 Calculate the length in cm (Lcm) by multiplying LFT with 30

 Print length in cm (LCM)

18 Algorithms and Flowcharts 12/15/2023


Example 1…

19 Algorithms and Flowcharts 12/15/2023


Example 2…
 Write an algorithm and draw a flowchart that will read the two

sides of a rectangle and calculate its area.

20 Algorithms and Flowcharts 12/15/2023


Example 2…

Algorithm
 Input the width (W) and Length (L) of a rectangle

 Calculate the area (A) by multiplying L with W

 Print A

21 Algorithms and Flowcharts 12/15/2023


Example 2…

22 Algorithms and Flowcharts 12/15/2023


Example 3
 Write an algorithm and draw a flowchart that will calculate the

roots of a quadratic equation

23 Algorithms and Flowcharts 12/15/2023


Example 3…
Algorithm:
 Input the coefficients (a, b, c) of the quadratic equation

 Calculate d

 Calculate x1

 Calculate x2

 Print x1 and x2

24 Algorithms and Flowcharts 12/15/2023


Example 3…

25 Algorithms and Flowcharts 12/15/2023


Example 4
 Write an algorithm that reads two values, determines the largest

value and prints the largest value with an identifying message.

26 Algorithms and Flowcharts 12/15/2023


Example 4…
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

27 Algorithms and Flowcharts 12/15/2023


Example 4…

28 Algorithms and Flowcharts 12/15/2023


Example 5
 Write an algorithm that reads three numbers and prints the value

of the largest number.

29 Algorithms and Flowcharts 12/15/2023


Example 5…
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2, N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX

30 Algorithms and Flowcharts 12/15/2023


Example 5…
 Flowchart: Draw the flowchart of the above Algorithm.

31 Algorithms and Flowcharts 12/15/2023


Example 6
 Write and algorithm and draw a flowchart to

a) read an employee name (NAME), overtime hours worked


(OVERTIME), hours absent (ABSENT) and

b) determine the bonus payment (PAYMENT).

32 Algorithms and Flowcharts 12/15/2023


Example 6…

33 Algorithms and Flowcharts 12/15/2023


Example 6…

34 Algorithms and Flowcharts 12/15/2023


Example 6…
 Flowchart: Draw the flowchart of the above algorithm?

35 Algorithms and Flowcharts 12/15/2023


Types of Errors
 Syntax Errors: Violation of syntactic rules in a Programming Language

generates syntax errors.


 Effect? Interpreter or Compiler finds it in Syntax Check Phase.

 Semantic Errors: Doing logical mistakes cause semantic errors in


Source code.
 Effect? Interpreters and Compilers cannot notice them, but on

execution, they cause unexpected results.


 Run-time Errors: Occur on program execution. Mostly caused by

invalid data entry or tries to use not existing resources.


 Effect? It occurs on run time and may crash the program execution
36 Algorithms and Flowcharts 12/15/2023
Assignment
1. Write the algorithm and pseudo-code for an ATM machine that
strictly allows only the following transactions to be carried out
by the user: check account balance, withdraw money, buy
airtime and deposit cash. Withdrawals and airtime purchases
require that there must be sufficient balance in the account. The
system should not allow any ATM transaction of more than KES
20,000. All transaction require PIN authentication and any three
consecutive wrong PIN inputs results in retention of the users
ATM card.

37 Algorithms and Flowcharts 12/15/2023


Assignment…
2. Using the below tax table for monthly income (slide 39) and
the example PAYE computation in slide number 40 write a C
program that accepts monthly gross salary as the input and
computes PAYE to be deducted. The program should accept
any amount greater than zero. More information can also be
obtained from the emailed PAYE guide.
 Use the if, elseif, and if-else statements where appropriate.

 If the total tax is less than Kshs. 1,162 then the tax relief

should not apply.


38 Your output
Algorithms should look like the one shown in slide number
and Flowcharts 12/15/2023
39 Algorithms and Flowcharts 12/15/2023
40 Algorithms and Flowcharts 12/15/2023
41 Algorithms and Flowcharts 12/15/2023
Assignment…
3. Develop an algorithm for a vending machine controller that sells
three products: coke, castle and mineral water at a cost of Kshs.
50.00, Kshs. 30.00 and Kshs. 15.00 respectively. The machine
accepts only Kshs. 5.00, 10.00, 20.00, and 40.00 coins as inputs.
Change is only available in multiples of Kshs. 5.00. The
machine operates as follows: the user inserts sufficient amount
of money and presses the product button. The vending machine
responds by giving out the selected product and change when
due.

42 Algorithms and Flowcharts 12/15/2023


Lab Exercise
1. Write a C/C++ program that adds two vectors of the same size.
For example: (1,2,3,4,5,6,7,8,9) plus (9,8,7,6,5,4,3,2,1).

43 Algorithms and Flowcharts 12/15/2023


END

44 Algorithms and Flowcharts 12/15/2023

You might also like