Sei sulla pagina 1di 18

Applied Programming

LO1
HCT ACADEMIC FORUM FALL 2017

Algorithms & Flow Charts

Dr. Hussein Abdul-Rahman


Fujairah Campuses
1
Algorithms and Flowcharts
An algorithm is a step-by-step procedure to solve a given
problem
A flowchart is a graphical representation of an algorithm showing
the steps involved.
Basic Flowchart Symbols

TERMINATOR
(START / STOP) DECISION DOCUMENTATION
(PRINT-OUT)

ON-PAGE CONNECTOR

PREPARATION INPUT / OUTPUT

OFF-PAGE CONNECTOR

PROCESS PREDEFINED PROCESS or FUNCTION DIRECTION (FLOW)


Three Basic Flow Charting Constructs
I. SEQUENCE II. SELECTION III. REPETITION

TRUE FALSE

FALSE

TRUE

4
Flowchart Examples

EXERCISES: SEQUENCE

Draw a flowchart that will ask the user to input two numbers and
will print the sum and product of these two numbers:

SAMPLE DISPLAY:

Input two numbers and then press <ENTER> key: 2.5 6


Sum=8.5 Product=15

5
START

A=0
B=0
Sum = 0
Prod = 0

Input A, B

Sum = A + B
Prod = A * B

Print Sum,
Prod

STOP

6
EXERCISES: SEQUENCE

Draw a flowchart of an algorithm that will ask the user to input a


temperature in Fahrenheit which is then converted to Celsius and
printed:

SAMPLE DISPLAY:

Enter a temperature in Fahrenheit to be converted: 32


Temperature in C = 0

7
START

F=0
C=0

Input F

C = ( F – 32 ) * (
5/9 )

Print C

STOP

8
EXERCISES: IF-ELSE

Draw a flowchart that will ask the user to input the name and
grade of a student. The algorithm will then print the name and
grade of the student plus whether they passed or failed:

grade < 60  “Fail”


grade >= 60  “Pass”

SAMPLE DISPLAY:

Input name and score and press <ENTER> key: John 66


John 66 Pass

9
START

Name = “ “
Grade = 0
Rem = “ “

Input Name, Grade

FALSE Grade >= TRUE


60

Rem = “Fail” Rem = “Pass”

Print Name, Grade,


Rem

STOP

10
EXERCISES: IF-ELSE

Draw a flowchart that will ask the user to input the name and
score of an employee. The algorithm will then print the name and
score of the employee as well as a remark about their
performance as follows:

[4.5 to 5.0]  “Outstanding”


[4.0 to 4.5)  “Very Satisfactory”
[3.5 to 4.0)  “Satisfactory”
[3.0 to 3.5)  “Needs Improvement”
Less than 3  “Poor”

SAMPLE DISPLAY:

Input name and score and press <ENTER> key: James 3.2
James 3.2 Needs Improvement
11
START

Name = “ “
Score = 0
Rem = “ “

Input Name, Score

TRUE FALSE
Score < 3

Rem = “Poor” TRUE Score < FALSE


3.5

Rem = “Needs
Improvement” TRUE FALSE
Score < 4

Rem =
“Satisfactory” TRUE Score < FALSE
4.5

Rem = “Very Rem =


Satisfactory” “Outstanding”

Print Name, Score,


Rem

STOP

12
EXERCISES: LOOPS

Draw a flowchart that will print the numbers from 1 to 10.

SAMPLE DISPLAY:
1 2 3 4 5 6 7 8 9 10

13
Solution 2
Solution 1
START
START

X=0
X=1

FALSE
FALSE X < 10 STOP
X <= 10 STOP

TRUE
TRUE

X=X+1
Print X

Print X
X=X+1

14
EXERCISES: LOOPS

Draw a flowchart that will print the area of a circle when the radius
r takes values between 1 and 5. The area of a circle is 𝜋𝑟 2

SAMPLE DISPLAY:
1 3.1416
2 12.5664
3 28.2744
4 50.2656
5 78.54

15
Solution 3
Solution 2
Solution 1 STAR
STAR T
STAR T
T Pi =
Pi = 3.1416
Pi = 3.1416 R=0
3.1416 R=1 A=0
R=1 A=0
A=0

R=R+1
R <= FALSE
A = Pi * R * 5
R
TRUE R <= FALSE
A = Pi * R * 5
Print R,
A R TRUE

TRUE A = Pi * R *
Print R, R
R=R+1 A

Print R,
R=R+1 A
R <=
5
FALSE
STOP
STOP
STOP

16
No Exponent in C
Remember that in C language there is no symbol for calculating the
power or exponent.
If you need to calculate power, use a loop to multiply a number as
many times as necessary.

17
Introduction to Dev-C++

• Preprocessor

• Compiler

18

Potrebbero piacerti anche