0% found this document useful (0 votes)
3 views

Part a CHP 4 Problem Solving 1

The document provides an overview of problem solving in computing, emphasizing the importance of defining problems, developing algorithms, coding, and testing. It outlines the steps involved in problem solving, including analyzing the problem, creating algorithms, and implementing solutions using programming languages. Additionally, it discusses the significance of algorithms, characteristics of good algorithms, and methods of representation such as flowcharts and pseudocode.

Uploaded by

Nik Ger
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Part a CHP 4 Problem Solving 1

The document provides an overview of problem solving in computing, emphasizing the importance of defining problems, developing algorithms, coding, and testing. It outlines the steps involved in problem solving, including analyzing the problem, creating algorithms, and implementing solutions using programming languages. Additionally, it discusses the significance of algorithms, characteristics of good algorithms, and methods of representation such as flowcharts and pseudocode.

Uploaded by

Nik Ger
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

CHP-4

Introduction to Problem Solving


Introduction
▪ Computers are used for solving various day-to-day problems and thus
problem solving is an essential skill
▪ It is pertinent to mention that computers themselves cannot solve a
problem. Precise step-by-step instructions should be given by us to solve
the problem
▪ The success of a computer in solving a problem depends on how correctly
and precisely we define the problem, design a solution (algorithm) and
implement the solution (program) using a programming language
▪ Problem solving is the process of identifying a problem, developing an
algorithm for the identified problem and finally implementing the
algorithm to develop a computer program
Steps for Problem Solving

▪ Analysing the Problem

▪ Developing an Algorithm

▪ Coding

▪ Testing and Debugging


Analysing the Problem
▪ It is important to clearly understand a problem before we begin to
find the solution for it

▪ Read and analyze the problem statement carefully in order to list the
principal components of the problem and decide the core
functionalities that our solution should have

▪ It deals with what inputs required, excepted outputs and any


limitations or constraints while solving the given problem
Developing an Algorithm
• The solution is represented in natural language and is called an
algorithm.
• We start with a tentative solution plan and keep on refining the
algorithm until the algorithm is able to capture all the aspects of the
desired solution.
Algorithm
• To complete each activity, we follow a sequence of steps.
• Suppose following are the steps required for an activity
‘riding a bicycle’
1) remove the bicycle from the stand,
2) sit on the seat of the bicycle,
3) start peddling,
4) use breaks whenever needed and
5) stop on reaching the destination
Coding
• We need to convert the algorithm into the format which can be
understood by the computer to generate the desired solution.
• Different high level programming languages can be used for writing
a program
• It is equally important to record the details of the coding procedures
followed and document the solution.
• This is helpful when revisiting the programs at a later stage.
Testing and Debugging
• The program created should be tested on various parameters. The
program should meet the requirements of the user
• It must respond within the expected time
• It should generate correct output for all possible inputs
• In the presence of syntactical errors, no output will be obtained
• In case the output generated is incorrect, then the program should be
checked for logical errors, if any
Why do we need an Algorithm?
• Such a roadmap is nothing but the algorithm which is the building
block of a computer program.
• For example, searching using a search engine, sending a message,
finding a word in a document, booking a taxi through an app,
performing online banking, playing computer games, all are based on
algorithms.
• Writing an algorithm is mostly considered as a first step to
programming.
• The purpose of using an algorithm is to increase the reliability,
accuracy and efficiency of obtaining solutions.
Characteristics of a good algorithm

• Precision — the steps are precisely stated or defined.

• Uniqueness — results of each step are uniquely defined and only


depend on the input and the result of the preceding steps.

• Finiteness — the algorithm always stops after a finite number of


steps.

• Input — the algorithm receives some input.

• Output — the algorithm produces some output.


While writing an algorithm, it is required to
clearly identify the following

• The input to be taken from the user

• Processing or computation to be performed to get the desired result

• The output desired by the user


