Lecture 7 Problem Solving Part2 Updated
Lecture 7 Problem Solving Part2 Updated
Lecture 7 Problem Solving Part2 Updated
6 13
temp
Step-1 Start
Step-8 Stop
start
Input Num1 ,
Num2
Temp=Num1
Num1=Num2
Nume2=Temp
Input Num1 ,
Num2
end
Algorithm & Flowchart to Swap Two Numbers
without using temporary variable
A B
Steps
6 13
Step-1 Start
Step-2 Input Two Numbers Say A,B A 6+13=19
Step-4 A=A+B
A 19-6=13
Step-5 B=A-B
Step-6 A=A-B
Step-7 Display After Swap Values A, B
Step-8 Stop Perpered by Dr. Walaa Khaled
Flowchart: Draw the flowchart of the previous Algorithm.
Algorithm & Flowchart to find Even number
between 1 to 50
DECISION STRUCTURES
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
Decision Logic Structure
yes no
1 2
Your Turn
X
COUNTER
=
AVERAGE = TOTAL
READ NUMBER COUNTER
PRINT AVERAGE
TOTAL = TOTAL + NUMBER
COUNTER = COUNTER + 1
STOP
END OF
DATA
X
NESTED IF/THEN/ELSE INSTRUCTIONS
Multiple decisions.
Instructions are sets of instruction in which
each level of a decision is embedded in a level
before it.
NESTED IF/THEN/ELSE INSTRUCTIONS
Repeat structure
To solve the problem that doing the same task
over and over for different sets of data
Types of loop:
WHILE loop
Do..WHILE loop
Automatic-Counter Loop
WHILE loop
WHILE loop
Set number = 1
Set total = 0
No
number <= 100
Yes
Display total
total =
total + number
End
number =
number + 1
DO…WHILE Loop
The body of the loop will process first
before check the condition.
Example: Get the sum of 1, 2, 3, …100.
Automatic Counter Loop
Use variable as a counter that starts counting at
a specified number and increments the variable
each time the loop is processed.
The beginning value, the ending value and the
increment value may be constant. They should
not be changed during the processing of the
instruction in the loop.
Automatic-Counter Loop
Terminator
for the
loop
Terminator for
the flowchart
Counter R=0
C=0
C=1
C=2
Counter R=1
C=0
C=1
C=2
Counter R=2
C=0
C=1
C=2
NESTED LOOP
The company pays for all of Plan 1. The individual has to pay for part of
the others.
The payroll deduction for Plan 2 = 4.65, for Plan 3 = 7.85, and for Plan 4
= 5.50.
Any other codes are considered in error. Write the algorithm and draw the
flowchart for a module to determine the payroll deduction.
Example of Case Logic Structure
NESTED IFS
One of the alternatives within an IF–THEN–
ELSE statement
may involve further IF–THEN–ELSE statement
EXAMPLE
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid