Importance of Using Flowcharts
One of the most important benefits of using the flowcharts before writing any
program is the following things:
1- Gives an integrated image of the steps required to solve the problem in the
mind of the programmer so that it can fully inform all parts of the problem from
the beginning to the end.
2- Help the programmer to diagnose the errors that occur in the program,
especially the logical mistakes whose discovery depends on the logical
sequence of steps to solve the problem.
3- Makes it easier for the programmer to make any adjustments in any part of
the problem quickly and without having to re-examine the problem.
4- Makes it easier for the programmer to follow the parts of the program that
have a lot of possibilities and branched.
5- The flowchart used in the design of solutions to some problems are a
reference in resolving other similar problems and a key to resolving new
problems related to old problems resolved.
1
Flowcharts Types
In general, it can be said that there are two main types of flowcharts:
(a) System Flowcharts
This type of flowcharts is used when designing engineering devices in factories
and others, which use self-control systems such as buoys in water tank, traffic
lights, and pressure control devices and temperatures in oil distillation towers so
that the flowcharts here are considered as the complete blueprint that shows the
order, relationship and function of each stage before and after, within the
framework of the integrated system.
The role of these flowcharts can be summarized as follow:
1- It is easy to detect any malfunction in the whole system just by looking at.
This is led to simply maintenance process and at the lowest cost.
2- Facilitates future modification to the system program at any location.
3- Explain the details of the data to be entered into the system.
4- Explain the details of the types of expected or required results from the
program prepared for the system.
5- To indicated how to link the system to the rest of system in the designated
organization.
(b) Program Flowcharts
This type of flowcharts is used to illustrate the main steps that are being
developed to resolve a problem in the form of idiomatic drawings showing the
logical relationship between the other steps of the solution and the location and
function of each of them within the framework of a comprehensive solution to
the problem.
There are four types from program flowcharts :
1. Simple Sequential Flowcharts
2. Branched Flowcharts
3.Simple-Loop Flowcharts
4. Multi-Loop Flowcharts
i) Simple Sequential Flowcharts
The solution steps for this type of flowchart are arranged in a straight series
from the beginning of the program to the end so that it is free of branches and
loops.
2
Example 1: Draw a flowchart to find the area and perimeter of circle have
known radius ( R ).
Start
Read R
PI=3.14
A=PI* R^2
C=2*PI*R
Print R,
A and C
End
Example 2: Draw a flowchart to find the values of variables A, B, and C from
the following equations.
1. A=X2 +2Y
2. B=2X-3A
3. C=A2 +XB
3
Start
Read
X, Y
A=X^2+2*Y
B=2*X-3*A
C=A^2+X*B
Print X,Y,
A,B, and C
End
ii) Branched Flowcharts
This type of flowcharts are used when there is a need to make a decision or a
trade-off between two or more choices. There are two types of resolution as
shown below:
Less than 0 Greater than 0
X? No Condition Yes
Equal 0
Three branches Two branches decision
decision
4
In general, the branch flowcharts can take one of the following two images:
5
NO
YES YES
Condition ? Condition ?
NO Event a Event b Event a
Event b
Fig.(2) Fig.(1)
Fig.(1) shows that if the answer of condition is (Yes) the next event in the
execution is event (a), but if the answer is (No), the next event is event (b) either
in Fig.(2) if the answer of condition is (Yes), the next event in the execution is
event (a) and then followed by event (b), but if the answer of condition is (No),
the next event will be event (b) directly.
Example (3):
START
Draw a flowchart to find the F(x) ?
𝑥 𝑥≥0
𝐹(𝑥) = {
𝑥 𝑥<0
READ
NO YES
X ≥0
F(X) = -X F(x)=x
PRINT
X and F(X)
11
Example (4): Draw a flowchart to find the value of W from the following equations,
note that the value of variable X is known ?
𝑋+1 𝑋>0
𝑊 = { sin(𝑋) + 5 𝑋=
0 2𝑋 − 1 𝑋<0
iii) Simple-Loop Flowcharts
This type of flowcharts we need to return a process or set of processes in the program
a specific or unspecified number of times, and the overall form of these flowcharts is
as follows:
The event Event a
is
repeated
NO every
Condition ? cycle until
NO
the Condition ?
answer is
yes
YES
11
YES
The event
Event a is repeated
every
cycle as Start
long as the
answer is Read X
yes
X?
W=2X-1 W=sin(X)+5 W=X+1
Print W
End
12
And this flowchart is called a single-loop flowcharts because it uses a single rotation
loop and is sometimes called a simple-loop flowcharts.
Example (5): The general Traffic Directorate in Iraq is submitting a project to obtain
online driving permits, provided that the applicant not under 18 years of age . Draw
the flowchart for this project?
START
Take person
NO Age ≥18
YES
Grant him License
YES
More people?
NO
END
iv) Multi-Loop Flowcharts
In this type of flowcharts the rotations are within each other so that do not intersect.
Example (6): A merchant wants to cut off a set of cloth sized more than 5 meters
long into small pieces of which are 5 meters long . Draw the flowchart for this project
?
13
START
Take a piece
Cut off 5m length
YES Is
Remainder
>5
NO
YES
More pieces?
NO
END
In this example we observed the presence of two loops one internal and called internal
loop and the other is external loop and the functioning of these two loops are
coordinated so that the priority of implementation is for internal loop.
14
The Idiomatic Form For Counters
i=1,N,∆ (1) i=1 (1)
The process
that will be
(2)
i=i+∆ returned
The process
that will be
returned
(4) (2)
i (3) i≥N
(3)
v)
Example (7): Draw a flowchart for program print the first ten natural numbers ?
15
Start Start
i=1,10 Put i=1
Print i No i≤10 Yes
i Print i
End
End i=i+1
16
Example (8): Draw a flowchart to read the number N and find its factorial
? N!=N(N-1)(N-2)(N-3)…………….
Example (9): Draw a flowchart to find the sum of the limits in the following amount ?
5
𝑆 = ∑ 𝑎𝑖
𝑖=1
17
Start
S=0
i=1,5
Read a
S=S+a
Print S
End
Example (10): Draw a flowchart to print the first thirty limits of the following series ?
1, 3, 5, ………………………….
18
Start Start
N=1 N=1
i=1
i=1,30 N=N+2
Print N
Print N
i=i+1
Yes
N=N+2 i<30
No
i
End
End
Example (11): Draw a flowchart to print the first two hundred limit of the
engineering series that it start with 5 so that the change rate is 3 ?
Start
N=5
i=1
N=N+3
Print N
i=i+1
Yes
i<200
No
17 End
Algorithms
Definition of Algorithm:
An algorithm is a finite, well-defined sequence of steps or instructions designed to
perform a specific task or solve a particular problem. In computer science, algorithms
are fundamental to programming, as they provide the logic and structure that drives
software applications and systems. An algorithm takes inputs, processes them
according to the defined steps, and produces an output. The efficiency and
effectiveness of an algorithm can vary depending on how the steps are organized and
the complexity of the task. Algorithms are not only used in computing but are also
applicable in everyday life, such as in cooking recipes, navigation directions, or even in
decision-making processes.
In fact, if you really understand the algorithm, you will be able to understand
any programming language you want because you will be able to control and
manipulate the code the way you want.
Algorithms are not programming language, but are the method of analysis and
thinking that you have to follow in order to write the code correctly and
therefore the algorithm can be defined as a set of steps or instruction arranged to
perform a calculation or logical process sequentially and systematically.
There are two way to represented the algorithm (Pseudo code and flowchart) as it will
be explained before.
OR
An algorithm is a set of commands that must be followed for a computer to perform
calculations or other problem-solving operations. According to its formal definition,
an algorithm is a finite set of instructions carried out in a specific order to perform a
particular task. It is not the entire program or code; it is simple logic to a problem
represented as an informal description in the form of a flowchart or pseudocode.
Problem: A problem can be defined as a real-world problem or real-world
instance problem for which you need to develop a program or set of
1
0
instructions. An algorithm is a set of instructions.
Algorithm: An algorithm is defined as a step-by-step process that will be
designed for a problem.
Input: After designing an algorithm, the algorithm is given the necessary and
desired inputs.
Processing unit: The input will be passed to the processing unit, producing the
desired output.
Output: The outcome or result of the program is referred to as the output
Characteristics of an Algorithm
An algorithm is a methodical process used to solve a task or solve a problem. Several
important factors impact an algorithm's effectiveness:
1. Finiteness
An algorithm must always have a finite number of steps before it ends. When the
operation is finished, it must have a defined endpoint or output and not enter an
endless loop.
2. Definiteness
An algorithm needs to have exact definitions for each step. Clear and straightforward
directions ensure that every step is understood and can be taken easily.
3. Input
An algorithm requires one or more inputs. The values that are first supplied to the
algorithm before its processing are known as inputs. These inputs come from a
predetermined range of acceptable values.
4. Output
One or more outputs must be produced by an algorithm. The output is the outcome of
the algorithm after every step has been completed. The relationship between the input
and the result should be clear.
1
1
5. Effectiveness
An algorithm's stages must be sufficiently straightforward to be carried out in a finite
time utilizing fundamental operations. With the resources at hand, every operation in
the algorithm should be doable and practicable.
6. Generality
Rather than being limited to a single particular case, an algorithm should be able to
solve a group of issues. It should offer a generic fix that manages a variety of inputs
inside a predetermined range or domain.
Advantage and Disadvantages of Algorithms
Advantages of Algorithms:
Efficiency: Algorithms streamline processes, leading to faster and more
optimized solutions.
Reproducibility: They yield consistent results when provided with the same
inputs.
Problem-solving: Algorithms offer systematic approaches to tackle complex
problems effectively.
Scalability: Many algorithms can handle larger datasets and scale with
increasing input sizes.
Automation: They enable automation of tasks, reducing the need for manual
intervention.
Disadvantages of Algorithms:
Complexity: Developing sophisticated algorithms can be challenging and time-
consuming.
Limitations: Some problems may not have efficient algorithms, leading to
suboptimal solutions.
Resource Intensive: Certain algorithms may require significant computational
resources.
Inaccuracy: Inappropriate algorithm design or implementation can result in
incorrect outputs.
Maintenance: As technology evolves, algorithms may require updates to stay
relevant and effective.
1
2
1. Getting specified output is essential after algorithm is executed.
2. One will get output only if algorithm stops after finite time.
3. Activities in an algorithm to be clearly defined in other words for it to
be unambiguous. Before writing an algorithm for a problem, one should find
out:
• what is/are the inputs to the algorithm
• what is/are expected output after running the algorithm
While writing algorithms we will use following
symbol for different operations:
‘+’ for Addition
‘-’ for Subtraction
‘*’ for Multiplication
’ ‘/’ for Division and
‘ =’ for assignment. For example A X*3 means A
will have a value of X*3.
Examples of Algorithm:
Problem 1 Find the area of a Circle of radius r.
Inputs: Radius r of the Circle and PI=3.14.
Expected output: Area of the Circle
Algorithm:
Step1: start
Step2: Read\input the Radius r of the Circle
Step3: Input PI=3.14
Step4: Calculate Area =PI*r*r
Step4: Print Area
Step5: End
1
3
Problem 2: Write an algorithm to read two numbers and find their sum.
Inputs: First num1. Second num2.
Expected output: Sum of the two numbers.
Algorithm:
Step1: Start
Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Calculate Sum =num1+num2
Step5: Print Sum
Step6: End
Problem 3: Write an algorithm to find the average of temperature T1, T2, and
T3.
Inputs: The temperature T1, T2, And T3.
Expected output: The average of three temperature.
Step1: Start
Step2: Read the values f temperature T1, T2, and T3..
Step3: Calculate the average=(T1+T2+T3)/3
Step4: Print the average
Step5: End
Problem 4: Convert temperature Fahrenheit to Celsius
Inputs to the algorithm:
Temperature in Fahrenheit
Expected output:
Temperature in Celsius
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C =5/9*(F32)
Step 4: Print Temperature in Celsius: C
Step5: End
Type of Algorithms
There are three types of control structure Algorithms:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
1
4
i) Sequence Algorithm
These three control structures are sufficient for all purposes. The sequence is
exemplified by sequence of statements place one after the other – the one above
or before another gets executed first. In flowcharts, sequence of statements is
usually contained in the rectangular process box.
ii) Branching (Selection) Algorithm
The branch refers to a binary decision based on some condition. If the condition is
true, one of the two branches is explored; if the condition is false, the other
alternative is taken. This is usually represented by the „if-then‟ construct in pseudo-
codes and programs. In flowcharts, this is represented by the diamond-shaped
decision box. This structure is also known as the selection structure.
Problem 1: write algorithm to find the greater number between two numbers
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem2: write algorithm to find the result of equation:
Step1: Start
Step2: Read/input x
Step3: If X Less than zero then F=-X
Step4: if X greater than or equal zero then F=X
Step5: Print F
Step6: End
Problem3: A algorithm to find the largest value of any three numbers.
Step1: Start
Step2: Read/input A,B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5:If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End
iii) Loop (Repetition) Algorithm
The loop allows a statement or a sequence of statements to be repeatedly executed
based on some loop condition. It is represented by the “while” and
1
5
“for” constructs in most programming languages, for unbounded loops and bounded
loops respectively. A trip around the loop is known as iteration. You must ensure that
the condition for the termination of the looping must be satisfied after some finite
number of iterations, otherwise it ends up as an infinite loop, a common mistake
made by inexperienced programmers. The loop is also known as the repetition
structure.
1
6
Problem 1: Write An algorithm to calculate even numbers between 0 and 99
1. Start
2. I=0
3. Print I in standard output
4. I = I+2
5. If (I <=98) then go to line 3
6. End
Note:
(Unbounded loops refer to those whose number of iterations depends on the
eventuality that the termination condition is satisfied; bounded loops refer to those
whose number of iterations is known before-hand.)
Problem2: Design an algorithm which gets a natural value, n,as its input and
calculates odd numbers equal or less than n. Then write them in the standard
output:
1. Start
2. Read n
3. I ← 1
4. Write I
5. I ← I + 2
6. If ( I <= n) then go to line 4
7. End
Problem3: Design an algorithm which generates even numbers between 1000 and
2000 and then prints them in the standard output. It should also print total sum:
1. Start
2. I ← 1000 and S ← 0
3. Write I
4. S ← S + I
5. I ← I + 2
6. If (I <= 2000) then go to line 3 else go to line 7
7. Write S
8. End
Problem4: Design an algorithm with a natural number, n, as its input which
calculates the following formula and writes the result in the standard output:
S = ½ + ¼ + … +1/n
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6. If (I <= n) then go to line 4 else write S in standard output
7. End
Combining the use of these control structures, for example, a loop within a loop
(nested loops), a branch within another branch (nested if), a branch within a loop,
a loop within a branch, and so forth, is not uncommon. Complex algorithms may
have more complicated logic structure and deep level of nesting, in which case it
is best to demarcate parts of the algorithm as separate smaller modules. Beginners
must train themselves to be proficient in using and combining control structures
appropriately, and go through the trouble of tracing through the algorithm before
they convert it into code.