0% found this document useful (0 votes)
17 views25 pages

Lec3 - Algorithms and Flowcharts

Uploaded by

mannanakbar52
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views25 pages

Lec3 - Algorithms and Flowcharts

Uploaded by

mannanakbar52
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Introduction to Computer Programming

Programming Fundamentals
L EC T URE 3 (A LGORITHMS A N D FLOWC HARTS)
BY
P ROF. D R. M . FA RHA N SHA FI Q U E
Program development life cycle
Step 1: Program
Specification

Step 6: Program Step 2: Program


maintenance design

Step 5: Program Step 3: Program


documentation code

Step 4: Program
test

2
Phase 1: Problem Specification (Analysis)
• The problem is defined to obtain a clear understanding of the problem requirements.
• It is important to do the following before proposing a solution
a. Analyze the problem thoroughly
b. Understand the requirement of the problem required solution
c. Divide the problem into sub-problems if the problem is complex
• The most important and major requirement that needs to be specified during problem definition
(analysis) is:
a. Input (I)
b. Process (P)
c. Output (O)
• However, to get a complete problem specification the following questions should be asked during problem
definition (analysis)
a. What is the input data?
b. What is the output data (desired results)?
c. What formula should be used for computing the results?
d. What other assumptions or constrains can be made?
e. What is the expected output screen?

3
Phase 2: Program design (Algorithm)
a. A step by step problem solving procedure
b. A sequence of instruction that tell how to solve a particular problem
c. A set of instruction for solving a problem especially on computer
d. A computable set of steps to achieve a desired result

4
Algorithm Representation
• Almost every program involves the steps of input, processing and output
• Therefore graphical representation are needed to separate these three steps
• An algorithm can be written or described or represent using several tools:
• Pseudo code
Using English like phrase to describe the processing process. It is not standardized since every
programmer has his or her own way of planning the algorithm
• Flowchart
Use standardized symbol to show the steps the computer needs to take to accomplish the
program’s objective, Because flow charts are cumbersome to revise, they are not used by
professional programmers often, pseudo code on the other has more acceptance.

5
Problem Design (Pseudocode)
• 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.

6
Pseudo Code Style

7
Pseudo code example 1
IPO Table

8
Pseudocode (Example 2)
Write a an algorithm in the form of pseudocode 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: Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Input a set of 4 marks
Step 2: GRADE ← (M1+M2+M3+M4)/4
Calculate their average by summing and dividing by 4
Step 3: if (GRADE < 50) then
if average is below 50 Print “FAIL”
Print “FAIL” else
Print “PASS”
else
Print “PASS”

9
Flowcharts
• Flowcharts is a graph used to represent a step by step solution
using symbols which represent tasks.

• The symbols used consist of geometrical shapes that are connected


by flow lines.
• Control flow from one action to the next (one symbol to next)

• It is an alternative to pseudocoding; whereas a pseudocode


description is verbal, a flowchart is graphical in nature.

10
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.

11
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.

12
Example 1 Flow chart

IPO Table

13
Flow chart structures

14
Sequence Structure
•With sequence structure, an action or event is performed in order, one after another
•A sequence can contain any number of events but there is no chance to branch off and skip
any of the events
•Once you start a series of events in a sequence, you must continue step by step until the
sequence ends

15
Sequence example

16
Sequence Example (cont.)
Step 2: Program design (Identification of input, process and output)

17
Sequence Example (Cont.)

18
Flow chart designs (Sequence)

19
Example 2 (Flow chart)
Write an algorithm in the form of flow chart 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.

START

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

Print Print
Pass Fail

STOP

20
Example 3
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Pseudocode:
START
Input the length in feet (Lft)
Input
Calculate the length in cm (Lcm) by multiplying LFT with 30 Lft

Print length in cm (LCM)


Algorithm Lcm ← Lft x 30

Step 1: Input Lft


Output
Lcm
Step 2: Lcm ← Lft x 30
Step 3: Print Lcm
STOP

21
Flowchart – example 4
Take the date of birth from the user Calculate the age of user and print senior citizen if the age is more than 60
and display the age
Begin
Begin

Read age
Read birth date

YES Age > 60? NO


Calculate
Age = current year – birth date

print “Senior Citizen” print “Young”


Display
age

End End

22
Flowchart – example 5
Explain how this algorithm works

Begin

sum = 0
current_number = 1

NO
current_number <= 10? print sum

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

23
Class Exercise
For each of the following problems analyze it using IPO table and then write the algorithm in the
form of pseudo code and flowchart
Problem 1: Calculate the area of a circle
Problem 2: Calculate the temperature in Celsius, temperature in Fahrenheit will be entered the
formula is Temp in Celsius = 5/9 (Temp in Fahrenheit -32)
Problem 3: Create an algorithm and a flowchart that will accept/read two numbers and then
display the bigger number
Problem 4: 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.
Problem 5: Create an algorithm and a flowchart that will output the largest number among the
three numbers.

24
Assignment 1
1. Create an algorithm and a flowchart that will output the G.C.D of two input numbers.
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
two entered numbers.
5. 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

25

You might also like