Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Lecture 02

Pseudocode & Flowcharts


.
Levels of Program Development

1. Define the problem. →Human thought


2. Plan the problem solution. → writing the
algorithm [pseudo-natural language (English,
Arabic) or drawing the flowchart diagram).
3. Code the program. → High Level
Programming Language (C, C++, Java, …)
4. Compile the program. → Machine Code
5. Run the program.
6. Test and debug the program.
2
From Lec1 we learn that

 When planning for a problem solution, algorithms


are used to outline the solution steps using
 English like statements, called pseudocode.
or
 A flowchart , which is a graphical representation
of an algorithm.

3
Pseudo code

• Pseudo code is a detailed description of what a computer program


must do, expressed in an English like language rather than in a
programming language.

4
Pseudocode Example

 Write a Program to Print the Sum of two integer


Numbers

1. Start the program


2. Read the first number and save in the variable (
N1 )
3. Read the second number and save in the variable (
N2 )
4. Sum the both numbers and save the result in the
variable ( Sum ) ➔ Sum = N1 + N2
5. Print the variable ( Sum )
6. End the program
5
Flowchart

• A flowchart is a type of diagram that represents an algorithm ,


showing the steps as boxes of various kinds [ex: rectangles, diamonds,
ovals], and their order by connecting these with arrows.

6
Flowcharts Symbols

7
End Start Start/End
Print n1 Read n1 Read/Print

N2 = n1+3 N2 = 5 Arithmetic Operations

n1 > 3 Decision , can be used with loops


Solution
start
Draw a flowchart for a program that calculates
and print the area and the perimeter of Read L, W
a rectangle.
• Input area = L * W
• Length
• width
• Processing perimeter = 2 (L+W)
• Area = length*width
• Perimeter = 2*( length + width)
Print area
• Output
• Area
• Perimeter Print perimeter

End 8
Example 2

• Draw the flow chart for a program that calculates the total salary for
an employee using this equation:
Total Sal = Salary +Overtime

9
Solution

 Input start
 Salary
 Overtime Read Salary
 Processing
 Total_Sal = Salary +Overtime
Read Overtime
 Output
Total_Sal =
 Total_Sal
Salary +Overtime

Print Total_Sal

End 10
Example 3

• Draw a flowchart for a program that calculates and prints the sum of
the even integers from 2 to 30.
• Input
• No input.
• Processing
• Sum = 2+4+6+8+……+28+30.
• Output
• sum

11
Solution

Pesudocode:
◼Start the program
◼Create a variable to hold a counter from 2 to 30.
◼Initialize the counter to 2.
◼Create a variable to hold the sum.
◼Initialize the sum to zero.
◼Loop While the counter is less-than-or-equal to 30
◼add the counter to the sum
◼add two to the counter.
◼ repeat until the counter reach 30
◼Print the sum.
◼End of program

12
Solution

Start

Counter=2, Sum=0

yes
no
Counter≤30

yes
Sum= Sum + Counter

Counter=Counter+2

Print Sum

End 13

You might also like