unit_i_dsps

advertisement
UNIT- I
General Problem Solving Concepts
2
Contents
• Introduction
• General Problem Solving Concepts
▫ Types of Problems
▫ Problem solving with computers
▫ Difficulties with problem solving
▫ Problem solving Aspect
• Problem Solving Concepts for • Programming Concepts
▫ Communicating with computers
Computer
▫ Organizing the problem
▫ Constants and variables
▫ Using the tools
▫ Data types
▫ Testing the solution
▫ Functions
▫ Coding the program
▫ Operators
▫ Time & Space complexity
▫ Expressions and equations
▫ Sorting Problem
3
problem
Definition
• A perceived gap between the existing state and a
desired state
4
5
Introduction
• “If I have one hour to save the world I would spend fiftyfive minutes defining the problem and only five minutes
finding the solution.
-Einstein
6
Six steps for problem Solving
▫
▫
▫
▫
▫
Identify the problem
Understand the problem
Identify alternative ways to solve the problem
Select the best way to solve the problem
List instructions that enable you to solve the
problem using the selected solution
▫ Evaluate the solution
7
Example: Problem what to do this evening
1. Indentify the Problem: How do the Individuals wish to spend the evening?
2. Understand the problem: The knowledge base of the participants must be
considered. The only solutions that should be selected are ones that everyone
would know how to do. You probably would not select as a possible solution
playing a game of chess if the participants do not know hot to play chess.
3. Identify Alternatives:
▫ Watch Television
▫ Invite friends over
▫ Play games
▫ Go to the movie
▫ Play miniature golf
▫ Go to a friends party
▫ Go to the amusement park
▫ List is complete only when you can think of no more alternatives
4. Select best way to solve the problem
▫ Cut out alternatives that are not acceptable
▫ Specify pros and Cons of each Remaining alternative
▫ Compare pros and cons to make final decision
5. Prepare a list of steps that will result in a fun evening.
6. Evaluate the solution. Are we having fun yet?
8
• E.G. Chitti The Robot
(From: PS&PC by Maureen Sprankle -pg no:463-Appendix A)
9
General Problem-Solving Concepts
10
Types of Problems
• Problems can be solved with series of actions
▫ Algorithmic Solution
• Problems do not always have straightforward solutions
▫ Heuristic solutions
• Problem solver can use six steps for both algorithmic and heuristic
solutions
• Evaluating correctness and Appropriateness of heuristic solutions
are far less certain.
• Most problems require a combination of the two kinds of solutions
11
Problem solving with computers
• Results means the outcome or the completed computer
assisted answer
• Program means the set of instructions that make up the
solution using programming language
• Computers are built to deal with algorithmic solutions
• Difficulty lies in Programming
• Solutions must be transformed into an algorithmic format
12
Difficulties with Problem Solving
• People have many problems with problem solving
• Afraid to make decisions
• People go through one or more steps inadequately
• Problem solving process is not easy
• Problem solving on computer
▫ Difficult task of writing instructions
▫ Computer has specific system of communication
13
Problem Solving Aspect
• There is no universal method for problem solving
• Phases
▫ Problem Definition Phase
▫ Getting started on a problem
▫ The use of Specific Examples
▫ Similarities among problems
▫ Working backwards from the solution
▫ General Problem Solving Strategies
14
Problem Definition Phase
• What must be done?
• We must try to extract set of precisely defined tasks.
• E.g. Finding Square root, Greatest Common Divisor
Getting Started on a Problem
• Sometimes, even after problem definition people do not
know where to start?
• What can we do?
15
The use of Specific Examples
• Best approach when we are stuck is to make a start on a
problem is to pick a specific example
• Geometrical and graphical representing certain aspect of a
problem can be useful
Similarities among problems
• Bring as much as past experiences as possible
• New problem cannot be completely different
• Sometimes it blocks us from discovering a new thing
• In the first instance try to independently solve the problem
16
Working Backwards from the solution
• In some cases we assume that we already have solution and then
try to work backwards
• Whatever attempts we make that we make to get started on a
problem we should write it down
General Problem Solving Strategies
• Divide and Conquer
• Binary doubling
• Dynamic Programming
▫ Greedy method
▫ Branch and Bound
▫ Backtracking
17
Problem Solving Concepts for the Computer
18
Constants and Variables
• The Computer uses constants and variables to solve problems
• Constants is a value that never changes during processing of
all instructions
▫ Can be numeric, alphabetic or special Symbol
▫ Constant is given a value and then referred to by its
name
• In contrast, the value of a variable may change during
processing
• In many languages variables are called Identifiers
19
Rules for naming and Using Variables
1. Name a variable according to what it represent
2. Do not use spaces in variable name
3. Start variable with a letter
4. Do not use dash(-) in a program
5. After introduces variable name, exact variable name must
be used where data item is used
6. Be consistent while using UPPERCASE and lowercase
variables
7. Use naming conventions specified by your company
20
Data Types
• To process solutions computer must have data.
• Data are unorganized facts
• Computer Must be told the data types of each variable and
constant
DATA
Data
Processed
into
information
Report
Checks
Deposits
Bk Charges
Calculates
the Balance
Balance
Sheet
Fig. Processing Data: How a computer Balances a Checkbook
21
Data Type
Data Sets
Examples
Numeric:
Integer
All whole numbers
3400
-34
Numeric:
Real
All real Numbers
(Whole + Decimal)
3256.344
12333.0
0.32323
Character :
Surrounded by
quotation mark
All letters, Numbers
and Special Symbols
“a”, “1”, “A”, “F”,
“%”, “&”
“$”
String:
Surrounded by
quotation mark
Combination of
more than one
character
“Rahul”
“343443”
“7070-3232-232”
Logical
True, False
True, False
22
Rules for Data Types
1. The data that define the value of a variable or a constant will
be of numeric, character, logical
2. Computer then associate variable with desired data type
3. Data types can not be mixed
4. Each of the data types uses what is called a data sets
5. Any numeric item that must be used in calculations resulting
in a numeric result must be of numeric type
23
Functions
• Functions are small set of instructions that perform specific
task and return values
• By using functions programmers can shorten the problem
solving time and improves readability
• Each language has set of functions within it
• Most languages allows programmers to write their own
functions
• Functions can be
▫ Mathematical functions
▫ String functions
▫ Conversion Functions
▫ Statistical functions
▫ Utility functions
24
Operators
• Operators are the data connectors within expressions
• They tell the computer how to process data
• Operators
▫ Mathematical + - / * % ^
▫ Relational < > <= >= != [<>]
▫ Logical && || !
• Operand and resultant are two concepts related to operator
• The data type of Operands and resultant depends on the
operator
25
Expressions and Equations
• Expressions and Equations make a part of instructions in the
solution
• An Expressions processes data, operands using operators
e.g. length * breadth
• An Equation stores the resultant of an expression
e.g. area=length*breadth
• Setting up /Evaluating
▫
▫
▫
▫
Numeric Expression
Mathematical equations
Relational Expression
Logical expression
26
Programming Concepts
27
Communicating with the computer
• We have to learn system of communication or Computer language
• System of communication requires instructions governed by
specific rules.
• Syntax are the rules governing the computer operating system
• An error is called a bug
• Bug must be found and corrected, a process is called Debugging
• Computers are exact machines but the people working with them
are not.
28
Organizing the problem
• Organizational tools will help us to solve problems on the
computer
• Tools
▫
▫
▫
▫
▫
Problem Analysis Chart
Structure Chart or Interactivity Chart
IPO chart
Algorithms
Flowcharts
29
Analyzing the Problem
• First programmer has to understand and analyze the
requirements of the problem
• Good way to analyze problem is to separate it into four parts
1.
2.
3.
4.
•
The given data
The required Results
The processing that required in the problem
A list of solution alternatives
PAC allows problem solver to eliminate the words and
separate the facts from the problem
30
1. Given Data
2. Required Results
3. Processing Required
4. Solution Alternatives
Fig: Problem Analysis Chart
31
1. Given Data
2. Required Results
Hours
Pay rate
Grosspay
3. Processing Required
4. Solution Alternatives
Grosspay=Hours*Payrate
1. Define the hours worked
and Payrate as constants
2. Define the hours worked
and payrate as input values
Fig: Problem Analysis Chart
32
Developing the
Interactivity(Structure) Chart
• Next task in organizing the solution is to divide the
processing into subtasks called modules
• Connect these modules to show interaction of processing
between modules
• Each module should contain the task to accomplish one
function
• There will be one module that controls the flow of most of the
other module[Control or Main module]
Interactivity Chart
Control
Module 1
Module 3
Module 2
Module 3
Module 4
Module 5
Module 6
Module 7
Interactivity Chart for Payroll Problem
Payrollcontrol
Read
Calc
Until No Employee
Print
34
Module 7
Module 6
Module 3
Module 2
Module 3
Module 1
CONTROL
Module 5
Interactivity Chart
[Object Oriented Solution]
Module 4
35
Read
PayrollControl
Calc
Interactivity Chart for Payroll Problem
Object Oriented Solution
Print
36
Developing the IPO chart
• The input-processing-output chart extends and organizes the
information in the PAC
• IPO also shows that where in the solution processing takes
place
• Sections
1.
2.
3.
4.
Input
The processing
Module Reference
Output
37
Input
Processing
Module
reference
Output
All data input
[From section 1 of
PAC]
All processing in
step
[From Section 3
and 4]
Module reference
from the
interactivity chart
All output
requirement
[from section1 and
2 from PAC]
Input
Processing
Module
reference
Output
Hours worked
Pay Rate
1. Enter Hours
Worked
2. Enter Pay Rate
3. Calculate Pay
4. Print Pay
5. End
Read
Gross pay
The IPO Chart
Read
Calc
Print
Payrollcontrol
The IPO Chart for the Payroll Problem
38
e.g
1. Joe would like to build several bookcases that are different
heights and widths. All will be 12 inches in depth. The
bookcases will have 3 shelves, in addition to the bottom and
top. Write a solution to print the no. of feet of 12 –inch-wide
boards that Joe need to complete a bookcase, given the
height and width.
2. Bob would like to know what percentage of his income his
rent is. Write a solution that would calculate and print this
percentage. (Hint: Percentage=Number/Total)
39
Writing the Algorithms
• Next steps in organizing the solution is to develop set of
instructions for the computer called algorithm
• Pseudo code is similar to algorithms
• Programmer writes a separate instructions for each module
in the structure chart
• Hardest part of problem solving
▫
▫
▫
▫
The instructions cannot assume anything
Cannot skip Steps
Must be executable one step at a time
Must be complete
40
Drawing Flowchart
• From the algorithms programmer develops the flowcharts
• Flowcharts are the graphical representation of the
algorithms
• Flowcharts can show errors in logic not readily visible in the
other charts
• A flowchart shows the flow of the processing from the
beginning to the end of a solution
41
Rules for drawing flowcharts
• You should write instructions inside the blocks
• If there is something you need to remember you can write a
note besides a block
• A flowchart always start at the top of the page and flows to the
bottom
• Use flowchart template to draw the symbols and flow lines
• Make the blocks big enough to write instructions so they can
be easily read
• Final copy of the flowchart may not be the first draft
42
Testing the Solutions
• When a solution is complete it is important to test it
• To test a solution programmers select test data, a set of
values
• A solution should never be assumed correct.
• If bug is detected, the solution has to be modified it and
then the testing process starts once again
43
Coding the Program
• Next step is writing the solution into a computer
language
• If programmer follows the proper steps in developing
solution there should be few logic errors in the program
44
Pointers of structuring program
• Use modules
▫
▫
▫
▫
▫
4 Logic structures
Sequential structure
Decision structure
Loop structure
Case structure
45
Modules
• Solution breaks always in modules
▫ Rules for designing modules
1. Each module is an entity itself with entrance and
exit
2. It should not jump in between
3. Only with Single function or task
4. Easily readable and modified
5. No. of instructions to complete the function
should be governed by functions
6. Order of processing
46
Types of modules
• Control module
• Initialization module
• Process module
• Calculation modules
• Print modules
• Read and data validation modules
• Wrap up modules
• Event module in OOP( mouse up/dw etc…)
47
Cohesion and coupling
Cohesion: independent of each
other
Module 1
Module 2
Coupling allows to share data with
some interface
Module 3
48
Solution development -6 steps
1. The problem analysis chart
2. The interactivity chart
3. The IPO chart
4. Coupling diagram with design of data flow
5. Algorithm writing
6. Flowchart
49
•
PART I END
Download