This chapter discusses problem analysis and control structures in programming, emphasizing the importance of identifying inputs, processes, and outputs for effective problem-solving. It introduces various control structures, including sequence and selection, and provides examples of their application in programming scenarios. Additionally, it highlights the use of mobile and smart devices, showcasing their specific functions and applications.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views16 pages
Programming
This chapter discusses problem analysis and control structures in programming, emphasizing the importance of identifying inputs, processes, and outputs for effective problem-solving. It introduces various control structures, including sequence and selection, and provides examples of their application in programming scenarios. Additionally, it highlights the use of mobile and smart devices, showcasing their specific functions and applications.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
This chapter will cover the following:
Analyzing problems
Control structures
Providing solutions to day-to-day problems
Developing programs using sequence and selection control structures
Applications of mobile and smart devices.
Sep Problem analysis
Analyzing a problem involves dividing the problem into smaller segments
of the problem and examine. This will make it easier to solve the problem.
For example, let us consider an invoice issued by a stationery shop.
To calculate the price
(amount) of each item, number
of items and unit price are
required. The items required to
prepare the invoice, are called
input. Calculating the total
price for each item and the
value of total bill is known as
processing. Price (amount) for
each item and total bill value
are known as output.
Hence, let us analyze
the above bill and identify
input, process and output.
Item
200 pages:
80 pages
‘Carbon pensInput: Item description, number of items, unit price
Process: For an item purchased;
Amount = number of items * unit price
Total bill = total price (amount) of all items
purchased
Output Amount to be paid
In order to develop a computer program, itis essential to identify the inputs,
process and outputs by analyzing a problem. (see figure 4.1)
———
Figure 4.1: Problem Analysis
‘Note - The S| needs analyzing before developing computer
(eal
When analyze a problem, it is possible to identify inputs, process and outputs.
Example 1
Problem: Find the year of birth when a person’s National Identity
Card number is given. nATGSdaa
National Identity Card number
Select the first two digits in the identity card number
Year of birth
mn
Example 2
Problem: Find the cost of purchasing five pens. | <——————=—>
Input: Price of a pen ZA
Process: Calculating cost LE
(total = price of a pen * 5)
Total amount
Figure 42: Pens[> Control structures
A control structure is a block in a
program that analyses variables and chooses
a direction of flow of the program.
control
structures
You have learnt in chapter five of
Grade 7 book that there are three types of
control structures as sequence, selection and repetition.
Note - In inputs and outputs are denoted by the symbol
the processes are by symbol
2.
rts
If the steps are carried out one after the other from the beginning to the end
ina particular consecutive order, it is called a sequence.
The flowchart and the Scratch program below is equivalent to the Example
2in page 40.
when licked
ES Boies
Pee to price perpen *@
ery Toul sno eee
‘Scratch program 1: find the cost of S pens
when the unit price is provided
Flowchart 1: finding cost of five pens
The price of the pen is shown by the price_per_pen variable and the amount
to be paid is shown by the Total variable,
raSelection decides which step(s) are
executed depending on whether a condition of an
algorithm is satisfied or not,
For example, consider a rainy day. If it
rains students are asked to go to the library. If
it docs not rain, students are asked to go to the
playground.
The decision box in flowcharts, is used to ¥U
show the selection control structure (‘See Figure
4.3). Ifthe condition is true, it is directed towards
"Yes", If it is false, it is directed towards "No. The following symbol is used To
indicate the decision making;
Yes
No
Figure 4.3 : Control structure Figure 4.4: Decision on whether its a rainy day
or not
Example 1] Indicate the above example in a flowchart,
Flow chart 2 : Going to the playground or the library according to the weather conditionExample 2 ] Making decisions when playing Snakes and Ladders Game
Snakes and Ladders is a popular game that can
be played by an individual or by a group of players. In
this game, there are number of boxes from beginning
(1) to end (36). Each ladder and snake has two boxes
connected at ends (Refer figure 4.5).
Each time the dice is tossed, the following instructions
are to be followed;
1. Check the number shown on the top face of dice.
2. Shift the counter to face by the number shown on Figure 4.5: Snakes and:
the top face of the dice. Ladders Board
3. If the counter reaches the bottom of a ladder, move it =
to reach the top of the ladder. e y oe
4. If the counter reaches the head of the snake, move ei"
down to where the tail is. counter dice
‘One has to follow these conditions and reach square 36 to become the winner.
Given below is the flowchart relevant to the above example.
c=
[a]
[eee]
Flowchart 3 : Snakes and Ladders game
For free distributionExample 3 ] Consider a scenario where 10 marks are awarded to the correct
answer.
Before marks are awarded, it must be checked whether the answer is
correct or wrong. If the answer is correct, 10 marks are awarded. Incorrect answers
are awarded no marks. The above scenario can be represented by the following
flowchart and control structures (Refer to Flowchart 4).
Flowchart 4 : Offering/Not offering marks for correctincorrect answer
Txample4] Considering the scenario of finding the larger number from two
unequal numbers.
‘Two numbers are given as input, Then the two numbers are compared. If
the first is greater than the second, the output shows as the first number is greater.
Otherwise, the output shows as the second number is greater (Refer to flowchart).
Yes No
[SowR wearin 8] [Siow Bs weacrtan8]
Flowchart 5 : Finding greater numberjon control structure in
[> Selection control structures
Seratch is an Open Source Visual Programming Language produced to
make programming easier to learn, Games, music, animations, interactive stories,
etc. can be created using Seratch, Basic knowledge about Scratch was provided in
the Grade 7 textbook.
‘Two types of selection control structures can be use\
programs;
in developing Scratch
1. IF... THEN instructions block
2. IF... THEN... ELSE instructions block
Table 1 : Selection control structures
For free distribution(Comparison blocks
‘There are instances where a decision has to be taken after comparing two
values in programming. The decision is taken after comparing the two values;
whether one value is greater/smaller/equal than other value.
Instruction blocks shown in the following table are used to compare values.
These blocks output "True” or "False" after comparison,
‘Table 2 : Comparison blocks
ap
cE
Check whether the value on
the left is greater than the one CEE) False
on the right
( Refer to workbook for Activity 4. )Instruction with logical blocks
The following instruction blocks are used to combine comparison
instruction blocks. There are three types of logical blocks as follows;
1, AND
==
3. NOT. ec»
Table 3 : Logical blocks
If the expression is false,
the output is true. If the
expression is true, the
output is false,-3-1 Developing Scratch program with selection control
Baga ties
Example 1] Display current balance after a withdrawal from an account with
Rs.5000/=
In withdrawing money from an account, the current balance is first checked
Money is released only if the current balance is greater than the amount requested.
The amount withdrawn is deducted from the current balance (Refer to Flowchart 6
and Seratch program 2).
oe
Etc tamer reqs Boe Rn
Seratch program 2 : Display account balance
Flowchart 6 : Display account balance
Example 2] Display whether an input number is odd
or even.
Mathematical operator MOD is used to determine
the remainder when a number is divided by another. For
example, 13 MOD 5S is 3. When 13 is divided by 5, the
remainder is 3.
Accordingly, ifa number is divided by 2 and the remainder equals 0, itis an
even number. If the remainder is 1, itis an odd number.eT
Bed enter number ee ens
Ferg even number
say EERE
Scratch program 3 : Test whether a
‘number is even fodd
Flowchart 7: Text whether a number is even’ odd
Example 3] The number of characters in a password is one of indicators of
strength. If the number of characters is less than 8, it is a weak
password. Ifit is more than 8, itis a strong password (see Flowchart
8 and Scratch program 4),
Seratch programming 4 : Display whether a
password is weak or strong
Flowchart 8 : Display whether a passwords is weak or strong.
For free distributionThe Scratch program for Snakes and Ladders game is given below; (Refer to
Seratch programs 5)
EZ] Whats the vale ofthe upward face ofthe dee Ba
eae a x ed
BE tac anannetononst ver? EROS
| az:
Tic corer nic rp crac nate Eek « Ee
FJ see dorset tm ottenake
Scratch program 5 : Snakes and Ladder game
Analysis of the program
Click on fatto start,
First, display "Toss the dice",
yen
Ask "What is the value?" and get the answer.
Assign the value obtained in step 03 above to the variable.
we
Declare 4 seconds to bring the counter forward according to the value of the
dice,
6. Obtain "Yes" or "No" answer for the question, "Did it land at bottom of ladder?"
7. If the answer is "Yes", then move the counter to the square at the top of the
ladder.
8. Display, "Did the counter come to the head of a snake?"
9. If the answer is "Yes", then move the counter to snake's tail.Refer to workbook for Activities in 4.6, 4.7, 4.8 and 4.9
Oman eu muatcate
(Mobile and smart devices
Various applications are available for mobile and smart devices. These
applications usually carry out tasks accurately and efficiently. Each mobile and
smart device is developed for a specific function and they can be used according to
the user requirements (See Figure 4.6).
‘Smart watch Smart TV
Tablet Smart camera
/ Smart phone
Figure 4.6 : Examples of mobile and smart devices
Smart sunglasses
egy
Drone camera
Tablets and laptops are widely used as general purpose machines. Other
devices are meant for a specific functions. Therefore, it is necessary to determine
whether the selected mobile device suits the intended purpose.
ryTable 4 : Smart devices and applications
Mobile device
Examples of use
Laptop computer
Asa mobile device for general computer applications
Tablet computer
To surf the Internet and to take photos, ete.
Smart mobile
phone
Telephone conversations, SMS and MMS messaging,
taking photos, recording audio and video clips, surfing
Internet, sending e-mails, etc.
Smart television
Managing and recording television programmes, e-mail
and Internet,
Smart camera
Taking photos, recording audio/video clips and sending
them to other smart devices
Smart wristwatch
Displays time, sends SMS, sets alarm, accesses the
Internet, ete.
Mobile spectacles
Watch scenery in 3D form, listen to audio, taking photos,
record video clips, etc.
Drone camera
A remotely controlled, mini helicopter used to obtain
aerial pictures or videos
Applications of mobile and smart devices
|
Many application software for mobile and smart devices are available on the
Internet. Some application software can be downloaded free of charge while others
have to be purchased.
These downloaded software can be used after installing in
the smart devices. The following shows some examples for application software for
mobile and smart devices. (See Figure 4.7)Sky map
Road map.
Compass ‘Language
translator
Voice recorder eg Chess Game
Dictionary
Figure 4.7 : Examples of the applications of mobile and smart devices
Table S : Mobile and smart devices and their uses
Smart device Examples of use
‘Compass finds the orientation
Route map directs using GPS (finding routes), finds distance between
two places, identifies traffic, ete.
‘Sky map. points the smart device towards a star or planet on the sky
and see the details such as name, location, ete.
QR Code Reader | obtains information by scanning the QR code
Language translator _| translates text in one language to another
‘Chess game computer play as the opponent of Game
Sinhala dictionary __| finds English term for Sinhala
Tape recorder records and playback sound
( Refer to workbook for the Activity 4.10 )
For:
EeComputer programs are developed to accept input, process data and
produce output. Generally, the algorithm is written first and then the
algorithm is converted to a program.
There are three types of control structures that can be used in an algorithm;
1. Sequence
2. Selection
3, Repetition
A sequence follows steps in the algorithm the one after the other.
In selection, the program selects the cause of action based on whether
condition is satisfied or not. Scratch programming uses "if then" and "if
then else” control structures for selection.
Scratch uses three types of comparison blocks.
GD GD Ca
Scratch uses three types of logical blocks.
The "Repetition" control structures, will be taught in a future lesson.