0% found this document useful (0 votes)
12 views97 pages

Chapter 9-1

Chapter 7 discusses the Program Development Life Cycle, which includes stages of Analysis, Design, Coding, Testing, and Maintenance. It emphasizes the importance of decomposition and abstraction in problem-solving, allowing complex systems to be broken down into manageable sub-systems. The chapter also covers algorithms, data types, variables, constants, and programming constructs such as sequence, selection, and repetition.

Uploaded by

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

Chapter 9-1

Chapter 7 discusses the Program Development Life Cycle, which includes stages of Analysis, Design, Coding, Testing, and Maintenance. It emphasizes the importance of decomposition and abstraction in problem-solving, allowing complex systems to be broken down into manageable sub-systems. The chapter also covers algorithms, data types, variables, constants, and programming constructs such as sequence, selection, and repetition.

Uploaded by

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

Algorithm Design

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

Program development Life cycle Design


is divided into five stages : Analysis, This stage uses the structure charts, flowcharts and pseudocode to
Design, Coding, Testing and develop the design of the program.
maintenance

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

User Input Devices


Output Device
Top-Down Design/ Decomposition
 is the decomposition of the a computer system into a set of sub-systems, then
breaking each sub-system down into a set of smaller sub-systems, until each
sub-system just performs a single task.
 E.g
 Large firms are broken down into smaller departments, and sub-teams

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

Example : Student mark system that calculate average mark

Input : Get data about student name and his/her marks

process : Calculate the average of input mark.

output : Display the student name and his/her average mark


Example
 Below is a structure diagram showing how a student mark system that
can calculate the average mark for student can be broken down into
smaller sub-systems.

Student Mark System

Get Data Calculate Average Mark Display Results

Output Output Student’s


Input Student’s Input Student’s
Student’s name Average Marks
name Marks
(b) Identify three of the component parts after a problem has been
decomposed.
1…………………………………………………………………
2…………………………………………………………………
3………………………………………………………………..
(c) Identify three different ways that the design of a solution to a problem can be
presented.
1…………………………………………………………………
2…………………………………………………………………
3………………………………………………………………..
A satellite navigation system is an example of a computer system that is made up of sub-
systems.
Part of a satellite navigation system:
• allows the user to enter details for a new destination or select a previously saved destination
• displays directions in the form of a visual map or as a list.
Draw a structure diagram for this part of the satellite navigation system.
Algorithm
 An algorithm is a procedure used for solving a problem or performing a computation.
 There are two ways to represent algorithm
 Flow Chart
 Pseudocode
Pseudocode
 is a method of expressing an algorithm design.
 It describes what the algorithm does by using English key words that are very similar to
those used in a high-level programming language
 are no definitive rules for writing pseudocode but it should make sense and logically solve
the problem/task.
 Once a problem has been solved logically using pseudocode, it can then be coded into
an actual programming language of choice.
How to Write Pseudocode?
Data/ Input
Result/Output
Program

Get input data


Process
Output
Data Type
 represents the kind of value that tells what operations can be performed on
a particular data

Data Type Description Literals


Integer The whole number. 3,10,-4 3
Real or float Numbers with decimal 4.0, 0.0, 0.1, 11.06,-3.4
places
Character Single character ‘A’, ‘b’, ‘@’ ‘A’ ‘1’
String A sequence of zero or “ Hello World”
more characters “Hello 123” “123”
Alphanumeric
Boolean The logical values True TRUE / FALSE or True/False
and False
Variable and Constant
 The program needs to remember/store a value to read and use later on
 In order to store a value, the program needs to establish a memorable (named)
place.
 These locations/named are called variables and constants.
Variable
 The identifier (name) given to a memory location used to store data.
 The value can be changed during program execution.
 Values are assigned variable using the <– operator.
Mark Name
Mark  80 80 60.5 John Smith

m 80
Name  “John Smith” n”John Smith”
1a
Mark  60.5 Total speed
numMark 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 - Integer Real = Real - Real Real = Real- Integer

Integer = Integer * Integer Real = Real * Real Real = Real* Integer

Real = Integer / Integer Real = Real / Real Real = Real/ Integer

Integer = Integer DIV Integer real = Real DIV Real real = Real DIV Integer

5.0 DIV 2.0 = 2.0


5 DIV 2 = 2
Operator Precedence

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

Operator(+) is used to concatenate two String

Str ← “Hi” + “Friend” Str has the value HiFriend


Activity 10.1
Amount ← 0 Amount has value 0.
Amount ← 100 Amount has value100.
TotalPrice ← Amount * 3.5 TotalPrice has value 350.0.
Discount ← 0.2 Discount has value 0.2.
FinalPrice ← TotalPrice – TotalPrice * Discount FinalPrice has value 280.0.
Name ←"Nikki" Name has value Nikki.
Message ←"Hello" + Name Message has value HelloNikki.
Number ←(20-8) * 2 / 3 Number has 8.0.
How to declare variable
Declare variables that are used in pseudocode.
DECLARE variable name : Data Type
DECLARE Number : INTEGER

