FLOWCHART
PSEUDO CODE
Flow Chart
Some of the symbols which are used in flowchart are:
Start & Stop Output &
Input
Process
Decision Box
Flow of Data
2
3
In an algorithm, we use two operations Read() and Write() to take a data from the user entered using
an input like the keyboard and to display some data on the output like the screen. Consider x as
number.
Read(x): the Read operation takes some value entered by the user on the keyboard and puts the
value in the variable x that is stored in memory.
Write(“Message”): the Write operation displays Message on the screen. Sometimes PRINT is also
used.
Write(x): the Write operation displays the value of x on the screen.
Write(“Message”, x): the Write operation displays Message followed by the value of x.
Example: int x = 7;
Write(x, “is a positive number.”); => 7 is a positive number.
4
5
6
BEGIN
int temp;
WRITE(“Enter the current temperature”);
READ(temp);
IF (temp < 0) THEN
WRITE(“The temperature ”, temp, “ is below zero. It is cold”);
temp = temp + 5;
ELSE IF (temp >40) THEN
WRITE(“The temperature ”, temp, “ is over forty. It is very hot”);
temp = temp - 5;
ELSE IF (temp >=20 AND temp <=25) THEN
WRITE(“Warm temperature, do not change!”);
ENDIF
ENDIF
ENDIF
END
7
8
Example 1: addition 2 numbers
Flow Chart
START
READ num1,num2
Sum = num1+ num2
WRITE sum
END
9
Example 2
Flow chart
Max between 2 numbers
START
READ n1,n2
yes No
n1>n2
WRITE n1 WRITE n2
END
10
Example 3: temperature
Flow chart
Freezing degree
START
READ temp
yes No
Temp<=0
WRITE "freezing
start" WRITE " above
freezing"
END
11
Example 4: odd or even numbers
Flow chart
Nb%2 allows to compute division remainder
START
READ nb
yes No
nb%2 ==0
WRITE "EVEN" WRITE "ODD"
END
12
Example 5: rectangle
Flow chart
Perimeter and area of a rectangle
START
READ length,width
Perimeter =2*(length+width)
area =length*width
WRITE Perimeter
WRITE area
13
END
Example 6
Flow chart
Area of a circle
START
READ radius
area =3.14*radius*radius
WRITE area
END
14
Exercise
Write an algorithm/FLOWchart to read the price (without the tax)
and quantity of an item and then calculates and displays the amount
of the bill knowing that the VAT rate is 10%.
15
1. BEGIN
2. float price, totalPrice;
3. int Quantity;
4. WRITE(“PRICE?”);
5. READ (Price)
6. READ(Quantity);
7. totalPrice = (Price + Price *0.1) * Quantity;
8. WRITE(“ Total=“, totalPrice);
9. END
16
2.2 Algorithms
Computing problems
Solved by executing a series of actions in a specific order
Algorithm is a procedure determining
Actions to be executed
Order to be executed
Example: recipe
Program control
Specifies the order in which statements are executed
17
2.3 Pseudocode
Pseudocode
Artificial, informal language used to develop algorithms
Similar to everyday English
Not executed on computers
Used to think out program before coding
Easy to convert into C program
Only executable statements
No need to declare variables
18
Building a Program
Whatever type of problem needs to be solved, a careful thought out plan of attack, called
an algorithm, is needed before a computer solution can be determined.
1) Developing the algorithm.
2) Writing the program.
3) Documenting the program.
4) Testing and debugging the program.
19
Building a Program
2) Writing the Program
If analysis and planning have been thoroughly done, translating the plan
into a programming language should be a quick and easy task.
3) Documenting the Program
During both the algorithm development and program writing stages,
explanations called documentation are added to the code.
Helps users as well as programmers understand the exact processes to be
performed.
23
Building a Program
4) Testing and Debugging the Program.
The program must be free of syntax errors.
The program must be free of logic errors.
The program must be reliable. (produces correct results)
The program must be robust. (able to detect execution errors)
Alpha testing: Testing within the company.
Beta testing: Testing under a wider set of conditions using
“sophisticated” users from outside the company.
24
The evolution of programming paradigms
25
Algorithm
a process or set of rules to be followed in calculations or other
problem-solving operations, especially by a computer.
26
Algorithm
In your algorithm (or program), you can use different types of statements.
There are 3 categories of control structures:
1- Sequence of statements
2- Selection
3- Repetition
27
Algorithm: Sequence
A compound statement (or a block) is a sequence of statements
ordered in a way to give a solution: e.g.
S1;
S2;
S3;
is a sequence of 3 statements
The statements that have a sequential control:
1- INPUT/OUPUT statements
2- Assignment statement
28
Algorithm: Sequence
INPUT/OUPUT statements
INPUT statement (in pseudo code):
Use READ statement to input data into variables from the standard input
device (e.g. a keyboard).
29
Algorithm: Sequence
Syntax: READ List of variables
where, List of variables contains one or more variables
e.g. READ x;
READ a, b;
The semantics (execution) of this statement:
You can enter the values you want for the variables in the
statement from the keyboard and the computer will assign these
values into the variables (stores them in memory).
30
Algorithm: Sequence
The WRITE statement has many uses.
Use WRITE statement to output
- The values of variables stored in memory.
- A message (i.e. a string of characters).
- The value of an expression.
31
Algorithm: Sequence
Syntax:
1- WRITE List of variables
where, List of variables contains one or more variables
e.g. WRITE x;
WRITE a, b;
The semantics (execution) of this statement:
This statement allows the computer to access the locations of the variables
mentioned in the statement and displays their contents on an output device
(e.g. a screen).
32
Algorithm: Sequence
2- WRITE message;
where message may by any string of
characters enclosed with double quotas.
e.g. WRITE “Enter 3 values”;
33
Algorithm: Sequence
The semantics (execution) of this statement:
This statement will display the message on the screen.
3- WRITE expression;
where expression is any arithmetic expression
e.g. WRITE 3 + 6;
WRITE x – y;
The semantics (execution) of this statement:
First, the expression is evaluated, then the result will be displayed on
the screen. For the first example, it will display 9.
34
Algorithm: Sequence
NOTE
You can mix between the different types of the WRITE statements.
e.g.
WRITE “Length = “ , length; è ex: Length= 15
The semantics (execution) of this statement:
This statement will display the message
Length = on the screen and on the same line it will display the value of the
variable length.
35
Assignment Statement
Storing a new value in a memory location is called assignment.
We use the operator ß as assignment operator.
Syntax:
Variable ç Expression;
The semantics (execution) of this statement:
1- The Expression on the right of ß is evaluated
2- The result of the expression is assigned to the variable on the left of
ß.
e.g.
X ß 10 ; (This means that X has 10 now )
Y ß X + 5; (This means that Y has 11 now, if X is 6)
36
Assignment Statement
NOTE:
The right hand side (RHS) of the assignment statement should be of the
same data type of the left hand side (LHS).
e.g.
1- T ß true;
This will be correct if T is of Boolean type (True or False).
2- A ß x + y * 2;
This will be correct if A has a numeric data type (e.g. integer, or real) and
the value of the expression on (RHS) has the same numeric data type.
37
Assignment Statement
n How to execute a statement like X ß X + 1; ?
Suppose we have:
X ß 5;
Then to execute X ß X + 1, we proceed as follows:
X Xß5
5 6
XßX+1
38
Assignment Statement
n Dereferencing:
If we want to copy a value from one memory location (say, X) into
another location (say,Y), we say that we dereference a variable.
e.g.
X ß 5;
Y ß 10;
X ßY ; // now X has the value 10
X Y
5 10 10
39
Example 1
Write an algorithm to Compute and print the
summation of two numbers.
First, we have to analyze the problem to understand what
is the input, output of the problem, and which formula to
use to solve the problem (if any).
40
Example 1
1- Analysis stage:
Problem Input:
- num1
- num2
Problem Output:
- summation of two numbers
Formula:
sum=num1+num2
41
Example 1
2- Algorithm Design
We write the algorithm by using the pseudo code
ALGORITHM Summation
BEGIN Output:
1. WRITE “enter 2 nbers: “ ; Enter 2 nbers: 100 5
Sum=105
2. READ num1, num2;
3. sumß num1+ num2;
4. WRITE “sum=“ ,sum;
END
42
Example 1
3- Testing the algorithm
We give a sample data to see whether the algorithm solves the problem
correctly or not.
To give a sample data, proceed as follows:
1- Prepare a table to contain all variables of the algorithm.
2- Give any data you like as input.
3- Calculate any formula in the algorithm using these data.
4- Show the output
e.g.
num1 num2 sum
13 50 ---
63
The output:
sum= 63
43
Control Structures
Selection: if/else
44
2.4 Control Structures
Sequential execution
Statements executed in order
Transfer of control
Next statement executed not next one in sequence
3 control structures
Sequence structure
Programs executed sequentially by default
Selection structures
if, if/else
Repetition structures
…
45
2.4 Control Structures
Flowchart
Graphical representation of an algorithm
Special-purpose symbols connected by arrows (flowlines)
Rectangle symbol (action symbol)
Any type of action
Oval symbol
Beginning or end of a program, or a section of code (circles)
Single-entry/single-exit control structures
Connect exit point of one to entry point of the next
Control structure stacking
46
2.5 if Selection Structure
Selection structure
Choose among alternative courses of action
Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
If the condition is true
Print statement executed, program continues to next statement
If the condition is false
Print statement ignored, program continues
Indenting makes programs easier to read
C ignores whitespace characters (tabs, spaces, etc.)
47
2.5 if Selection Structure
Translation into pseudo code
If student’s grade is greater than or equal to 60
Print “Passed”
if ( grade >= 60 )
WRITE "Passed";
Diamond symbol (decision symbol)
Indicates decision is to be made
Contains an expression that can be true or false
Test condition, follow path
if structure
Single-entry/single-exit
48
2.5 if Selection Structure
Flowchart of pseudocode statement
A decision can be made on
any expression.
true zero - false
grade >= 60 WRITE “Passed”
nonzero - true
Example:
false 3 - 4 is true
49
2.6 if/else Selection Structure
if
Performs action if condition true
if/else
Different actions if conditions true or false
Example text to Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
Algo:
if ( grade >= 60 )
WRITE "Passed";
else
WRITE "Failed";
50
Exam Grade Flowchart
READ examGrade, quizGrade;
if (examGrade < 60)
WRITE “We have a problem” ;
if (quizGrade < 10)
WRITE “We have a real problem” ;
else
WRITE “Ok”;
examGrade < 60 true
“We have a problem”
false quizGrade < 10 true
“Ok” “We have a real problem”
51
Ascending order 3 var using one auxiliary var START
READ A, B,C
yes
A>B
Tmp=A Swap A and B
A=B
B=tmp
No
B>C
yes
Tmp=B
B=C
C=tmp Swap B and C
No
A>B yes
Tmp=A
Swap A and B
A=B
B=tmp
write A, B,C
52 END
Ascending order 3 var using one auxiliary var START
A B C Tmp READ A, B,C
5 3 1
yes
A>B
5 3 1
Tmp=A Swap A and B
A=B
3 5 1 3 B=tmp
No
3 5 1 B>C
yes
Tmp=B
3 1 5 5 B=C
Swap B and C
C=tmp
No
3 1 5 3 A>B yes
Tmp=A
Swap A and B
1 3 5 A=B
B=tmp
write A, B,C
53 END
Ascending order 3 var using one auxiliary var START
READ A, B,C
yes
A>B
Tmp=A Swap A and B
A=B
B=tmp
No
B>C
yes
Tmp=B
B=C
C=tmp Swap B and C
No
A>B yes
Tmp=A
Swap A and B
A=B
B=tmp
write A, B,C
54 END
Ascending order 3 var using one auxiliary var START
A B C Tmp READ A, B,C
5 3 1
yes
A>B
5 3 1
Tmp=A Swap A and B
A=B
3 5 1 3 B=tmp
No
3 5 1 B>C
yes
Tmp=B
3 1 5 5 B=C
Swap B and C
C=tmp
No
3 1 5 3 A>B yes
Tmp=A
Swap A and B
1 3 5 A=B
B=tmp
write A, B,C
55 END
Exercise
ascending order without auxiliary var only branching
statements
56
START
READ A, B,C
No yes
A>B
No yes
No yes B>C
A>C
No No yes
yes C<A
B<C
write C,A, B write A, B,C
write B,C, A
write A, C, B
write A, B,C write B,A, C
57