Chapter 1 Computer Science 10 Class Federal Board
Chapter 1 Computer Science 10 Class Federal Board
PROGRAMMING TECHNIQUES
Reading
UNIT INTRODUCTION
A computer is a general-purpose electronic
machine invented to help people solve various
problems. Computer must be programmed by
human beings to perform various tasks. Various
programming techniques are used for solving
problems on computer. This unit explains how to
write algorithms and draw flowcharts which are
essential to develop a computer program.
Problem 1: A person is unhappy with a product or service he/she has purchased from a
company.
After reading the above statement, we understand that a customer has purchased any product
or service from a Company and now the customer is not satisfied with it. This statement defines
the problem clearly and requires the solution. It is a general type of problem.
Problem 2: Finding average marks of a student.
It is clear from the problem statement that average marks of a student have to be found. This is
numeric problem.
1.1.2 ANALYZING THE PROBLEM
At this stage of problem solving, the programmer investigates the problem and gathers as much
information as possible to find a solution. The following questions are to be asked to analyze the
problem.
• Is it possible to solve the problem on a computer?
• What is to be done to find the solution of the problem?
• What is the proper sequence of steps to solve the problem?
• What are the inputs and what output is required?
• How many solutions are possible?
• Which solution is the best and why?
• How solution will be implemented?
To analyze the Problem 1 (given above), the following points are to be kept in mind and
some information is to be gathered.
1 Programming Techniques
Know the outcome you want:
• Think about what is important to you and what you want to achieve. For example, do you
want a refund, a replacement?
• Would you like them to provide the service again, this time meeting agreed standards?
• The more specific you are the more likely it is that you will get the outcome you want.
Prepare for the discussion:
• Find out about your legal rights.
• Write down exactly what your complaint is, including details such as dates and times.
• Gather any relevant paperwork, e.g. advertisement that misled you, or a quote that the
trader gave you.
Find out what the organisation’s complaint process is and follow this:
• Have a look on their website or on any documentation they have given you.
To analyze Problem 2 the following information is required.
• What are the subject marks of the student in each subject?
• How many subjects are there?
1 Programming Techniques
1.2 ALGORITHM
1.2.1 THE ALGORITHM
Algorithm means method, procedure, technique or plan. Algorithm is a step-by-step problem
solving method that is easy to understand and follow. It is a set of steps that clearly defines a
sequence of operations to solve a problem.
1.2.2 ROLE OF ALGORITHM IN PROBLEM SOLVING
Algorithm plays an important role in computer programming. Computer programming is the
process of taking an algorithm and coding it in a programming language. Formulating an
algorithm is the first step for developing a computer program.
1.2.3 MEASURING EFFICIENCY OF AN ALGORITHM
The efficiency of an algorithm is the property which relate the algorithm to the amount of
computational resources used in it. An algorithm must be analyzed to determine its resource
1 Programming Techniques 13
usage 1 Programming Techniques (e.g. time, memory and storage space). For maximum efficiency
we wish to minimize resource usage. However, the various resources cannot be compared
directly, so which of two algorithms is considered to be more efficient often depends on which
measure of efficiency is considered the most important, e.g. the requirement for high speed,
minimum memory usage is considered.
There may be several algorithms for solving a particular problem. How fast a problem can
be solved, depends on efficient algorithm. Therefore, analysis of algorithms is very important to
compare algorithms and conclude that which one is better than the other. Analyzing the efficiency
of algorithms means comparing efficiency of different methods of solutions of a problem.
1.2.4 ALGORITHMS FOR SOLVING PROBLEMS
This section presents algorithms to solve various mathematical problems. This develops skills
in finding solutions of problems.
Problem 1: Find the sum, product and average of five given numbers.
Planning the Solution:
Input: Five given numbers
Required Output: Sum, product and average of five numbers
Processing: Addition, multiplication and division of numbers
Algorithm:
Step 1: Start
Let the five numbers be A=2, B=5, C=8, D=4 and E=12
Step 2: FIND the sum (SUM)
SUM=A+B+C+D+E
Step 3: FIND the product (PROD)
PROD=A*B*C*D*E
Step 4: FIND the average (AVG)
AVG=SUM/5
Step 5: Output SUM, PROD, AVG
Step 6: Stop
Problem 2: Find the largest of three unequal numbers.
Planning the Solution:
Input: Three unequal numbers
Required Output: The largest of three numbers
Processing: Comparison of each number with the other two one by one Algorithm:
Step 1: Start
Let the three numbers be A=10, B=20 and C=30
Step 2: Check if A is the largest number
14
1 Programming Techniques
Let the height, H be 10 and the base, B be 15
Step 2: CALCULATE the area, A
A = (B*H)/2
Step 3: Output A
Step 4: Stop
Problem 6: Read marks (M) and print letter grade according to the following
scheme.
Marks Letter Grade
--------- ----------------
M≥80 and M≤100 A1
M≥70 and M<80 A
M≥60 and M<70 B
M≥50 and M<60 C
M≥40 and M<50 D
M≥33 and M<40 E
M≥0 and M<33 Ungraded
Planning the Solution:
Input: Marks
Required Output: Letter Grade
Processing: Check in which range the marks fall and accordingly print the letter grade.
Algorithm:
Step 1: Start
Let the marks, be M
Step 2: Compare the marks
IF M≥80 THEN print “Grade A1” otherwise GOTO Step 4
Step 3: GOTO Step 13
Step 4: Compare the marks
IF M≥70 THEN print “Grade A” otherwise GOTO Step 6
Step 5: GOTO Step 13
Step 6: Compare the marks
IF M≥60 THEN print “Grade B” otherwise GOTO Step 8
Step 7: GOTO Step 13
Step 8: Compare the marks
IF M≥50 THEN print “Grade C” otherwise GOTO Step 10
Step 9: GOTO Step 13
1 Programming Techniques 15
Problem 3: Find acceleration of a moving object for given mass and the force
applied.
Planning the Solution:
Input: Mass and the Force
Required Output: Acceleration of moving object
Processing: Divide the force by the mass
Algorithm:
Step 1: Start
Let the mass (M) be 50 and the force (F) be 12
Step 2: CALCULATE the acceleration (A)
A=F/M
Step 3: Output A
Step 4: Stop
Problem 4: Find the volume of a cube.
Planning the Solution:
Input: Length of side of cube
Required Output: Volume of cube
Processing: Side of cube raised to the power of three
Algorithm:
Step 1: Start
Let the length of one side of cube, L be 12
Step 2: CALCULATE the volume, V
V=L3
Step 3: Output V
Step 4: Stop
Problem 5: Find the area of a triangle when the lengths of height and base
are given.
Planning the Solution:
Input: Height and base of triangle
Required Output: Area of triangle
Processing: Multiply height with base and divide by two
Algorithm:
Step 1: Start
16
Step 12: Compare the marks
IF M≥33 THEN print “Grade E” otherwise print “Ungraded”
Step 13: Stop
Algorithm:
Step 1: Start
Let the temperature in Fahrenheit, F be 100
Step 2: CALCULATE temperature in Celsius (C)
C = 5/9(F-32)
Step 3: Output C
Step 4: Stop
18
1 Programming Techniques
Problem 13: Find factorial of a given number.
Planning the Solution:
Input: Given Number
Required Output: Factorial of given number
Processing: Find the product of all the numbers from 1 to the given number. Algorithm:
Step 1: Start
Let the number, N be 5
Step 2: Initialize loop variable K and factorial F to 1
K=1, F=1
Step 3: CALCULATE the product
F=F*K
Step 4: Increment K by 1
K=K+1
Step 5: Check the value of K
IF K≤N THEN GOTO Step 3 otherwise GOTO Step 6
Step 6: Output F
Step 7: Stop
1.3 FLOWCHART
1.3.1 THE FLOWCHART
Flowchart is a diagrammatic representation of algorithm. It describes what operations are
required to solve a given problem.
1.3.2 IMPORTANCE OF FLOWCHART IN SOLVING A PROBLEM
Flowchart illustrates the sequence of operations to be performed to solve a problem in the form
of a diagram. Computer programmers draw flowcharts before writing computer programs. It
provides an easy way to analyze and find solutions of problems. Once, the flowchart is drawn, it
becomes very easy to write the program in any high level language. It is very helpful in
communicating the problem solving method to other people. It also helps in finding and removing
errors in computer programs.
1.3.3 STEPS FOR DRAWING FLOWCHART
The flowchart developer must determine the following requirements for the given problem
or algorithm before drawing a flowchart.
• Input to the flowchart
• Type of processing required
• Decisions to be taken
1 Programming Techniques 19
Start/Stop Decision
Input/Output Connector
On-Page Off-Page
calculations appear inside the processing symbol, such as "SUM= A + B". Variables are also
initialized inside the process symbol, such as “K=1”.
Decision Symbol: A diamond shaped symbol represents decision in a flowchart and it contains
a condition. If the condition is true, the path marked YES is to be followed. If the condition is
false, the path marked NO is to be followed. The words TRUE or FALSE can also be used
instead of YES or NO.
Connector Symbols: These symbols are used to connect one part of a flowchart to the other
on the same page (On-Page connector) or on the new page (Off-Page connector).
1.3.5 FLOWCHARTS TO SOLVE PROBLEMS
In this section algorithms presented in the previous section will be converted into flowcharts for
better understanding of the logic of problem solving.
Flowchart 1: Flowchart to find the sum, product and average of five numbers
Before drawing the flowchart to solve this problem, the input to the flowchart, type of calculation
to be performed and the required output must be defined. From the statement of the flowchart,
it is clear that sum, product and average are to be calculated and given are five numbers.
Flowchart to find sum, product and average of five numbers is shown in Fig.1-1.
START
INPUT A,B,C,D,E
SUM=A+B+C+D+E
PROD=A*B*C*D*E
AVG=SUM/5
OUTPUT SUM,PROD,AVG
STOP
START
INPUT A,B,C
YES
A>B AND A>C ? OUTPUT A
NO
YES
NO
STOP
OUTPUT C
Flowchart 3: Flowchart to find acceleration of a moving object given mass and the force
applied
START
ACCELERATION=FORCE/MASS
OUTPUT ACCELERATION
STOP
START
INPUT L
V = L3
OUTPUT V
STOP
Flowchart 5: Flowchart to find the area of a triangle when the lengths of height and base
are given
START
INPUT B, H
A=(B*H)/2
OUTPUT A
STOP
START
INPUT N, E
P=1, K=1
P=P*N
K=K+1
YES
K ≤ E?
NO
OUTPUT P
STOP
START
K=1
OUTPUT K
K=K+2
YES
K < 100 ?
NO
STOP
START
K=27
OUTPUT K
K=K-3
YES
K ≥ -6 ?
NO
STOP
START
SUM=0, K=2
SUM=SUM+K
K=K+2
YES
K ≤ 100 ?
NO
OUTPUT SUM
STOP
START
INPUT N
K=1
P=N*K
OUTPUT P
K=K+1
Yes
K ≤ 12?
No
STOP
START
INPUT F
C = 5/9(F-32)
OUTPUT C
STOP
START
INPUT N
F=1, K=1
F=F*K
K=K+1
YES
K≤N?
NO
OUTPUT F
STOP
Key Points
Computer is a general- purpose electronic machine invented to help people solve various
problems.
Candid solutions of a problem are all the possible solutions for a problem.
Algorithm is a step by step problem solving method that is easy to understand and follow. It
is a set of steps that clearly defines a sequence of operations to solve a problem.
Flowchart is a diagrammatic representation of algorithm.
Sequential Structure refers to the execution of operations in the order in which they appear.
Repetition Structure o r loop provides a way to repeat one or more operations as many
times as required to solve a problem.
Selection Structure allows a choice among various options while solving a problem
1 Programming Techniques 29
Extensive Questions
Q3. Describe the steps involved in problem solving.
Q4. Write an algorithm to calculate the area of a rectangle for given breadth and length.
Q5. Write an algorithm that inputs length in inches and calculates and prints it in centimeters.
Q6. Write an algorithm that inputs marks and prints the message “PASS” or “FAIL”. Passing
marks are 33.
Q7. Write an algorithm to find the sum of given sequence.
SUM = 20 + 25 + 30 + 35 + 40 + 45 + 50 + 55 + 60
Q8. Write an algorithm to find the product of given numbers.
PRODUCT = 1x3x5x7x9x11x13x15
Q9. Write an algorithm to print multiplication table of a number in reverse order.
Q10. Convert the algorithms of questions Q4 to Q9 to flowcharts.