Declare constant that are used in pseudocode.

CONSTANT name  value

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.

To get data from user To display data to user Example


Res ← 4
INPUT variable Name OUTPUT variable
OUTPUT Res 4
READ variable Name PRINT variable
or
PRINT Res
Example
OUTPUT variable , variable Res ← 4 45
INPUT Num PRINT variable, variable Num ← 5
READ Num OUTPUT Res, NUM
Num

8 8 OUTPUT String , variable Res ← 4


PRINT String, variable OUTPUT “The result is”, res

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

DECLARE Area , Radius : REAL


CONSTANT PI  3.14
OUTPUT “Enter the radius of circle :”
INPUT Radius
Area  PI * Radius * Radius
OUTPUT “ The area of circle :”, Area
Exercise
• Write an algorithm that find the area of square.
• Write an algorithm that find the area of triangle.
• 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

• TotalMin  (Hours * 60) + Minutes


• TotalSec  TotalMin * 60
Sequence, Selection and Repetition
Programming language make use of three basic programming constructs ( Sequence , selection and
repetition).
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

 A condition that can be true or false: IF … THEN … ELSE … ENDIF

 A choice between several different values: CASE … OF … OTHERWISE …ENDCASE


IF … THEN … ELSE …
ENDIF
 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
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

Operator Comparison x ←6 x←3


> Greater than x > 4 = TRUE x > 4 = FALSE
< Less than x < 7 = TRUE x < 0 = FALSE
= Equal x = 6 = TRUE x= 6 = FALSE
>= Greater than or x >=4 = TRUE x >=6 = FALSE
equal x >= 6 = TRUE
<= Less than or x <= 7 = TRUE x <= 0 = FALSE
equal x <= 6 = TRUE
<> Not equal x < > 5 = TRUE x <> 3 = FALSE
Problem 2
(c ) Write an algorithm that convert the value of centimeter to meter and display the
converted meter.

OUTPUT “Enter the centimeter value:” Cm = pos - meter


conversion
INPUT Cm
Cm = neg  error
Meter ← Cm / 100
OUTPUT “The equivalent meter value is”, Meter

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.

OUTPUT “Enter the centimeter value:” Input Output


INPUT Cm 1 The equivalent meter value is 0.01

IF Cm > 0 Ture False - 400 Your input value is incorrect

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.

OUTPUT “Enter Your mark:”


INPUT Mark
IF Mark < 50
IF Mark >= 50
THEN
THEN
OUTPUT “Sorry!! You fail exam”
OUTPUT “Congratulation!! You pass exam”
ELSE
ELSE OUTPUT “Congratulation!! You
OUTPUT “Sorry!! You fail exam” pass exam”
ENDIF
ENDIF
Input Output
60 Congratulation!! You pass exam
44 Sorry!! You fail exam
Problem 3
(b) Write a algorithm that check the given number is even or odd.
Testing/Trace
OUTPUT “Enter Number:” Input Num Reminder Output
INPUT Num 16 16 0 Your number is even
Reminder ← Num MOD 2 5 5 1 Your number is odd
IF Reminder = 0

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.

OUTPUT “Enter Two Number:”


INPUT Num1 Input Output
INPUT Num2 79 9 is largest number
IF Num1 > Num2
55 5 number is largest
THEN
OUTPUT Num1 ,“is largest number”
ELSE
OUTPUT Num2 ,“is largest number”
ENDIF
OUTPUT “Enter Two Number :”
INPUT Num1
INPUT Num2
IF Num1 > Num2 THEN OUTPUT “ N…”
ELSE
IF Num1 = Num2 THEN OUTPUT “ E..”
ELSE
OUTPUT “ Num2 is
ENDIF
ENDIF
Problem 3
(c) Write a algorithm to find the largest number of given two numbers.

OUTPUT “Enter Two Number:”


INPUT Num1 Input Output
INPUT Num2 79 9 is largest number
IF Num1 > Num2
55 Two number is equal
THEN
OUTPUT Num1 ,“is largest number”
ELSE
IF Num1 = Num2
Nested if
THEN OUTPUT “Two number is equal”
ELSE
OUTPUT Num2 ,“is largest number”
ENDIF
ENDIF
Logical Operator
 Check a number is between 0 and 10

0 Num 10
Num > 0 Num< 10

 Logical operator is used to combine condition statements

x←3 x←6
AND Both (x > 0) AND (x < 10) = TRUE (x > 0) AND (x < 5) = FALSE

