Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
CS-1301
Lecture 2
Algorithms and flowcharts Department of Computer Science
College of Arts and Science
Lecturer : Mr. Asad Abd Elrashid
1
Content
2
Introduction.
Pseudocode.
Algorithm.
The Flowchart.
Control structure.
If structure.
Introduction
3
Computer programming can be divided into two
phases:
Problem solving phase
–Make an ordered sequence of steps that
solves a problem
– these sequence of steps is called an
algorithm
Implementation phase :
– implement using a programming language
Pseudocode
4
Artificial, informal language that helps us
develop algorithms
Similar to everyday English
Not actually executed on computers
Helps us “think out” a program before writing
it.
Algorithm
5
Step 1: input x,y
Step 2: sum x+y.
Step 3: print sum value.
The Flowchart
6
Graphical representation of an algorithm.
Drawn using certain special-purpose symbols connected by arrows called flow lines.
–A flowchart must have a start and stop
–A steps in a flowchart must connect. Can’t leave a step “hanging” with no connection.
Flow chart symbols
7
Example 1
8
Input two numbers.
Multiply your numbers together.
Output Multiplication result.
Cont..
9
Step 1: Input x,y.
Step 2 : z = x*y.
step 3 : print z.
Flow chart
10
Start
z = x*y
Enter x,y
Print z
End
Control Structures
11
Sequential execution :
Statements executed one after the other in the
order written.
All programs written in terms of three control
structures
Sequence structures: Built into C. Programs
executed sequentially by default.
Cont..
12
Selection structures:
C has three types: if, if…else, and switch.
Repetition structures:
C has three types: while, do…while and for.
Sequence structures
13
Start
z = a+b
Enter a,b
Print z
End
Selection structures:
14
Used to choose among alternative options of
actions.
Step 1: start
Step 2: if(a=0) then
Step 3: print a.
Step 4: stop
If structure
15
if
A=0
Print A
Y N
Print A
End
If else structure
16
Specifies an action to be performed both when
the condition is true and when it is false.
Challenge :
Think about any example of if else structure.
Example (1)
17
Step 1: Start.
Step 2: Input M1,M2,M3,M4
Step 3: GRADE (M1+M2+M3+M4)/4
Step 4: if (GRADE < 60) then
Print “FAIL”
else
Print “PASS”
endif
Cont..
18
“PASS”
START
Input
M1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
GRADE<60
“FAIL”
STOP
YN
Print “Fail”Print “Pass”
Example
19
Input two numbers.
Check which number is greater.
Output greater number.
Cont..
20
Step 1 : start.
Step 2 : input A and B.
Step 3 : if A is greater than B.
Step 4 : if “true” print A.
Step 5 : else print B.
Step 6 : stop.
Challenge
21
Change previous algorithm to flow chart?
Thank you for your attention
22
Lecturer : Aasd Abd Elrashid