Chapter 9-1
Chapter 9-1
and Problem-solving
Chapter 7
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-DownDesign/decompositon
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 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
Operator Action Example
Age ← 10 Const PI ← 3.14
+ add 9 + 3 =12
Name ← “John Smith”
- subtract 9 – 3=6
Chosen ← False
* multiply 9 * 3=27
/ divide 5 / 2=2.5
Cost ← 10 Cost has the value 10
DIV Integer 5 Div 2=2
Price ← Cost * 2 Price has the value 20 division
Tax ← Price * 0.12 Tax has the value 2.4 (MOD) modulus 9 MOD 3=0
SellingPrice ← Price + Tax ^ Raise to 3^3 =27
power
SellingPrice has the value 22.4
Integer = Integer + Integer Real = Real+ Real Real = Real+ Integer
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.
• Sum Num1 + Num2
• Pseudocode
Input and output statement
Process
INPUT OUTPUT
• INPUT/ READ is 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.
(a) Write a algorithm that calculate the area of circle.
Area PI * Radius * Radius
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
IF condition False
True
THEN
Statement1
c
Statement 2
ELSE
Statement1
c
Statement 2
ENDIF
Boolean/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
• DECLARE Number, Remainder : INTEGER
• OUTPUT “Enter Number :”
• INPUT Number
• Remainder Number MOD 2
• IF Remainder = 0
• THEN
• OUTPUT “Even Number “
• ELSE
• OUTPUT “Odd Number”
• ENDIF
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
DECLARE Grade : CHARCTER DECLARE Grade : CHARCTER
OUTPUT “ Enter Grade :” OUTPUT “ Enter Grade :”
INPUT Grade INPUT Grade
IF Grade = ‘A’ THEN CASE OF Grade
OUTPUT “ E…” ‘A’ : OUTPUT “E…”
ELSE ‘B’ : OUTPUT “G…”
IF Grade =‘B’ THEN ‘C’ : OUTPUT “Av..”
OUTPUT “G..” OTHERWISE
ELSE OUTPUT “Error”
IF Grade =‘C’ THEN ENDCASE
OUTPUT “Av..”
ELSE
OUTPUT “Error”
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
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 Choice OF
‘+’: Res ← 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
Homework
(b) Use a CASE statement to display the Month of the Year if the variable Month has the value 1
to 12 and an error otherwise.
Looping / Iteration / Repetition statement
• Print the string “Hello” 5 times.
OUTPUT “Hello”
OUTPUT “Hello”
OUTPUT “Hello” Iteration
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
Print 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
Average Total / 5
• Total 0
• FOR Count 1 To 5
• OUTPUT “Enter Number :”
• INPUT Num
• Total Total + Num
• NEXT Count
• OUTPUT “ Total :”, 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
1. Write an algorithm which
• Accept 50 input numbers and
• Find the number of even number and the number of odd number.
Evencount 0
Oddcount 0
FOR count 1 TO 50
OUTPUT “Enter Nu…”
INPUT Num
IF Num MOD 2 = 0 THEN
Evencount Evencount +1
ELSE
Oddcount Oddcount +1
ENDIF
NEXT Count
OUTPUT …
1. Write an algorithm which
• Accept 50 input numbers and
• Find the total of positive number and the average of positive
number.
Avg postotal / poscount
Postotal 0
Poscount 0
FOR count 1 To 50
INPUT Num
IF Num > 0 THEN
Postotal Postotal + Num
Poscount Poscount +1
ENDIF
NEXT count
Avg Postotal / Poscount
• Write an algorithm which
• Accept the number as an input
• Find and output the multiplication table of given input number ( 1 to 16)
• 2*1=2
OUTPUT “Enter Number :”
INPUT Num
FOR Count 1 TO 16
Ans Num * Count
OUTPUT Num , “*”, Count, “=“, Ans
NEXT Count
Homework
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.
Total ← 0
True: enter into loop False: exit loop/endwhile
INPUT Num
// 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
Rewrite the given pseudocode using WHILE---DO—ENDWHILE
Result1 Result 1
Count2
For count2 To 12 WHILE Count <= 12 DO
Result Count * Result
Result count * Result Count Count +1 //
ENDWHILE
NEXT Count PRINT “Result”, Result
PRINT “ Result”, Result
• Write a pseudocode that check the given input number is prime or
not prime.
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.”
Difference Between WHILE.DO.ENDWHILE 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
Looping/ Repetition
Standard action
Totaling - Total Total + Num
Counting- Count Count + 1
Count Count -1 ( count down)
Example 1
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.
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
Algorithm and Pseudocode
INPUT Num
INPUT Num Total 0
Count1
Total 0 WHILE Count <= Num Do
Total Total + Count
FOR Count 1 TO Num Count Count + 1
ENDWHILE
Total Total + Count OUTPUT “The toal is :”, Total
NEXT Count
OUTPUT “The Total is :”, Total
• Write a pseudocode or python which
• input the name and birth year of 5 student
• Find and display the oldest person of student name
• Find and display the youngest person of student name