Algorithm Design and Program Development
Algorithm Design and Program Development
Problem-solving
Chapter 7
Chapter 1
Calculation ( denary, binary, hexa)
File size
Chapter 2
Chapter 10
Chapter 3
The Program development life cycle
Analysis
The analysis stage uses abstraction and decomposition tools to
identify exactly what is required from the program
Coding
The program or set of program is developed using a suitable
programming language and then tested to see if it works
Testing
The completed program or set of programs is run many times with
different test data to ensure that all tasks completed work together as
specified in the program
Computer System , Sub system and Decomposition
What is Computer System
is made up of software, data, hardware,
communications and people. Computer System
each computer system can be divided up into a
set of sub-systems.
Sub-system Sub-system
Each subsystem can be further divided into sub-
systems and so on until each sub-system just
performs a single action.
Sub-system Sub-system
Program/software
Process
Data Result
Decomposing a problem
Any problem that uses a computer system for its solution need to be decomposed into its
components parts
Inputs : the data used by the system that needs to be entered while the system is active.
Processes : the tasks that need to performed using the input data and any other stored data.
Outputs : information that need to be displayed for the user of the system.
Storage : data that needs to be stored in file for use in the future.
Advantages of Top-Down Design / decomposition
Breaking a problem down into smaller parts/tasks makes it far easier to understand,
solve and manage
Top down design allows several programmers or teams to work on the same
project/task, without getting in each other’s way
Each sub-system of code to be tested separately
Abstraction
Abstraction keeps the key elements required for the solution to the problem and discard
any unnecessary details and information that is not required.
Method used to design and construct a solution to a problem
Structure Diagram
can be used to show top-down design (decomposition) in a diagrammatic
form.
shows the design of a computer system in a hierarchical way
m 80
Name “John Smith” n”John Smith”
1a
Mark 60.5 Total speed
numMark TotalSpeed
Total_speed
Variable and Constant
Constant
• A named memory location that contains data that can be read but not changed by the program
• Values are assigned variable using the <– operator.
DAYSOFWEEK PI
DAYSOFWEEK 7
7 3.14
PI 3.14
PI 3
Assignment Operator
Values are assigned variable/ Constant using the <– operator.
The variable to the left is assigned to a single value or several values combined with
mathematical operators on the right.
Mathematical/Arithmetic operators
Integer = Integer DIV Integer real = Real DIV Real real = Real DIV Integer
Num1 ← 4
Num2 ← 5 Operator
Operator Precedence
Num2 ← Num1 * Num2
() Highest
Operand ^
* / MOD DIV
Num1 ← 10 - 3 + 7 * 2 Num1 has value 21. +-
7 14 Lowest
CONSTANT PI 3.14
Problem 1
• Write a algorithm that calculate the addition of two numbers.
Input and output statement
Process
INPUT OUTPUT
• INPUTis used for the entry of data from user. Algorithm
• OUTPUT/ Print display of information/result to user.
The result is 4
Problem 1
• Write a algorithm that calculate the addition of two numbers.
• Sum Num1 + Num2
(a) Write a algorithm that calculate the area of circle.
Exercise
• Write an algorithm that find the area of square
• Write a pseudocode that convert cm to meter
• Meter Cm / 100
(3)Write an algorithm which
• Accept the hours and minutes as the input and
• Output the total minutes and the total second
• Write a algorithm that find and output perimeter and area of
rectangle .
Programming Construct
Programming language make use of three basic programming constructs (
Sequence
selection and
repetition ( looping)
Combining these constructs provides the programmer with the tools required to solve logical
problems
Sequence
Sequence is indicated by the order in which the code is written, usually top to bottom.
Sequential execution is when statements are executed one after another in order.
Statements are followed in sequence so the order of the statements in a program is important.
INPUT Num
Res Num * 2
OUTPUT Res
Selection /Conditional statements
Selection used to determine a different set of steps to execute based on a decision (condition).
Code branches and follows a different sequence based on decision made by program
For an IF condition the THEN path is followed if the condition is true and the ELSE path is
followed if the condition is false.
There may or may not be an ELSE path.
The end of the statement is shown by ENDIF.
IF condition False
True
THEN
Statement1
c
Statement 2
ELSE
Statement1
c
Statement 2
ENDIF
Logical/Comparison/Relational
Operator
A condition can be set up in different ways:
Using a Boolean variable that can have the value TRUE or FALSE
Using comparison operators
Input Output
1 The equivalent meter value is 0.01
2000 The equivalent meter value is 20
- 400 The equivalent meter value is -4
Problem 2
(a) Write a algorithm that convert the value of centimeter to meter and display the
converted meter.
THEN
Meter ← Cm / 100
OUTPUT “The equivalent meter value is”, Meter
ELSE
OUTPUT “Your input value is incorrect”
ENDIF
Problem 3
(a) Write a algorithm that determine the given mark is a pass or fail? ( a pass mark
is at least 50.
THEN
OUTPUT “Your number is even”
ELSE
OUTPUT “Your number is odd”
ENDIF
• Write a algorithm that check the given number is positive or negative
Problem 3
(c) Write a algorithm to find the largest number of given two numbers.
0 Num 10
Num > 0 Num< 10
x←3 x←6
AND Both (x > 0) AND (x < 10) = TRUE (x > 0) AND (x < 5) = FALSE
Statement OTHERWISE
Input Output OUTPUT “Error”
ENDCASE Good
B ENDCASE
DELCARE Grade : CHARACTER
OUTPUT “Enter Your Grade :”
INPUT Grade
IF Grade = ‘A’ THEN
OUTPUT “Ex….”
ELSE IF Grade = ‘B’ THEN
OUTPUT “G…”
ELSE IF Grade=‘C’ THEN
OUTPUT “A…”
ELSE
OUTPUT “Err..”
ENDIF
ENDIF
ENDIF
INPUT Grade INPUT Grade
CASE Grade OF IF Grade = ‘A’
‘A’ : OUTPUT “Excellent” THEN
‘B’ : OUTPUT “Good” OUTPUT “Excellent”
‘C’ : OUTPUT “Average” ELSE
OTHERWISE IF Grade = ‘B’
OUTPUT “Error” THEN
ENDCASE OUTPUT “Good”
ELSE
IF Grade = ‘C’
THEN
OUTPUT “Average”
ELSE OUTPUT “Error”
ENDIF
ENDIF
ENDIF
Problem 4
• Use a CASE statement to display the day of the week if the variable DAY has the value 1 to 7 and
an error otherwise.
INPUT Day
CASE OF Day Input Output
1: OUTPUT “Monday” 5 Friday
2: OUTPUT “Tuesday”
3: OUTPUT “Wednesday”
4: OUTPUT “Thursday”
5: OUTPUT “Friday”
6: OUTPUT “Saturday”
7: OUTPUT “Sunday”
OTHERWISE OUTPUT “Please enter a valid choice”
ENDCASE
• Write an algorithm which
• accept the two number and the operation as the input
• Find and output the result according to the following
Operation output
+ Display the addition result of two input number
- Display the sub..
* Display the mul
/ Display the division
Problem 4
Write a algorithm that perform mathematical OUTPUT “Enter Two Number”
operation of given two number according to the INPUT NUM1
user choice.
INPUT NUM2
Choice Operation OUTPUT “Enter Your choice”
+ Add INPUT Choice
- Subtract CASE OF Choice
‘+’: Res ← Num1 + Num2 // OUTPUT Num1 + Num2
* Multiply
OUTPUT “The result is ”, Res
/ Divide
‘-’: Res ← Num1 - Num2
OUTPUT “The result is ”, Res
Input Output ‘*’: Res ← Num1 * Num2
93/ The result is 3 OUTPUT “The result is ”, Res
‘/’: Res ← Num1/ Num2
53* The result is 15 OUTPUT “The result is ”, Res
OTHERWISE OUTPUT “Please enter a valid choice”
ENDCASE
•Write the pseudocode statements to perform this task:
•• accept the input of a whole number from 1 to 4 inclusive
•• use a CASE statement to:
• ○ output the number (1 to 4 inclusive) that was entered
• ○ output the word “ERROR” if a 1 to 4 inclusive number was not entered.
Homework
OUTPUT “Hello”
OUTPUT “Hello”
OUTPUT “Hello” Iteration/looping
OUTPUT “Hello”
OUTPUT “Hello”
Looping
When some actions performed as part of an algorithm need repeating, this is called
‘iteration’.
Loop structures are used to perform the iteration until certain number of times are reached or
until a certain condition is met.
.
There are three different types of loop structure in pseudocode:
1. FOR … TO … NEXT ( Count Control Loop)
2. WHILE … DO …ENDWHILE (Conditional Control loop)
3. REPEAT … UNTIL (Conditional Control loop)
FOR … TO … NEXT
•A for loop can only be used where the number of iteration is known at the time of programming.
• A variable is set up with a start value and an end value and then incremented in steps of one
until the end value is reached and the iteration finishes.
Record the
number of loop Count<=5
NEXT Variable
Count Output
OUTPUT “Hello”
OUTPUT “Hello” 1 Hello
OUTPUT “Hello” 2 Hello
OUTPUT “Hello” 3 Hello
OUTPUT “Hello” 4 Hello
5 Hello
6
Example
• Print 1 to 5 number. Count Output
1 1
FOR Count ← 1 TO 5 2 2 Count 1 to 5
OUTPUT Count 3 3 (5 times)
4 4
NEXT Count 5 5
6
4 -7
Problem 5
• Input 5 number and Find the sum of these input numbers. Input data : 2 4 3 3 5
NUM 2 4 3 3 5
Total ← 0 + + + + +
FOR Count ← 1 TO 5
OUTPUT “Enter Num” Total 0 2 6 9 12 17
INPUT Num Total ← Total +Num
Total ← Total +Num
NEXT Count
OUTPUT “The result is”, Total
Problem 5
• Input 5 number and Find the sum of positive input
numbers. Input : 3 5 -2 9 -3
Input : 3 5 -2 9 -3
Total ← 0
Count Num Total Output
FOR Count ← 1 TO 5
0
INPUT Num
1 3 3
IF Num > 0
2 5 8
THEN
3 -2 8
Total ← Total +Num
4 9 17
ENDIF
5 -3 17
NEXT Count
6 The result is 17
Print “The result is”, Total
Problem 5
Output: 15
Problem 5
INPUT NUM
Largest ← NUM
• Input 5 number and find the largest number. For Count ← 1 TO 4
3 4 2 7 1 Largest ← -1000
> > > > > FOR Count ← 1 TO 5
Largest -1000 3 4 4 7 7 INPUT Num
IF Num > Largest
THEN
Count Num Largest Output Largest ← Num
-1000 ENDIF
1 3 3 NEXT Count
2 4 4 Print “The largest Number is”, Largest
3 2 4
4 7 7
5 1 7
The Largest number is 7
• Write a pseudocode that take 50 numbers as the input and
• Find the number of positive input number
Poscount 0
Postotal 0
FOR Count 1 TO 50
OUTPUT “ Enter Number :”
INPUT Num
IF Num > 0 THEN
Poscount Poscount + 1 // count
Postotal Postotal + Num // total
ENDIF
NEXT Count
OUTPUT “ Pos total :”, Postotal
OUTPUT “ Pos count :”, Poscount
1. Write an algorithm which
• Accept 50 input numbers and
• Find the number of even number and the number of odd number.
1. Write an algorithm which
• Accept 50 input numbers and
• Find the total of positive number and the average of positive
number.
• Write an algorithm which
• Accept the number as an input
• Find and output the multiplication
table of given input number ( 1 to 16)
• Enter Number : 2
• 2* 1= 2
• 2*2 =4
• 2*3 = 6
• ..
• 2* 16= 32
3. Write an algorithm which take number as the input and
find and output the factorial value of input number
4 ! = 4 * 3 * 2* 1 = 1 * 2 * 3 * 4 = 24
3! = 1 * 2 * 3 = 6
1. Write pseudocode to input 100 and find the total and the average.
2. Write pseudocode to input 10 and find the smallest number.
3. Find the total number 5 to 50
- Loop ( for)
- For var <
- Total Total + 5
- Total total + 6
Total 0
Num 5
For i 1 To 45 Do
Total Total + Num
NumNum +1
Next i
WHILE … DO … ENDWHILE
is used when the number of repetitions/iterations is not known and the actions are only
repeated WHILE a given condition is true.
If the WHILE condition is untrue when the loop is first entered then the actions in the loop
are never performed.
WHILE condition DO
Statement 1
Statement 2
ENDWHILE
// Count ← 1
Total ← 0 Total ← 0
FOR Count ← 1 TO 5 Count ← 1
INPUT Num WHILE Count <=5 DO
// Count <= 5
Total ← Total +Num INPUT Num
NEXT //Count = Count +1 Total ← Total +Num
Print “The result is”, Total Count ← Count +1
ENDWHILE
Print “The result is”, Total
REPEAT … UNTIL
is used when the number of repetitions/iterations is not known and the
actions are repeated UNTIL a given condition becomes true.
The actions in this loop are always completed at least once.
REPEAT
Statement 1
Statement 2
UNTIL condition True exit loop
False repeat
Example
Convert cm to m for given number. ( Only
accept positive number)
MyNum ← 9 MyNum ← 9
PRINT “ I have a number (0-10), can you PRINT “ I have a number (0-10), can you
guess it” guess it”
REPEAT PRINT “ Enter your guess number”
PRINT “ Enter your guess number” INPUT UserNum
INPUT UserNum WHILE MyNum <> UserNum DO
PRINT “ Enter your guess number”
UNTIL MyNum = UserNum
INPUT UserNum
PRINT “ Congratulation, You are right.”
ENDWHILE
PRINT “ Congratulation, You are right.”
• Username “STZA”
• Password “stza123”
• Count 0
• REPEAT
• OUTPUT “ Enter username and password:”
• INPUT User
• INPUT Pass
• Count Count + 1
• UNTIL (User = Username AND Password = Pass) OR Count = 3
• IF Count = 3 THEN
• OUTPUT “ Lock your acc:”
• ELSE
• OUTPUT “Welcome”
• ENDIF
• Username “STZA”
• Password “stza1234”
• Count 0
• REPEAT
• OUTPUT “ Enter username and password :”
• INPUT User
• INPUT Pass
• Count Count + 1
• UNTIL ( User = Username AND Pass = Password ) OR Count =3
• IF ( User = Username AND Pass = Password ) THEN
• OUTPUT “ Welcome “
• ELSE
• OUTPUT “ Lock Your acc “
• ENDIF
Difference Between [Link] and
REPEAT..UNTIL
WHILE
In WHILE , condition check at the start of the loop
If the condition at the first iteration, appears false, the
iterations do not occur
REPEAT UNTIL
// Count ← 1
Total ← 0 Total ← 0
FOR Count ← 1 TO 5 Count ← 1
INPUT Num Count = 6 REPEAT
// Count <= 5
Total ← Total +Num INPUT Num
NEXT //Count = Count +1 Total ← Total +Num
Print “The result is”, Total Count Count +1
UNTIL Count > 5
Print “The result is”, Total
Tickets are sold for a concert at $20 each, if 10 tickets are bought then the discount is 10%,
if 20 tickets are bought the discount is 20%. No more than 25 tickets can be bought in a
single transaction.
Write a program to calculate the cost of buying a given number of tickets.
Totalcost tickets * 20
Discount Totalcost * Dispercent
Cost Totalcost- Discount
IF TicketNumber >= 10 AND TicketNumber <20
REPEAT THEN
OUTPUT “ How many ticket would you like to buy” Discount 0.1
INPUT TicketNumbers ELSE IF TicketNumber >=20 THEN Disount 0.2
ELSE
UNTIL TicketNumbers > 0 AND TicketNumber < 26 Discount 0
IF TicketNumbers >= 20 Testing
Then How many ticket would you like to buy 5
Discount <- 0.2
TicketNumber =5
ELSE
Discount =0
IF TicketsNumbers>=10
Cost= 100
THEN
Discost= 0
Discount <- 0.1
cost= 100
ELSE
How many ticket would you like to buy 21
Discount <- 0
TicketNumber =21
ENDIF
ENDIF Discount =0.2
Cost <- TicketNumbers * 20 Cost= 420
Discost <- Cost * Discount Discost= 84
Cost <- Cost – Discost cost= 336
OUTPUT “Your tickets cost “, cost
• A school with 5 students wants to produce some information from the exam result of the four subject
in Physics, Science, English and IT. Each result is out of 100 marks. The information output should
be the total and average marks of each student. The information output should be average mark for
each student. All the marks need to be input. Write a program to complete this task.
CASE Subject OF
Stu Name Subject Subna Mark Total Averag
1: SubName ← “Physics” me e
2: SubName ← “Science” 1 Mya 0
3: SubName ← “English” 1 Phy 63 63
1. Write pseudocode to input 100 and find the total and the average.
2. Write pseudocode to input 10 and find the smallest number.
Homework
(a) Write a algorithm that find the area of square.
(b) Write a algorithm that calculate the area of triangle.
(c) Write a algorithm that convert the value of mile to meter and display the converted
meter.
(d) Write pseudocode to input number and find the total of these number, to finish when
total number is greater than 1000