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

3 - Lectures Note Week4

Uploaded by

eaina6933
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)
55 views

3 - Lectures Note Week4

Uploaded by

eaina6933
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/ 39

CSC120: Computer as a Problem-Solving

Tool
Topic 3:
Concept & Properties of Algorithm
Lecture 3 Week4:
Lecturer
Dr. Ufuoma C. Ogude

Department of Computer Sciences


University of Lagos
1
Outlines of the Course Wk2-
7
 Concept of Problem solving
 Introduction to programming
 Concept & properties of Algorithm
 Fundamentals of programming (1): variable/variable naming
convention/data types in Python
 Fundamentals of programming (2): operators and expressions
 Fundamentals of programming (3): control structures

2
Concept & Properties of Algorithms
 Concept & properties of Algorithm

 Role of Algorithm in problem solving

 Design of Algorithms

 Pseudocode

 Flowchart

3
Problem Solving
1. Understand the Problem

2. Formulate a Model

3. Develop an Algorithm

4. Write the Program

5. Test the Program

6. Evaluate the Solution


4
Design of Algorithms
A person must translate an algorithm into a computer
program.

5
Introduction
 In our day-to-day life we perform activities by following

certain sequence of steps.

 Examples of activities include


 getting ready for school,
 making breakfast,
 riding a bicycle,
 wearing a tie,
 solving a puzzle and so on.

 To complete each activity, we follow a sequence of steps. 6


Algorithm
 Every problem solution starts with a plan. That plan is

called an algorithm.

 An algorithm is a plan for solving a problem.


 Step by Step description of the method to solve a
problem.
 Effective procedure for solving a problem in finite
number of steps.
 Developing an algorithm is a step of program design.

7
Algorithm
 1st Definition:

 Sequence of steps that can be taken to solve a problem

 Better Definition:

 A precise sequence of a limited number of unambiguous,

executable steps that terminates in the form of a solution

 Instructions for solving a problem or subproblem in a

finite amount of time using a finite amount of data


8
Development Process
 Algorithm is part of the blueprint/plan for the computer
program
 Algorithm:
 “An effective procedure for solving a class of problems in a
finite number of steps.”
 Every algorithm should have the following 5 characteristic
feature:
 Input
 Output
 Definiteness
 Effectiveness
9
 Termination
Characteristics
 Characteristics of a good algorithm
 Precision — the steps are precisely stated or defined.
 Uniqueness — results of each step are uniquely defined
and only depend on the input and the result of the
preceding steps.
 Finiteness — the algorithm always stops after a finite
number of steps. It must terminate after a finite
number of steps.
 Input — the algorithm receives some input.
 Output — the algorithm produces some output.
10
Requirement
 While writing an algorithm, it is required to

clearly identify the following:

 The input to be taken from the user

 Processing or computation to be performed

to get the desired result

 The output desired by the user


11
NOTE
 Input Data
 Read: Read student name
 Get: Get student name

 Output Information
 Print: Print ‘Program Complete’
 Write: Write record to file
 Output: Output totalAmount
 Display: Display ‘Program Complete’

 Perform Arithmetic
 Add: Add num1 to num2
 Subtract: Subtract num1 from num2
 Multiply: Multiply num1 by num2
 Divide: Divide num1 by num2

12
NOTE
 Assign Values
 Initialise: Initialise totalPrice to zero
 Set: Set totalPrice to zero
 Store: Store zero in totalPrice
 Compare Values
 IF…THEN…ELSE
 IF num1 > num 2 THEN
ADD num1 to total
ELSE
ADD num2 to total
ENDIF
 Repeat Actions
 DOWHILE
 DOWHILE Num1 > Num2
ADD num1 to total
Multiply total by 3
Subtract Num2 by 3 13

ENDDO
Requirement
 Three Requirements
1. Sequence is:
a. Precise
b. Consists of a limited number of steps
2. Each step is:
a. Unambiguous (Explicit, Definite, Clear-cut, Clear)
b. Executable
3. The sequence of steps terminates in the form of
a solution
14
Example1 of Algorithm
 Compute the average marks of students obtained in a TEST of
"Computer Programming"
Human Language Specification: (In English)
 Read the no of students
 Read the marks of each students
 Sum the marks of all students as total marks.
 Divide the total marks by number of students.
 Report the result of division as average marks of students.
Pseudocode Specification:
 Input N
 Input N marks: m1, m2, m3,..... mN
 T = (m1+ m2 + m3 +... ... ....+ mN) / N
 A=T/N
 Output N. 15
Example2 of Algorithm
 Design an algorithm to add two numbers and display
the result
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP 16
Example3 of Algorithm
 Write an algorithm to find the square of a number.
Before developing the algorithm, let us first identify the
input, process and output:
 Input: Number whose square is required
 Process: Multiply the number by itself to get its square
 Output: Square of the number
 Algorithm to find square of a number.
Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square 17
 There are two common methods of representing an
