265x Filetype PDF File size 2.48 MB Source: www.e-thaksalawa.moe.gov.lk
1
1 Programming
Pr ogramming
The chapter concerns the following;
The chapter concerns the following;
How to analyze a problem and develop an algorithm
² How to analyze a problem and develop an algorithm
² Control structures and their use
Control structures and their use
² 'UDZLQJÀRZFKDUWVZULWLQJSVHXGRFRGHVDQGFRQYHUVLRQVEHWZHHQWKHP
² Finding alternate solutions to a problem
Finding alternate solutions to a problem
Programming in Pascal
² Programming in Pascal
Evolution of programming languages
² Evolution of programming languages
1.1 Analyzing a pr oblem
1.1 Analyzing a problem
The raw materials that are used to solve a problem are known as the ©input©.
The raw materials that are used to solve a problem are known as the 'input'.
The result obtained after solving a problem is known as the ©output©. Converting
The result obtained after solving a problem is known as the 'output'. Converting
input to output is called the 'process'. A process takes place step by step and it is
input to output is called the ©process©. A process takes place step by step and it is
YHU\LPSRUWDQWWRXQGHUVWDQGWKHRUGHURIWKHSURFHVV:KHQDQDO\]LQJDSUREOHP
WKHLQSXWSURFHVVDQGRXWSXWDUHLGHQWL¿HGVHSDUDWHO\
Example -
Example - Preparing a letter which can be posted.
Pr oblem 1 : Preparing a letter which ca n be posted.
Problem 1 :
Input : A sheet of paper suitable to write the letter on and a pen
Input : A sheet of paper suitable to write the letter on and a pen
An envelope and a stamp
An envelope and a stamp
Glue
Glue
Pr ocess : 1. Writing the letter
Process : 1. Writing the letter
2. Folding the letter and putting it into
2. Folding the letter and putting it into
the envelope
the envelope
3. Pasting the envelope
3. Pasting the envelope
4. Writing the recipient's address on the
4. Writing the recipient©s address on the
envelope
envelope
5. Sticking the stamp
5. Sticking the stamp
Output : A letter ready to be posted.
Output : A letter ready to be posted.
Note: Steps No. 4 and 5 in this process can be interchanged.
Note: Steps No. 4 and 5 in this process can be interchanged.
+RZHYHUWKHRWKHUVWHSVVKRXOGEHIROORZHGLQWKHRUGHU
indicated.
indicated. 1
For free distribution 1
For free distribution
Making a cup of tea
Pr oblem 2 : Making a cup of tea
Problem 2 :
Input : Tea leaves
Input : Tea leavesVXJDUKRWZDWHU
Pr ocess : 1. Putting tea leaves in the strainer
Process : 1. Putting tea leaves in the strainer
2. Pouring hot water to the cup through
2. Pouring hot water to the cup through
the strainer
the strainer
3. Adding some sugar to the cup
3. Adding some sugar to the cup
4. Stirring it well with a spoon
4. Stirring it well with a spoon
7HVWLQJIRUWDVWHWDNLQJDVPDOOVLSIURPWKHFXS
,IWKHWDVWHLVQRWVDWLVIDFWRU\JRWRVWHSDQGUHSHDWVWHS
Nos. 4 and 5
Nos. 4 and 5
Output : A cup of tea
Output : A cup of tea
Dividing 40 page and 80 page books from a parcel of
Problem 3 : Dividing 40 page and 80 page books from a parcel of
Pr oblem 3 :
books between two siblings - Sanduni and Anupama.
books between two siblings - Sanduni and Anupama.
Input : The parcel of books
Input : The parcel of books
Process : 1. Opening the book parcel
Pr ocess : 1. Opening the book parcel
2. Taking a book out from the parcel
2. Taking a book out from the parcel
,ILWLVDSDJHERRNJLYLQJLWWR
Sanduni
Sanduni
,ILWLVDSDJHERRNJLYLQJLWWR
Anupama
Anupama
5. Go to Step No. 2 till all the books are taken out of the
5. Go to Step No. 2 till all the books are taken out of the
parcel
parcel
Output : Sanduni getting 40 page books
Output : Sanduni getting 40 page books
Anupama getting 80 page books
Anupama getting 80 page books
Problem 4 :
Pr oblem 4 : Adding two numbers
Adding two numbers
Input : Two numbers
Input : Two numbers
Process : Adding the two numbers
Pr ocess : Adding the two numbers
Output : Total
Output : Total
Problem 5 :
Pr oblem 5 : Finding the area of a rectangle
Finding the area of a rectangle
Input : Length and width of the rectangle
Input : Length and width of the rectangle
Process : Area = Length x Width
Pr ocess : Area = Length x Width
Output : Area
Output : Area
2
2 For free distribution
For free distribution
Problem 6 : Finding the larger number between two numbers
Pr oblem 6 : Finding the lar ger number betw een two numbers
Input : Two numbers
Input : Two numbers
Pr ocess :
Process : &RPSDULQJWKHWZRQXPEHUV¿QGLQJWKHODUJHURQH
Output : Larger number
Output : Lar ger number
Finding whether a number is odd or even
Problem 7 : Finding whether a number is odd or even
Pr oblem 7 :
Input : Number
Input : Number
Pr ocess : Dividing the number by 2
Process : Dividing the number by 2
Deciding that the number is even if
Deciding that the number is even if
the remainder = 0
the remainder = 0
Deciding that the number is odd if the remainder = 1
Deciding that the number is odd if the remainder = 1
Output : Indicating whether the number is odd or even
Output : Indicating whether the number is odd or even
Activity
Activity
,GHQWLI\WKHLQSXWSURFHVVDQGRXWSXWUHODWHGWRGLYLGLQJWRIIHHV
among 20 people.
among 20 people.
,GHQWLI\WKHLQSXWSURFHVVDQGRXWSXWLQPDNLQJDNLWH
What are alternative solutions?
What ar e alternative solutions?
,IWKHUHLVPRUHWKDQRQHVROXWLRQWRDJLYHQSUREOHPVXFKVROXWLRQVDUH
called alternative solutions. Such solutions depend on the nature of the problem.
called alternative solutions. Such solutions depend on the nature of the problem.
3
For free distribution 3
For free distribution
Example
Example
Imagine you come to school by school bus. If the bus breaks down on your way to
Ima gine you come to school by school bus. If the bus breaks down on your way to
VFKRRO\RXFHUWDLQO\ZLOOWKLQNRIRWKHUDOWHUQDWLYHZD\VWRUHDFKVFKRRO7KXV\RX
will think of alternative solutions to reach school.
will think of alternative solutions to reach school.
1. Coming to school by another school bus which goes to your school
1. Coming to school by another school bus which goes to your school
2.
2. ,I\RXKDYHPRQH\UHDFKLQJWKHVFKRROE\&7%RUSULYDWHEXV
3. Walking to school along the road
3. Walking to school along the road
4. Walking to school using a short-cut
4. Walking to school using a short-cut
5.
5. ,I\RXKDYHDZD\WRLQIRUP\RXUSDUHQWVJHWWKHLUVXSSRUWWRUHDFKWKH
school
school
6. Reaching the school by car or motor bike with the support of a trustworthy
6. Reaching the school by car or motor bike with the support of a trustworthy
person
person
You may select a good solution out of these if it is mandatory to go to school that
You may select a good solution out of these if it is mandatory to go to school that
day .
day.
7KXVLIWKHUHDUHPDQ\VROXWLRQVVHWWRDSDUWLFXODUSUREOHPLWLVVXLWDEOHWR
consider these and select an appropriate solution.
consider these and select an appropriate solution.
All the solutions pertaining to a problem are called solution space . In computer
All the solutions pertaining to a problem are called solution space. In computer
SURJUDPPLQJ DOVR YDULRXV VROXWLRQV VKRXOG EH LGHQWL¿HG DQG DQ DSSURSULDWH
VROXWLRQVKRXOGEHVHOHFWHG7KHQZHFDQFUHDWHDVKRUWVLPSOHSURJUDP
Example 1
Example 1
/HWXVH[DPLQHWKHVROXWLRQVSDFHWR¿QGWKHSHULPHWHURIDUHFWDQJOH
/HWXVDQDO\]HWKHLQSXWSURFHVVDQGRXWSXWUHODWHGWRWKLVSUREOHP
Input : Length and width of the rectangle
Input : Length and width of the rectangle
Process : Calculating the perimeter
Process : Calculating the perimeter
Output : Indicating the perimeter
Output : Indicating the per imeter
Let us examine the solution space to calculate the perimeter.
Let us examine the solution space to calculate the perimeter .
1st solution Perimeter = length + width + length + width
1st solution Perimeter = length + width + length + width
2nd solution Perimeter = length × 2 + width × 2
2nd solution Perimeter = length × 2 + width × 2
UGVROXWLRQ 3HULPHWHU OHQJWKZLGWKî
2XWRIWKHVHVROXWLRQVDSHUVRQZKRKDVNQRZOHGJHRQO\RIDGGLWLRQFDQVHOHFWWKH
1st solution as the most appropriate. A person who has knowledge of multiplication
1st solution as the most appropriate. A person who has knowledge of multiplication
and addition can select the 3rd solution out of the 2nd and 3rd solutions as the most
and addition can select the 3rd solution out of the 2nd and 3rd solutions as the most
DSSURSULDWH7KHUHDVRQIRUWKLVLVLWKDVWKHPLQLPXPQXPEHURIDGGLWLRQVDQG
multiplications.
multiplications.
4
4 For free distribution
For free distribution
no reviews yet
Please Login to review.