0% found this document useful (0 votes)
6 views

CSE031.Lecture - 04.flow - Charts - Pseudocode - Fall 2019

Uploaded by

Bishoy Emad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CSE031.Lecture - 04.flow - Charts - Pseudocode - Fall 2019

Uploaded by

Bishoy Emad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Computer and Systems Engineering Department

Ain Shams University, Cairo, Egypt


How do you solve a problem in the exam?
1. Understand the problem.
2. Define the variables and collect the
required facts (inputs).
3. Make any assumptions if needed.
4. Select suitable formulae or theorems.
5. Solve the problem.
6. Check your results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
• The computer is only a problem-solving tool
• To use the computer to solve a problem:
• Perform steps 1-4 in previous slide
• Develop the steps the computer should
follow to solve the problem or Algorithm
• Represent the steps graphically using a
Flowchart.
• You may also represent the steps in
structured English as Pseudocode
• Edit, run and test the program in a
computer programming language, Fortran,
C, Java, Python ….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
• Definition
• A graphical approach to Design/Plan
the program
• Why?
• Arrange our ideas in a visual way
• Is programming language independent
• Easier for checking the correctness of
the algorithm and testing the program

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
• Definition
• A structured English for describing
algorithms.
• Properties
• Focus on the problem not the
implementation (program).
• Is programming language independent
• No Universal standard. Each company
enforces its own standard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Start Start

END End

Process

Yes No
`
` Decision

Input or Output Process

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
• The pseudocode is a sequence of constructs that
describe the algorithm.
• Each construct can be:
• Calculation / Input / Output
• Input length, and width
• Calculate area as length * width
• Print area
• Loop
• WHILE - END WHILE
• FOR – END FOR
• REPEAT - UNTIL
• Decision
• IF - THEN - ELSE – END IF
• CASE – WHEN – END CASE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Problem: Calculate the area of a rectangle given
its length and width.
Analysis:
Inputs: Length L and Width W, Output: Area
Formula: Area = L*W
Assumptions: L, W > 0
Steps:
1. Input L, W
2. Calculate Area
3. Output Area
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Start
Sequential Instructions
INPUT L

PROGRAM main
Input L,W INPUT W

Calculate Area as L * W
Print Area AREA = L * W
END main
PRINT AREA

END

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Problem: Given the score of a student in an exam,
print “Failed” if the score is less than 50 or
“Succeeded” otherwise.
Analysis:
Input: Score, Output: “Succeeded”/“Failed”
Assumptions: 0 ≤ score ≤ 100
Steps:
1. Input Score
2. If Score < 50 Print “Failed”
Else Print “Succeeded”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Start
2-sided if structure
INPUT SCORE

PROGRAM main
Input Score Yes is No
IF (SCORE < 50) THEN SCORE < 50
?
Print “Failed”
ELSE
Print “Succeeded” PRINT “Failed” PRINT “Succeeded”
END IF
END main
End

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Problem: Read 3 numbers and print the value of
the greatest one.
Analysis:
Inputs: A, B, C, Output: max(A, B, C)
Steps:
1. Input A, B, C
2. If A > B
If A > C Print A, Else Print C
Else
If B > C Print B, Else Print C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Start
PROGRAM main
Input A,B,C
READ A
IF (A > B) THEN READ B
IF (A>C) THEN READ C

Print A
Yes No
ELSE is
A>B
Print C ?

END IF Yes is No Yes is


B>C
No
A>C
ELSE ? ?

IF (B>C) THEN PRINT C PRINT B PRINT C


PRINT A
Print B
ELSE
Print C
END IF
END IF
END main End

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Problem: Print numbers from 1 to 100.

Analysis:
Input: None, Output: Numbers from 1 to 100

Steps:
1. I = 1
2. Repeat until I > 100
i. Print I
ii. I = I + 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Start

Repetition or loop structure


with loop test at the bottom I=1

PROGRAM main
PRINT I
Set I=1
REPEAT
Print I I=I+1

Set I=I+1
UNTIL (I>100) Yes is
END main I <= 100
?

No