Representation of Algorithms
• Using their algorithmic thinking skills, the software designers or
programmers analyse the problem and identify the logical steps that need to
be followed to reach a solution.
• Once the steps are identified, the need is to note write down these steps
along with the required input and desired output.
• There are two common methods of representing an algorithm
✓flowchart
✓ pseudocode
• Either of the methods can be used to represent an algorithm while keeping in
mind the following:
✓ it showcases the logic of the problem solution, excluding any
implementational details
✓ it clearly reveals the flow of control during execution of the program
Flowchart — Visual Representation of
Algorithms
• A flowchart is a visual representation of an algorithm
• A flowchart is a diagram made up of boxes, diamonds and other
shapes, connected by arrows.
• Each shape represents a step of the solution process and the arrow
represents the order or link among the steps.
Write an algorithm to find the square of a
number.

Before developing the algorithm, Algorithm to find square of a


number.
let us first identify the input, process
and output Step 1: Input a number and store
it to num
• Input: Number whose square is
required Step 2: Compute num * num and
store it in square
• Process: Multiply the number by
itself to get its square Step 3: Print square
• Output: Square of the number
The algorithm to find square of a number can be
represented pictorially using flowchart as shown
Algorithm For adding two numbers
• Step1 :start
• Step2 : Input two numbers
• Step3 : Process Sum= num1+num2
• Step4 :Display Sum
• Step 5: Stop
The algorithm to
find square of a
number can be
represented
pictorially using
flowchart as shown
Draw a flowchart to solve
the problem of
a non-functioning light
bulb
Pseudocode
• A pseudocode is another way of representing an algorithm.
• The word “pseudo” means “not real,” so “pseudocode” means “not real
code”.
• It is considered as a non-formal language that helps programmers to write
• algorithm.
• It is a detailed description of instructions that a computer must follow in a
particular order.
• It is intended for human reading and cannot be executed directly by the
computer.
• No specific standard for writing a pseudocode exists.
• Following are some of the frequently used keywords while writing
pseudocode
▪ INPUT
▪ COMPUTE
▪ PRINT
▪ INCREMENT
▪ DECREMENT
▪ IF/ELSE
▪ WHILE
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:
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
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
INPUT length
INPUT breadth
COMPUTE
Area = length * breadth
PRINT Area
COMPUTE
Perim = 2 * (length + breadth)
PRINT Perim
Benefits of Pseudocode
• A pseudocode of a program helps in representing the basic
functionality of the intended program.
• By writing the code first in a human readable language, the
programmer safeguards against leaving out any important step.
• Besides, for non-programmers, actual programs are difficult to read
and understand, but pseudocode helps them to review the steps to
confirm that the proposed implementation is going to achieve the
desire output.
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 based on a decision
or even repeat some part for a finite number of times
Sequence
• 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.
• However, statements in an algorithm may not always execute in
• a sequence.
• We may sometimes require the algorithm to either do some routine
tasks in a repeated manner or behave differently depending on the
outcomes of previous steps.
Selection
Let us look at some other examples where decision making is
dependent on certain conditions.
For example,
(i) Checking eligibility for voting. (ii) Let us consider another
Depending on their age, a person will example
either be allowed to vote or not If a student is 8 years old and the
allowed to vote: student likes Maths
✓If age is greater than or equal to 18,
✓ Put the student in Group A
the person is eligible to vote
Otherwise
✓ If age is less than 18, the person is
✓ Put the student in Group B
not eligible to vote
In which group will these students go as per the
condition?

• 8-year-old Ravi who does not like Maths • Group B

• 8-year-old Priti who likes Maths: • Group A

• 7-year-old Anish who likes Maths: • Group B


Conditionals are written in the algorithm as follows:
If <condition> then
steps to be executed
There are situations where we also need to take action when the
condition is not fulfilled

To represent that, we can write:


If <condition> is true then
statements1
statements 2
Let us write an algorithm to check whether a number is odd or even.
• Input: Any number
• Process: Check whether the number is even or not
• Output: Message “Even” or “Odd”

Pseudocode of the algorithm can be written as follows:


PRINT "Enter the Number"
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Number is Even"
ELSE
PRINT "Number is Odd"
Flowchart
• Let us write a pseudocode and draw a flowchart where multiple conditions
are checked to categorize a person as either child (<13), teenager (>=13 but
<20) or adult (>=20),based on age specified:
• Input: Age
• Process: Check Age as per the given criteria
• Output: Print either “Child”, “Teenager”, “Adult”