OR either (x > 0) OR (x < 5) = TRUE (x > 0) OR (x < 5) = TRUE

NOT NOT NOT (x > 0) = FALSE NOT (x < 5) = TRUE


Problem 3
The algorithm that checks if a mark is valid and a pass or a fail. Note . The input
mark is between 0 and 100 (inclusive).
OUTPUT “Enter Your mark:”
INPUT Mark Input Output
IF Mark >= 0 AND Mark <= 100 80 Congratulation!! You pass exam
THEN
120 Invalids marks
IF Mark >= 50
THEN
OUTPUT “Congratulation!! You pass exam”
ELSE
OUTPUT “Sorry!! You fail exam”
ENDIF
ELSE
OUTPUT “Invalids marks”
ENDIF
Problem 3
• Write a algorithm to find the largest number of given difference three numbers.
OUTPUT “Enter three Number:”
INPUT Num1 ELSE
INPUT Num2 OUTPUT Num3 ,“is largest number”
INPUT Num3 ENDIF
IF Num1 > Num2 AND Num1 > Num3 ENDIF
THEN
OUTPUT Num1 ,“is largest number” Input Num1 Num2 Num3
598 5 9 4
ELSE
IF Num2 > Num3
THEN 9 ,“is largest number”
OUTPUT Num2 ,“is largest number”
Write a pseudocode that
• Input three number
• Find the multiply value of largest number and second largest number.
Num1, Num2, Num3

Num1 * Num2 ( if Num3 is smallest)


Num1 * Num3 (if Num2 is smallest)
Num2 * Num3 (if num1 is smallest)
• OUTPUT “Enter Three Number :”
• INPUT Num1
• INPUT Num2
• INPUT Num3
• IF Num1 < Num2 AND Num1 < Num3
• THEN
• Ans  Num2 * Num3
• OUTPUT “ The multiply of two number :”, Ans
• ELSE
• IF Num2 < Num3
• THEN
• Ans  Num1 * Num3
• OUTPUT Ans
• ELSE

CASE … OF … OTHERWISE …ENDCASE
 Tests the value of a variable and compares it with multiple value (case).
 Once the case match is found, a block of statement associated with this case is executed.
 If a case match is not found, the otherwise statement is executed.

CASE OF variable/statement INPUT Grade


A Excellent
CASE OF Grade
value1: statement
Grade B Good ‘A’ : OUTPUT “Excellent”
value2: statement ‘B’ : OUTPUT “Good”
C Average
OTHERWISE ‘C’ : OUTPUT “Average”

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

FOR Variable ← start value TO End value FOR Count ← 1 TO 5

Statement 1 Print “Hello”


Statement 2 NEXT Count Count ← Count+1

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

Input 4 number Input data: 1 10 8 -7

FOR Count ← 1 TO 4 Count Num


INPUT Num 1 1
2 10
NEXT Count
3 8

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

Total ← 0 Count Num Total Output


FOR Count ← 1 TO 5 0
INPUT Num
Total ← Total +Num 1 3 3
2 5 8
NEXT
Print “The result is”, Total 3 -2 6
4 9 15
Average ← Total / 5
5 -3 12
Print “The result is”, Average
The result is 12
Problem 5
• Input 5 number and the sum of positive input number

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

• Find the total number of 1 to 5


Total ← 0
Total ← 0
Total ← Total +1
FOR Count ← 1 TO 5
Total ← Total +2
Total ← Total +Count
Total ← Total +3 NEXT
Total ← Total +4 Count Total
Print Total
Total ← Total +5 1 1
2 3
3 6
4 10
5 15

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
NumNum +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

WHILE condition DO WHILE Num <> 0 DO


Statement 1 Total  Total +Num
Statement 2
INPUT Num
ENDWHILE
ENDWHILE
Write a pseudocode that find the addition of user’s OUTPUT “The Total is”,total
input number until user’s input number is 0.
Total ← 0
Total Num
INPUT Num 0
0
WHILE Num <> 0 The Total is 0
Total ← Total +Num NUM Total Output
INPUT Num 0
ENDWHILE 2 2
OUTPUT “The Total is”,Total 5 7
3 10
0 The Total is 10
Problem 7
• Write pseudocode to input a series of numbers, –1 to finish, and find the total and the
average.
Total ← 0
Count ← 0 // for average
INPUT Num NUM Total count Average Output
0 0
WHILE Num <> -1 DO
0 0 1
Total ← Total +Num
4 4 2
Count ← Count +1
8 12 3
INPUT Num
-1 4 12 4
ENDWHILE
Average← Total / Count
OUTPUT Total
OUTPUT Average
Problem 7

• Input 5 number and the sum of these input numbers.

// 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