END

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Input: None Start
Output: Odd Numbers from 1 to 100
I=1

PROGRAM main
Set I=1
REPEAT PRINT I

Print I
Set I=I+2 I=I+2
UNTIL (I>100)
END main
Yes is
I < 100
?

No

END

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Input: M Start

Output: Numbers from 1 to M


and their squares INPUT M

PROGRAM main I=1 1 1


Input M 2 4
Set I=1 J=I^2
3 9
REPEAT
Set J = I^2 PRINT I, J
.. ..
Print I,J M M2
Set I=I+1 I=I+1

UNTIL (I>M)
END main No is
I>M
?
Yes

END

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Input: None
Output: Sum
PROGRAM main
Set SUM=0; N=1
REPEAT
Set SUM = SUM+N
Set N=N+1
UNTIL (N>100)
Print SUM
END main

PROGRAM main
Set SUM=0
FOR (N=1 to 100 Step 1)
Set SUM = SUM+N
END FOR
Print SUM
END main

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Problem: Calculate the average of the scores of 50
students.
Analysis:
Input: scores of 50 students, Output: average score
Formula: Average = (S1+ ... + S50)/50
Steps:
1. Initialize Sum=0, N=1
2. Repeat until N > 50
i. Input next Score
ii. Sum = Sum + Score
iii. Increment N
3. Print Sum/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Start

PROGRAM main
Set SUM=0 SUM = 0
FOR (N=1 to 50 Step 1) N=1
Input SCORE
Set SUM = SUM+SCORE
END FOR
Set AVERAGE = SUM/50 INPUT SCORE
Print AVERAGE
END main
SUM = SUM + SCORE AVERAGE = SUM / 50

N=N+1 PRINT AVERAGE

No is Yes END
N > 50
?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1
N =1 N
M

Repetition or loop structure


with loop test at the top
PROGRAM main
Input M
WHILE (M<1)
print “Error”
Input M
END WHILE
Set S=0
FOR (N=1 to M Step 1)
Set S = S+1/N
END FOR
Print S
END main

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Problem: Calculate the factorial of an integer.
Analysis:
Input: M, Output: F
Formula: F = M*(M-1)* … * 1 for M ≥ 1
F = 1 if M = 0 (special case)
Assumption: M is non-negative
Steps:
1. Input M
2. Initialize F=1, N=1
3. While M>N
i. N=N+1
ii. F=F*N
4. Print F

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
In this example the Start

loop test is the first


INPUT M
statement in the
loop. Why? N=1
F=1

PROGRAM main
INPUT M F=F*N
Set F=1; N=1
WHILE (N<M) N=N+1
No
Set N=N+1 is
N >= M
Set F=F * N
?
END WHILE
Yes
Print F
END main PRINT F

END
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Exam-like Questions I
1. For the given flowchart, what is the first value printed?
Start
A) 0 B) 1 C) 2 D) 100
INPUT M 2. For the given flowchart, what is the value of M?
A) 0 B) 5 C) 100 D) It depends on the user
X=1
S=0
3. For the given flowchart, if M = 100, how many values are printed?
A) 0 B) 1 C) 100 D) 101
S=S+X
4. For the given flowchart, if M = 100, what is the final value of X?
PRINT S A) 0 B) 1 C) 100 D) 101
5. For the given flowchart, if M = 5, what is the last value printed?
X=X+1
A) 0 B) 1 C) 5 D) 15
No is
X>M
?
Yes

END
Exam-like Questions II
Questions 1 to 8 are based on the given flowchart
1. For the given flowchart, if M=100, how many times will the statement N=N+1 be executed?
A) 99 B) 100 C) 101 D) None of the
previous
2. For the given flowchart, if M = 100, how many values are printed?
A) 0 B) 1 C) 100 D) 101
3. For the given flowchart, if M = 5, what is the final value of N?
A) 3 B) 4 C) 5 D) 6
4. For the given flowchart, if M = 3, what is the value printed by the program?
A) 1 B) 1.25 C) 1.5 D) 1.75
Exam-like Questions II (cont.)
Thank You
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

You might also like