Lecture - 02 - Problem Solving and Algorithm
Lecture - 02 - Problem Solving and Algorithm
1
Introduction to Programming
Lecture 02: Problem Solving and Algorithms
2 We will learn about:
What is Problem Solving?
How to solve a problem?
Problem Solving Strategies
Solving problem with computer
Software Development Method of Problem
Solving
Steps in the Software Development Method
Design & representation of algorithms
Programming errors & debugging
Program verification & testing
Flowchart & Pseudocode
3 What is Problem Solving?
Definition:
Problem solving is the process of transforming the
description of a problem into the solution of that
problem by using our knowledge of the problem
domain and by relying on our ability to select and use
appropriate problem-solving strategies, techniques
and tools.
4 Problems Faced in Everyday Life
Solving
Guess and Check (Try an Error)
Start at the end (Work Backward)
Strategies Divide and Conquer
Look for a Pattern
7 Guess and Check (Try and Error)
Solution
I'll guess his brother owns 8 games.
That means Prince Carl owns 11 games. That's a total of 19 games.
My guess is too high.
I'll guess again. This time I'll guess his brother owns 6 games.
That means Prince Carl owns 9 games. That's a total of 15 games.
My guess is right.
His brother owns 6 games.
9 Start at The End (Working Backward)
Sometime to solve some problem, you may need to undo some of the key actions in the
problem.
Also known as bottom-up approach.
Example:
The castle kitchen servants brought in 4 pies left over from the feast. 12 pies were eaten at
the feast. Queen Mab took 2 home with her. How many pies did the servants bring into the
feast at the beginning?
10 Start at The End (Working Backward)
Solution
First, I'll account for all the pies that were eaten or taken home.
12 + 2 = 14
Then I'll add the 4 pies that were left over.
14 + 4 + 18
Therefore, there must have been 18 pies at the start of the feast.
11 Divide and Conquer
Solution
Shelf 1 2 3 4 5 6
Loaves 1 3 5 7 9 11
15 Other Strategies
Drawings/modeling
Logical Reasoning
Extra Information
TIPS:
Strategy is only to help you to solve problem. Most
important thing is to keep your mind cool and try
to find ways to overcome the difficulties!!
16 Computers use algorithmic solutions:
Solving
Results – outcome of running the program
Problem
with Testing – Are the outcomes what we expected
and correct
Computer
Documentation
Interpretation of results.
18
Step 6: Documentation
20
Requirement
Analysis
Specification
Basic Steps in
Implementation &
Design
Deployment Software/Application
Development
Testing Documentation
21
Requirement Specification
Correctness
It must be correct and must solve the
problem for which it is designed.
Finiteness
It must execute its steps and terminate
in finite time.
An algorithm that never terminates is
unacceptable.
28 Algorithm
Read status
• if
• status is equal to 1
• print “on”
• else if
Another • status is equal to 0
Example of • print “ Off”
• else
Pseudocode • print “Error in status code”
• end if
End
38 Algorithm in Real-Life
Start
Preheat the oven at 180oC
Prepare a baking pan
Beat butter with sugar
Mix them with flour, eggs and
essence vanilla
Pour the dough into the
baking pan
Put the pan into the oven
End
40 Algorithm in Real-Life
Start
Prepare the breakfast
End
42 Algorithm in Real-Life
1.Start
2. Prepare a Breakfast
2.1. Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
OR some of you 2.1.2 Prepare tuna paste
may write the 2.2. Prepare some chips
pseudocode in more 2.2.1 Cut potatoes into slices
detail on preparing a 2.2.2 Fry the potatoes
breakfast 2.3. Make a cup of coffee
2.3.1 Boil water
2.3.2 Add water with sugar and coffee
3. End
43
Start/Stop Decision
Process Connector
Sub-module / Function
(Barred Rectangle)
45 Flowchart Symbols – Start / Stop
Processes are labeled with the statement the task to be performed, for
example:
• For example, getting values from the user or printing something on the
screen.
• Input/output symbols are labeled with the statement that receives the input
or generates the output, which could also include opening files and devices.
Examples
get stdNumber
get stdNumber
48 Flowchart Symbols - Decision
• Decisions are labeled with the condition that they are testing, for example:
numRecords > 10
foundMatch = true
Yes No
numRecords > 10
49 Control Structures
Sequence structure
Selection structure
Yes
then-part
condition
else-part
No
-------------------------------------------------------------------
Yes
then-part
condition
No
50 Control Structures
Repetition structure
Yes
loop-body
condition
No
51 Flowchart Symbols – Connector
Used to connect two segments of flowchart that appear on the same
page.
This is done when your flowchart runs to the bottom of the page and we are
out of room: end it with an on-page connector and then place another on-page
connector in a free spot on the page, and continue the flowchart from that
connector.
A
53 Flowchart Symbols - Decision
Used to specify a function/module call or a group of related statements.
Start Submodule
Example
Submodule
Return
End
Communication
54
• Flowcharts are better way of communicating the logic of a system to all
concerned.
Effective analysis
• With the help of flowchart, problem can be analyzed in more effective way.
Proper documentation
The benefits • Program flowcharts serve as a good program documentation, which is
Efficient Coding
flowcharts • The flowcharts act as a guide or blueprint during the systems analysis and
program development phase.
Proper Debugging
• The flowchart helps in debugging process.
Calculate and display the price of apple(s).Lets assume the weight in kg and price
per kg are given.
Input
Quantity
Input
Price_per_kg
Output
Price
End
59
Programming Error
• Another challenge that awaits is program debugging.
Debugging is defined as the process of finding and correcting errors in computer programs.
• No matter how careful you are as a programmer, most programs you write will contain
errors. Either they won’t compile, or they won’t execute properly.
• This situation is something that happens very frequently to every programmer. You should
take program debugging as a challenge, develop your debugging skills, and enjoy the
process.
• Syntax errors
• Logic errors
• Run-time errors
60 Programming Error
Syntax error:
is violation of syntax rule, which define how the elements of a
programming language must be written.
They occur during the implementation phase and are detected by the
compiler during the compilation process.
Another name for syntax error is compilation error.
Logic error:
occur during the analysis, design, and implementation phases.
We may choose an incorrect method of solution for the problem to
be solved, mistakes in translating an algorithm into a program or
design erroneous data for the program.
Run-time error:
are detected by the computer while the program is being executed.
They are caused by program instructions that require the computer
to do something illegal, such as attempting to store inappropriate
data or divide a number by zero.
61 Debug
Debugger
is a computer program that is used to test and debug other
programs.
Usually comes together with compilers
Debugger will help programmer by provide list of error in
compiling the program
Still depends on programmers to solve the problem!!
63 Debug
Debugging technique
Probe
Put an external variable inside the program and try to
force the program to output result in the middle.
Good to trace modular logic error or data error.
Trace
Back-tracking or front-tracking.
Go step-by-step (or line-by-line) to detect error.
Good in tracking logic error
64 Testing and Verification
• In this phase, the main objective is to convince yourself and eventually your clients that the
program will do what it is expected to do. In other words, you will want to verify that your
program is correct.
Program verification is the process of ensuring that a program meets user requirements.
• One of the techniques that can be used for program verification is program testing.
• A program must be tested using a sufficiently large sample of carefully designed test data sets
such that every logical path in the program is traversed at least once.
• You must continue to test your program until you are sure that all statements in it are
functioning correctly.
Documentation
Program documentation consists of these
elements:
A concise requirements specification
Descriptions of problem inputs, expected
outputs, constraints, and applicable formula
A pseudocode and flowchart for its algorithm
A source program listing
A hardcopy of a sample test run of the
program
A user’s guide explaining to non-programmer
users how the program should be used.
Documentation can and should be done within
the coding itself by using comments!!
65
End of Lecture 02
Let’s test your understanding on ALGORITHM
66
TEST
67 1
Pseudocode:
Start
• Fill a kettle with water
• Boil the water in the kettle
• Put the coffee powder and sugar inside the pot
• Pour boiling water in the pot
End
69 Flowchart
Start
End
TEST
70 2
Pseudocode:
Start
• Input Radius
• Calculate perimeter = 44/7 * Radius
• Print Perimeter
End
72 Flowchart
Start
Input Radius
Print Perimeter
End
TEST
73 3
Pseudocode:
Start
• Input X
• Input Y
• Calculate
• Sum=x+y
• Print Sum
End
75
Flowchart
Start
Input X
Input Y
Sum = X+Y
Print Sum
End
76
77