algorithm. It may be expressed in either:
 flowchart (Graphical representation) and
 Pseudocode (Human language (e.g. English)).
 Either of the methods can be used to represent an
algorithm while keeping in mind the following:
 it showcases the logic of the problem solution,
excluding any implementation details
 it clearly reveals the flow of control during execution
of the program
18
Flowchart
 A flowchart is the graphical representation of the algorithm
for any problem.
 It is useful for developing and representing algorithms.
 Flow charts are drawn using certain special-purpose
symbols connected by arrows called flow lines.
 The symbols indicate the actions to be performed, and
flow lines indicate the order in which actions are to be
performed.
 Flow lines have arrows (direction of flow).

19
Reasons for Flowchart
Major reasons:
 is easier to read
 more closely follows a standard
 easier to understand

20
Flowchart Symbols
 Here is a chart for some of the common symbols used in
drawing flowcharts.

21
Flowchart Symbols

22
Flowchart Symbols

23
Example4 of Algorithm
 Write an algorithm and draw flowchart for finding
the sum of any two numbers.
 Algorithm
Step1: Start
Step2: Display “Enter two numbers”.
Step3: Read A and B
Step4: C= A+B
Step5: Display “C as sum of two numbers”
Step6: Stop 24
Flowchart

25
Average of two numbers

26
Guidelines for Developing Flowcharts
 These are some points to keep in mind while developing a
flowchart:
 Flowchart should have only one start and one stop
symbol
 On-page connectors are referenced using numbers
 Off-page connectors are referenced using alphabets
 General flow of processes is top to bottom or left to
right. Only one flow line should enter a decision symbol,
but two or three flow lines can leave the decision symbol.
 Arrows should not cross each other.
27
Pseudocode
 The word “pseudo” means “not real,” so “pseudocode” means “not
real code”.
 A Pseudocode is another way of representing an algorithm.
It is a non-formal language that helps programmers to develop
algorithm. It is a cross between human language and a
programming language.
 It is a detailed description of instructions that a computer must
follow in a particular order.
 It is intended for human reading and cannot be executed directly by
the computer.
 Not an actual programming language
 Not actually executed on computers
28

 No specific standard for writing a pseudocode exists.


Pseudocode
Why do you need it?
 Helps to “think out” a program before writing it
 May be converted easily into a corresponding
programming language code.

 Similar to a programming language, but not as rigid


 The method of expression most suitable for a given situation is used:
29
 At times, plain English
 At others, a programming language like syntax
Pseudocode Construct
 Following are some of the frequently used keywords
while writing pseudocode:
 INPUT
 COMPUTE
 PRINT
 INCREMENT
 DECREMENT
 IF/ELSE
 WHILE
 TRUE/FALSE

 The core of pseudocode is the ability to represent 6 programming


constructs (always written in uppercase): SEQUENCE, CASE,
WHILE, REPEAT-UNTIL, FOR, and IF-THEN-ELSE.
 These constructs are also called keywords and are used to describe
the control flow of the algorithm
30
Pseudocode Construct
1. SEQUENCE represents linear tasks sequentially performed

one after the other.

2. WHILE a loop with a condition at its beginning.

3. REPEAT-UNTIL a loop with a condition at the bottom.

4. FOR another way of looping.

5. IF-THEN-ELSE a conditional statement changing the flow

of the algorithm.

6. CASE the generalization form of IF-THEN-ELSE 31


Example3
Write an algorithm to display the sum
of two numbers entered by user, using
both pseudocode and flowchart.

32
Solution to Example3
Pseudocode for the sum of two
numbers will be:
input num1
input num2
COMPUTE Result = num1 +
num2
PRINT Result
33
Solution to Example3
Flowchart to display sum of two numbers

34
Benefits of Pseudocode
 It helps in representing the basic functionality of the

intended program.

 It provide the code first in a human readable language,

 It also helps the programmer safeguards against leaving out

any important step.

 Pseudocode helps to review the steps to confirm that the

proposed implementation is going to achieve the desire output

(for non-programmers). 35
 It shows the flow of events as represented in the flow chart.

 The events can flow in a sequence, or on branch based on a

decision or even repeat some part for a finite number of times.

 Components of an algorithm:
 Values and Variables
 Instruction
 Sequence (of instructions)
 Procedure (involving instructions)
 Selection (between instructions)
 Repetition (of instructions) 36

 Documentation (beside instructions)


Further Study
 Advantages and Disadvantages of the following:

 Flowchart
 Pseudocode

37
Next Lecture: Week5
 Fundamentals of programming (1):
variable/variable naming convention/data
types in Python

38
Fundamentals of Programming
(1)
 variable/variable naming convention/data types in Python

39

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy