LESSON 4 : INTRODUCTION TO PROBLEM SOLVING
1
INTRODUCTION TO PROBLEM SOLVING
CONTENT:
• INTRODUCTION
• PROBLEM SOLVING CYCLE
• DESIGNING ALGORITHM
INTRODUCTION:
• Famous quote by Steve Jobs, “Everyone in this country should learn to program a
computer, because it teaches you think.
PROBLEM SOLVING CYCLE:
• To create efficient and effective programs, we should adopt proper problem
solving methodology.
• Four main steps:
1. Identify and analyse problems.
2. Find solutions and develop algorithm.
3. Code solution in programming language.
4. Test and debug code solution.
2
ANALYZE THE PROBLEM:
1. UNDERSTAND THE PROBLEM WELL:
• Problem is just not that appears but something else causing it.
2. ANALYZE THE PROBLEM:
• Identify processing components.
• Identifying the relationships among processing components.
DEVELOPING THE ALGORITHM:
3. Think of possible solutions
• Think of different ideas for solutions
• Check your ideas for aptness
• Finally, zero on most appropriate solutions
4. Follow modular approach while designing most appropriate solution
• Deciding step by step solution
• Breaking down solution into simple steps
5. Identifying operations for solution
• Minimum number of inputs required.
• Arithmetic and logical operations required.
• Simple and efficient data structures suiting the need.
CODING:
6. Code program using appropriate control structures
• Use appropriate control structures such as conditional or looping
control.
• Think programs efficiency in terms of speed, performance and
effectiveness.
3
TESTING AND DEBUGGING:
7. Test and debug your program
• Finding errors in it.
• Rectifying the errors
8. Complete your documentation
• Consist of detailed description of what program does and how to
use it.
IMPLEMENT AND MAINTAIN:
9. Implement your code
• Implement program foe actual use on site.
10. Maintain your program
• Modifying the programs to remove previously undetected errors.
• Keep the program up-to-date as government or company policies
change.
PROBLEM SOLVING USING DECOMPOSITION:
• The process of breaking down as a big or complex problem into a set of smaller
sub-processes in order to understand a problem or situation better is known as
Decomposition.
• Decomposition involves:
• Dividing a task into a sequence of subtask.
• Identifying elements or parts of a complex system.
• Example:
task of creating mobile app
PROBLEM SOLVING USING DECOMPOSITION:
4
NEED FOR DECOMPOSITION:
• Large problems are extremely harder to solve than smaller problems.
• It is much easier to write two small program than writing one large program.
• Harder and more difficult to solve large program compared to smaller program.
DESIGNING ALGORITHM:
• The set of rules that define how a particular problem can be solved in finite
number of steps is known as algorithm.
• An algorithm is composed of a finite set of steps, each of which may require one
or more operations.
• Type of operations as algorithm can include:
• Each operation must be definite
• Each operation must be effective
• Each operation must be finite.
CHARACTERISTICS OF A GOOD ALGORITHM:
• Characteristics of algorithm are as follows:
1. Precision
2. Uniqueness
3. Finiteness
4. Input
5. output
COMPONENTS OF AN ALGORITHM:
• An algorithm identifies following components:
1. Input: The inputs can be provided by a user or can be self-
contained, reading from a file.
2. Processing: What and how the processing would use inputs to
produce the desired output.
3. Output: The output expected from the algorithm, the objective
of the algorithm.
5
FLOWCHART:
• A flowchart is a pictorial representation of step-by-step solution of a problem.
• Flowchart is not only pictorially represents but also used as an aid in developing
algorithm.
• Flowchart symbols are used to carry different messages and used for different
purposes.
FLOWCHART:
• A flowchart symbols are:
WRITING ALGORITHS:
• Rules for writing algorithms:
IDENTIFIERS:
• Identifiers are the names given to various components of a program.
• While choosing names for identifiers, make sure that they are meaningful and not
unnecessaryily long or sort names.
WRITING ALGORITHS:
ASSIGNMENT:
• The assignment of values to variables is done through assignment statements.
variable <expression> eg:, A 10
• Some use assignment operator to assign values to variables in algorithm.
6
WRITING ALGORITHS:
WRITING ALGORITHS:
FLOWCHART EXAMPLE:
DRAW A FLOWCHART TO ADD TWO NUMBERS:
ALGORITHM FLOWCHART
STEP 1: Start
STEP 2: Get two number N1 and N2
STEP 3: S N1 + N2
STEP 4: Print the result S
STEP 5: Stop
7
PSEUDOCODE:
• Pseudocode is an informal way of describing the steps of a programs solution
without using any strict programming language syntax or underlying technology
considerations.
• Example:
If student’s marks is greater than or equal to 50
display “Passed”
else
display “Failed”
PSEUDOCODE:
PSEUDOCODE ADVANTAGES:
• It uses a language which is easy to understand.
• It highlights the sequence of instructions.
• The structure is clearly visible.
• Can be easily modified.
PSEUDOCODE DISADVANTAGES:
• Not visual tool like flowchart.
• No accepted standard for pseudocode.
• Only be tested on paper.
• Informal representation of an algorithm.
VERFIYING AN ALGORITHM:
• Verifying an algorithm means to ensure that the algorithm is working as planned.
• To verify we should test with sample inputs for which we know the answers.
• A useful mechanism of verifying an algorithm called Dry-Run is used.
DRY-RUN ALGORITHM:
• A Dry-Run is the process of programmer manually working through their code to
trace the value of variable.
• There is no software involved in this process.
• A Dry-Run would involve a print out of the code.
• Trace tables enables the variable values in an algorithm to be recorded as the
algorithm is dry run.
8
DRY-RUN ALGORITHM:
• Characteristics of a Dry-Run are:
1. It is carried out during design, implementation, testing or maintenance.
2. It is used to identify the logic errors in a code.
3. It cannot find execution errors in a code.
COMPARING ALGORITHM:
• In order to decide which algorithm to choose over another, their efficiency will be
the deciding factor.
• Major factor that governs the efficiency of an algorithm are:
1. The time it takes to find solution.
2. The resources which are consumed in the process.
• The two deciding factors for algorithm are:
1. Space-wise efficiency
2. Time-wise efficiency