Pseudocode is as follows:
• INPUT Age
• IF Age < 13 THEN
• PRINT "Child"
• ELSE IF Age < 20 THEN
• PRINT "Teenager"
• ELSE
• PRINT "Adult"
Flowchart
Repetition
• Write pseudocode and draw a flowchart to
• accept 5 numbers and find their average.
• The flowchart representation is shown in Figure 4.10.
• Pseudocode will be as follows:
• Step 1: SET count = 0, sum = 0
• Step 2: WHILE count < 5 , REPEAT steps 3 to 5
• Step 3: INPUT a number to num
• Step 4: sum = sum + num
• Step 5: count = count + 1
• Step 6: COMPUTE average = sum/5
• Step 7: PRINT average
Flowchart
Write pseudocode and draw flowchart to accept numbers till the
user enters 0 and then find their average.

• Pseudocode is as follows:
• Step 1: SET count = 0, sum = 0
• Step 2: INPUT num
• Step 3: WHILE num is not equal to 0, REPEAT Steps 4 to 6
• Step 4: sum = sum + num
• Step 5: count = count + 1
• Step 6: INPUT num
• Step 7: COMPUTE average = sum/count
• Step 8: PRINT average
Verifying Algorithms
• when an algorithm is written, 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.
• 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:
1. Identify any incorrect steps in the algorithm
2. Figure out missing details or specifics in the algorithm
• It is important to decide the type of input value to be used for the
simulation. In case all possible input values are not tested, then the
program will fail.
Comparison of Algorithm
(i) Algorithm requires large number of calculations (means more processing time) as it checks for all the
numbers as long as the divisor is less than the number. If the given number is large, this method will take more
time to give the output.
(ii)Algorithm is more efficient than
(ii) Algorithm as it checks for divisibility till half the number, and thus it reduces the time for computation of
the prime number.
(iii) is even more efficient as it checks for divisibility till square root of the number, thereby further reducing the
time taken. As algorithm
(iv) uses only the prime numbers smaller than the given number for divisibility, it further reduces the
calculations.
But in this method we require to store the list of prime numbers first. Thus it takes additional memory even
though it requires lesser calculations.
Algorithms can be compared and analysed 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 proecssing time required (time complexity) and the memory they utilise (space complexity)
Coding
• An algorithm is finalised, it should be coded in a high-level programming language as selected by the programmer.
• 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.
• An advantage of using high-level languages is that they are portable, i.e., they can run on different types of computers with little
or no modifications.
• Low-level programs can run on only one kind of computer and have to be rewritten in order to run on another type of system.
• A wide variety of high-level languages, such as FORTRAN, C, C++, Java, Python, etc., exist.
• 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.
• There are multiple programming languages available and choosing the one suitable for our requirements requires us to consider
many factors.
• It depends on the platform (OS) where the program will run. To decide whether the application would be a desktop application,
a mobile application or a web application.
• Desktop and mobile applications are generally developed for a particular operating system and for certain hardware whereas the
web applications are accessed in different devices using web browsers and may use resources available over cloud. Besides,
programs are developed not only to work on a computer, mobile or a web browser, but it may also be written for embedded
systems like digital watch, mp3 players, traffic signals or vehicles, medical equipment and other smart devices. In such cases, we
have to look for other specialised programming tools or sometimes write programs in assembly languages.
Decomposition
• The basic idea of solving a complex problem by decomposition is to
'decompose' or break down a complex problem into smaller sub problems
• 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.
• Breaking down a complex problem into sub problems also means that
each sub problem can be examined in detail. Each sub problem can be
solved independently and by different persons (or teams).
• Having different teams working on different sub problems can also be
advantageous because specific sub problems can be assigned to teams who
are experts in solving such problems.
• There are many real life problems which can be solved using
decomposition. Examples include solving problems in mathematics and
science, events management in school, weather forecasting, delivery
management system, etc.

You might also like