Lecture 7 Problem Solving Part2 Updated

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

Programming Languages

and Program Development


(Part 2)

Prepared by Dr. Walaa Khaled


Your Turn

Write an Algorithm & Flowchart to Swap Two Numbers using


Temporary Variable

6 13

temp

Prepared by Dr. Walaa Khaled


Steps

Step-1 Start

Step-2 Input Two Numbers Say NUM1,NUM2

Step-4 TEMP = NUM1 Step-5 NUM1 = NUM2

Step-6 NUM2 = NUM1

Step-7 Display NUM1,NUM

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-3 Display Before Swap Values A, B B 19-13=6

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

Write an Algorithm and a Flowchart to read cost price


and selling Price and to find profit or loss.
Perpered by Dr. Walaa Khaled
Example
 Assume your are calculating pay at an hourly
rate, and overtime pay(over 40 hours) at 1.5
times the hourly rate.
 IF the hours are greater than 40, THEN the
pay is calculated for overtime, or ELSE the
pay is calculated in the usual way.
Example Decision Structure

Perpered by Dr. Walaa Khaled


If Then and simple Loop
Example 8
 Finding Average Problem
 Read a sequence of number, find the average of the number
and print the average.
 Solution: Stepwise Analysis of Average Problem
◼ Start the processing
◼ Read a number
◼ Add the number
◼ Repeat reading until last data
◼ Calculate the average
◼ Print the average
◼ Stop the processing
Example 8
START

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

Perpered by Dr. Walaa Khaled


The Loop Logic Structure

 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

 Do the loop body if the condition is true.


 Example: Get the sum of 1, 2, 3, …, 100.
 Algorithm:
◼ Set the number = 1
◼ Set the total = 0
◼ While (number <= 100)
◼ total = total + number
◼ number = number + 1
◼ End While
◼ Display total
WHILE loop
Start

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

Perpered by Dr. Walaa Khaled


Automatic-Counter Loop

Perpered by Dr. Walaa Khaled


NESTED LOOP
Suppose you have two counter R and C , for each R you need to
Write three values of C. Write A flow chart assume R varies from
0 to 1 and c from 0 to 2

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

Perpered by Dr. Walaa Khaled


NESTED LOOP

Perpered by Dr. Walaa Khaled


Your Turn
Write an algorithm and a Flowchart of multiplication
table for a given number N

Perpered by Dr. Walaa Khaled


Perpered by Dr. Walaa Khaled
The Case Logic Structure
 Made up of several or many sets of instructions, only one
of which will be selected by the user and executed by the
computer
 Algorithm:
CASE OF VARIABLE
= constant1:
actions for VARIABLE = constant1
= constants2:
actions for VARIABLE = constant2

OTHERWISE:
Actions for VARIABLE = anything else
END-OF-CASE
Perpered by Dr. Walaa Khaled
Case Logic Structure

Perpered by Dr. Walaa Khaled


Case Logic Structure
 Example: A company has four different medical plans. The programmer
has given each plan a code corresponding to the beginning initial of the
company: Plan 1 = F, Plan 2 = B, Plan 3 = K, Plan 4 = E.

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

• Write an algorithm that reads three numbers and prints


the value of the largest number.
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX ← N1 [N1>N2, N1>N3]
else
MAX ← N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX ← N2 [N2>N1, N2>N3]
else
MAX ← N3 [N3>N2>N1]
endif
endif
Perpered by Dr. Walaa Khaled
Step 3: Print “The largest number is”, MAX
Problem 3

 Write an algorithm and draw a flowchart to


a) read an employee name (NAME), overtime
hours worked (OVERTIME), hours absent
(ABSENT) and
b) determine the bonus payment (PAYMENT).
Problem 3

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10

You might also like