Computer System I PUC
Chapter 4: Introduction to Problem Solving
Steps for Problem Solving
In other words, we have to apply problem solving techniques. Problem solving begins with the
precise identification of the problem and ends with a complete working solution in terms of a
program or software.
1. Analyzing the problem: Involves identifying the problem, inputs the program should
accept and the desired output of the program.
2. Developing an Algorithm: The solution to the problem represented in natural
language is called Algorithm. For a given problem, more than one algorithm is
possible and we have to select the most suitable solution.
3. Coding: Different high-level languages can be used for writing the code based on the
algorithm developed.
4. Testing and Debugging: To ensure that the software meets all the business and
technical requirements and works as expected. The errors or defects found in the
testing phases are debugged and rectified and the program is again tested. This
continues till all the errors are removed from the program.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
Algorithm
A finite sequence of steps required to get the desired output is called an algorithm. Algorithm
has a definite beginning and a definite end and consists of a finite number of steps.
Algorithm is required by a programmer to clearly visualize the instructions it requires
to code in order to develop a software.
These also help him to identify the proper inputs and correct outputs.
The purpose of using an algorithm is to increase the reliability, accuracy and
efficiency of obtaining solutions.
Characteristics of a good algorithm
Input – the algorithm receives some input.
Precision – the steps are precisely stated or defined.
Uniqueness – results of each step are uniquely defined and only depend on the input
and the results of the preceding steps.
Finiteness – the algorithm always stops after a finite number of steps.
Output – the algorithm produces some output.
While writing an algorithm, it is required to clearly identify the following:
o The input to be taken from the user.
o Processing or computation to be performed to get the desired result.
o The output desired by the user.
Representation of Algorithms
There are two common methods of representing an algorithm —
i. flowchart ii. pseudocode
1. Flowchart — Visual Representation of Algorithms
o A flowchart is a pictorial representation of an algorithm.
o A flowchart is a diagram made up of boxes, diamonds and other shapes,
connected by arrows.
o Each shape represents a step of the solution process and the arrow represents
the order or link among the steps.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
2. Pseudocode
Algorithms can also be represented as pseudocodes.
It is considered as a non-formal language that helps programmers to
write algorithms.
It is intended for human reading and cannot be executed directly by the
computer.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
Frequently used keywords while writing pseudocode:
INPUT
COMPUTE
PRINT
INCREMENT
DECREMENT
IF/ELSE
WHILE
TRUE/FALSE
Benefits of Pseudocode
Improves readability of the algorithm
It can be used as a rough documentation for increases program understandability of
programs by different programmers.
Write an algorithm to display the sum of two numbers entered by user, using both
pseudocode and flowchart.
Pseudocode for the sum of two numbers will be:
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
c
Write an algorithm to calculate area and perimeter of a rectangle, using both
pseudocode and flowchart.
Pseudocode for calculating area and perimeter of a rectangle.
The flowchart for this algorithm is given in Figure
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
Flow of Control
The flow of control depicts the flow of events as represented in the flow chart. The events can
flow in a sequence, or on branch on a decision or even repeat some part for a finite number
of times.
1. Sequence: It has events occurring in a sequence one after the other without being
dependent on any condition.
i.e., the statements are executed one after another, i.e., in a sequence. Such algorithms
where all the steps are executed one after the other are said to execute in sequence.
2. Selection: Here the flow of control gets branched based on whether a particular
condition evaluates to true or false.
For example,
Checking eligibility for voting. Depending on their age, a person will either be allowed to vote
or not allowed to vote:
• If age is greater than or equal to 18, the person is eligible to vote
• If age is less than 18, the person is not eligible to vote
Conditionals are written in the algorithm as follows:
There are situations where we also need to take action when the condition is not fulfilled. To
represent that, we can write:
In programming languages, 'otherwise' is represented using Else keyword. Hence, a
true/false conditional is written using if-else block in actual programs.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
Let us write an algorithm to check whether a number is odd or even:
3. Repetition
Here a sequence of steps is performed iteratively until some condition is met.
In programming, repetition is also known as iteration or loop. A loop in an
algorithm means execution of some program statements repeatedly till some
specified condition is satisfied.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
Write pseudocode and draw a flowchart to accept 5 numbers and find their average.
Verifying Algorithms
The software designer should make sure that the functioning of all the components
are defined correctly, checked and verified in every possible way.
In the same way, when we have written an algorithm, we want to verify that it is
working as expected. To verify, we have to take different input values and go through
all the steps of the algorithm to yield the desired output for each input value and may
modify or improve as per the need.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
The method of taking an input and running through the steps of the algorithm is
sometimes called dry run. Such a dry run will help us to:
Suppose we develop some software without verifying the underlying algorithm and if there
are errors in the algorithm, then the software developed will not run. Hence, it is important
to verify an algorithm since the effort required to catch and fix a mistake is minimal.
Comparison of Algorithm
Algorithms can be compared and analyzed on the basis of the amount of processing
time they need to run and the amount of memory that is needed to execute the
algorithm.
These are termed as time complexity and space complexity, respectively. The
choice of an algorithm over another is done depending on how efficient they are in
terms of processing time required (time complexity) and the memory they utilise
(space complexity).
Coding
Once an algorithm is finalized, it should be coded in a high-level programming
language as selected by the programmer called coding.
The ordered set of instructions are written in that programming language by
following its syntax.
Syntax is the set of rules or grammar that governs the formulation of the statements
in the language, such as spellings, order of words, punctuation, etc.
A program written in a high-level language is called source code. We need to translate
the source code into machine language using a compiler or an interpreter, so that it
can be understood by the computer.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
Decomposition
The basic idea of solving a complex problem by decomposition is to ‘decompose’ or
break down a complex problem into smaller subproblems.
These sub problems are relatively easier to solve than the original problem.
Finally, the sub-problems are combined in a logical way to obtain the solution for the
bigger, main problem.
MCQS:
1. Which of the following is a way to represent an algorithm:
a. Pseudocode
b. Flowchart
c. None of these
d. Both of these
Answer: a) pseudocode: non-formal language representation of an algorithm
b) flowchart: pictorial representation of an algorithm
c) none of these
b) Both of these
Both option a and b are correct. So the correct answer is d) both of these.
2. Flow of control can be of the following types.
a. Sequential
b. Iterative
c. Derivative
d. Both a and b
Answer: checking the options:
a) Sequential – it is a type of flow of control
b) Iterative – it is a type of flow of control
c) Derivative – it is not a type of flow of control
d) Both a and b
Both option a and b are correct. So the correct answer is d) both a and b.
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA
Computer System I PUC
3. The choice of algorithm is made on the basis of
a. Ease of development
b. User choice
c. Time and space complexity
d. None of these
Answer: The choice of algorithm is made on the basis of time and space complexity.
So , the correct answer is C)
4. Flowchart uses special keywords to represent an algorithm.
a. True b. False
Answer: pseudocode uses special keywords to represent an algorithm. Therefore, the given
statement is false.
5. Flowcharts can have symbols in any order irrespective of the flow of the program
during execution.
a. True b. False
Answer: A flowchart is simply a graphical representation of an algorithm. It shows steps in
sequential order and symbols should be in order of the flow of the program during execution.
Therefore, the given statement is false.
6. Analysis phases identify the problem and various inputs and outputs.
a. True b. False
Answer: Analysis phases involve identifying the problem, inputs the program should accept
and the desired output of the program. Therefore, the given statement is true.
7. ……………… means removing errors that occur during a dry run of the algorithm.
Answer: Debugging
8. ………………..uses sample inputs to check if the algorithm produces the desired output.
Answer: Dry run
………….. keyword in a pseudocode is used to show data being input from the user.
Answer: INPUT/ACCEPT
9. An algorithm which has certain steps being repeated finite number of times is
called…………..
Answer: Iterative
PREPARED BY: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER SCIENCE, CHITRADURGA