Result1 Result 1
Count2
For count2 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.

7 = prime = 7 % 2 <>0 7%3 <> 0 …..7%7 ==0


27 = not prime 27 % 2 <> 0 27%3 =0
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)

OUTPUT “Enter the centimeter value:”


REPEAT
INPUT Cm
PRINT “ Enter Centimeter value:”
IF Cm > 0 INPUT Cm
THEN UNTIL Cm > 0
Meter ← Cm / 100 Meter ← Cm / 100
OUTPUT “The equivalent meter value is”, Meter
PRINT “ Meter value=”, Meter
ELSE

OUTPUT “Your input value is incorrect”


ENDIF Testing

Testing Enter Centimeter value: -20


Enter Centimeter value: -20 Enter Centimeter value: -400
Your input value is incorrect Enter Centimeter value: 400
Meter value= 4
Problem 8

 Write a guessing number game.


 First the program will print that it has a number in its mind, and the user have to guess it.
User have chance to guess number until he/she can guess the right 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

In REPEAT UNTIL, condition check at end / post-test


The iteration occurs at least once
• Write an algorithm which
• Input a number and output the multiplication table for given input
• Number. (1 to 12)
• Input 2
• 2* 1 = 2
• 2* 2 = 4
• ..
• 2* 12 = 24

Print Num , “*”, count,”=“, Res


Problem 7

• Input 5 number and the sum of these input numbers.

// 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

UNTIL Count > 5


Operator math, comparison,logical AND,
Summary OR,NOT
Data Types 5
Selection/ Conditional statement
INPUT ( Input)
IF….THEN….ELSE….ENDIF OUTPUT PRINT ( output)
CASE… OF….OTHERWISE…ENDCASE

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.

Average of each student = TotalMarks of 4 subjects / 4

For One Student TotalMark ← 0


To find ToalMarks of 4 subjects FOR Student ← 1 TO 10
Average ← TotalMark / 4
Input Marks of 4 subject from user PRINT “ Enter Name” PRINT Name
Find TotalMarks INPUT Name PRINT “The average:”,Average
To find Average of student using above equation TotalMark ← 0 NEXT
PRINT “ Enter Physics Mark”
Iteration 4 INPUT Mark
times
Iteration 10 times TotalMark ← TotalMark + Mark
PRINT “ Enter Science Mark”
INPUT Mark
TotalMark ← TotalMark + Mark
.
.
FOR Student ← 1 TO 10
PRINT “ Enter Name” Average ← TotalMark / 4
INPUT Name PRINT Name
PRINT “The average:”,Average
TotalMark ← 0
NEXT Student
FOR Subject ← 1 TO 4

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

4: SubName ← “IT” 2 Sci 80 143

ENDCASE 3 Eng 64 207


REPEAT 4 IT 73 280
PRINT “ Enter”, SubName, “Mark” 5 70
INPUT Mark 2 SU 0
UNTIL Mark > -1 AND Mark < 101 1 Phy 56 56
TotalMark ← TotalMark + Mark
NEXT Subject
A school with 600 students wants to produce some information from the results of the
four standard tests in Maths, Science, English and IT. Each test is out of 100 marks.
The information output should be average mark for each test. All the marks need to be input.
Use pseudocode to write an algorithm to complete this task.
FOR Subject ← 1 TO 4
SubTotal ← 0 Average ← SubTotal / 600
PRINT SubTotal
CASE Subject OF PRINT “The Average Mark:”,Average
Average of each Subject 1: SubName ← “Maths” NEXT
= TotalMarks of 600 students/ 600
2: SubName ← “Science”
3: SubName ← “English”
For One Subject
4: SubName ← “IT”
To find ToalMarks of 600 ENDCASE
Input Marks of 600 students from user FOR Student ← 1 TO 600
Find TotalMarks REPEAT
To find Average PRINT “ Enter”, SubName, “Mark”
INPUT Mark
UNTIL Mark > -1 AND Mark < 101
Iteration 4 times SubTotal ← SubTotal + Mark
NEXT
5000 numbers are being input which should have either one digit (e.g. 5), two digits (e.g. 36), three digits (e.g.
149) or four digits (e.g. 8567).
Write an algorithm, using pseudocode or a flowchart only, which
 inputs 5000 numbers
 outputs the number of one digit, two digits, three digits and four digits
• Meeting ID : 2685926118
• Passcode: jJx8R3
Sunday 5:30
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.
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

Problem Algorithm Pseudocode


Write an algorithm 1. Get Two Numbers Input num1
that calculate the 2. Add two Numbers Input num2
addition of given 3. Display results of res num1 + num2
two numbers. addition Output res
Rewrite the following pseudocode using while looping

INPUT Num
INPUT Num Total  0
Count1
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

You might also like