UNIT I
■
General Problem Solving
Overview
■ Problem Solving in Everyday Life
■ Type of Problems
■ Problem Solving with Computers
Objectives
■ Describe the difference between heuristic
and algorithm solution to problems
■ List and describe the six problem-solving
steps to solve a problem that has an algorithmic solution
■ Use the six problem-solving steps to solve
Problem Solving in Everyday Life
■ People make decision every day to solve
problem that affect their lives.
■ Bad decision is made, time and resources
are wasted.
Six Steps in Problem Solving
■ There are six steps to follow to ensure the
best decision
■ Identify the Problem
■ Understand the Problem
■ Identify alternative ways to solve the
problem
■ Select the best way to solve the problem
from the list of alternative solution
■ List instructions that enable you to solve the
problem using the selected solution
How to Avoid Bad Decision
■ https://www.youtube.com/watch?v=ASY4
Activity Think Pare and Share
■ Take the Problem of What to do this
■ STEP I:n Identify the problem
■ Step II: Understand the Problem
■ Step III: Identify alternative Solution
■ Solution Pros Cons ■ A.
■ B.
■ Step IV: Select the Best Solution
■ Step V: List a set of step by step
instruction to attain the solution
■ 1. ■ 2
Type of Problems
■ The Problem do not always have straight
forward solution
■ Some problem can be solved series of
actions is called Algorithm
■ The Solutions can be reached by
completing the actions in steps
■ A process of solving a problem in trial and
Problem Solving with Computers
■ Solution: It means the instruction listed
during steps of problem solving
■ The Instruction that must be followed to
produce the best results
■ Results: It Means the outcome or the
completed computer assisted answers
■ Program: The set of instruction that make
up the solution after they have been
■ Computer are build to deal with
algorithmic solutions which are often
difficult and time consuming for humans
■ People are better than computer at
developing heuristic solution
■ But computer are good for solving difficult
problems example sorting 10000 data in ascending order
■ Artificial Intelligence: The field of
Difficulties with Problem
Solving
■ Lack of Problem Solving Experience
■ Inadequate Solution Steps
■ Incorrect Problem Definition
■ Alternative Chosen Incorrectly
■ Invalid Logic
Difficulties with Problem Solving
■ People Have many problem to Problem
Solving
■ Not been taught how to solve problems
■ Fear in making decision
■ One or more steps inadequate
■ They may define the problem correctly
■ Not generated list of alternatives
■ Problem solving is not easy it time
Questions
■ What are the six steps of problem solving
■ What is an algorithm solution to a problem
■ What is a heuristic solution to a problem
■ What is Program?
■ What is Instruction?
■ What is Artificial Intelligence?
Summary
■ General Problem Solving
■ Six Steps for Problem Solving
■ Algorithm
Overview
■ Constants and Variables
■ Rules for Naming and Using Variables
■ Data Types
■ Numeric Data
■ Character Data – Alphanumeric Data ■ Logical Data
■ Other Data Types
■ Rules for Data Types ■ Example of Data Types
■ Functions ■ Operators
Objectives
■ Differentiate between variables & constants.
■ Differentiate between character, numeric and
logical data types.
■ Identify operators, operands and resultants.
■ Identify and use functions.
■ Identify and use operators according to their
placement in the hierarchy chart.
■ Set up an evaluate expressions and equations
Problem types in Computer
■ Problems that can be solved on computers
generally consists of:
■ Computational problems
■ Involve some kind of mathematical processing
■ Logical problems
■ Involve relational and logical processing
■ Repetitive problems
■ Involve repeating set of mathematical or logical
Constants and Variables
■ A constant is a value that never change
during the processing.
■ Constants can be of any data types –
numeric, alphabetical or special symbols.
■ Eg: PI = 3.14
■ A variable may change during processing.
■ Also called as identifiers.
Rules for Naming and Using
Variables
■ Name a variable according to what it represents.
Eg: hours for hours worked.
■ Do not use spaces in a variable name. eg: Hours
Worked
■ Start a variable name with a letter
■ Do not use a dash or any symbol used as a
Rules for Naming and Using
Variables
■ Once the variable name is specified the
same is used in all places.
■ Be consistent when using upper and
lowercase characters.
■ Use the naming conventions specified by
Data Types
■ Data are unorganized facts.
■ They go into the computer as input and
are processed by the program.
■ Output is the information
Data Types
Data Data Processed into
Information Report
Input Output
Check deposits & Bank
Charges
Calculates the
balance Balance Sheet
Numeric Data
■ Numeric data includes all types of
numbers.
■ The subtype of numeric includes integers
and real numbers.
■ A data set is the set of values that are
Character Data
■ Character data set consists of letters and
special characters available to computer.
■ Also called alphanumeric when consists of
single digit numbers.
■ Character data or string data can be
Logical Data
■ Logical data consists of two values in the
data set – true and false.
■ These are used in making the decisions
ie:- yes or no.
■ The logical data set true and false are not
Other Data Types
■ Date Data Types
■ The use of this data type allows the user to
perform mathematical calculations on any date.
■ User – defined Data Types
■ Data types defined by the programmers.
■ User must specify the items in the data set for
Rules for Data Types
■ The data define the value of a variable or a
constant will be most commonly be one of the three data types: numeric, character or logical.
■ The programmer designates the data type
during the programming process.
■ Data types cannot be mixed.
■ Each of the data types uses a data set. ■ Any numeric item that must be used in
Functions
■ Functions are small set of instructions that
perform specific tasks and return values.
■ Functions are used as parts of instructions
in a solution
■ Syntax for a function:
functionname(data)
■ data listed as a part of the functions are
Functions
■ Functions are divided into following classes:
■ Mathematical Functions: mostly used in business
and science.
■ Square root, random number, abs, sign
■ String Functions
■ Copy, length, concatenation, left, right
■ Conversion Functions
■ Type conversion (string to numeric)
■ Statistical Functions
■ Maximum values, minimum values,sum,avg
■ Utility Functions
Contd…
■ Functions names are not allowed to used
Operators
■ Operators are the data connectors within
expressions and equations.
■ Used to process the data.
■ Also specify the type of processing.
■ Types of operators:
■ Mathematical ( +, - , *, / , %, ^) ■ Relational (>, <, =, <=, >= )
Operators
■ Operands are the data that the operator
connects and process.
■ Resultant is the answer that results when
the operations is completed.
■ Operands can be constant or variable.
■ Data type of operands and resultants
Operators
■ All operators have a hierarchy or precedence, an
order in which their operations take place.
■ To reorder the normal processing sequence the
programmer uses parenthesis.
■ The processing of the operands always starts
Expressions and Equations
■ An expression processes data, the
operands through the use of operators.
■ Eg: length * width
■ An equation stores the resultant of an
expression in a memory location in the computer through the equal (=) sign.
Evaluate Expressions
■ Example 1: Setting Up a Numeric Expression
■ A programmer is given following expression to
use in solving the problem.
■ X(3Y + 4) – 4Y/X+6
■ The above expression is rewritten as:
■ X * (3 * Y + 4) – 4 * Y / (X + 6)
■ Parenthesis are added when the hierarchy has
Evaluate Expressions
■ Write all parentheses, operands and
operators on a single line with the dividend first followed by the divisor.
X ( 3Y + 4) – 4Y/X+6
■ Insert all implied operators.
X * ( 3 * Y + 4) – 4 * Y / X + 6
■ Insert all parentheses where the hierarchy
needs to be reordered.
Evaluate Equations
■ Example 2: Setting up a Mathematical
Equation
■ A mathematical equation is given to the
programmer as:
Y + 3 = X ( Z + 5 )
■ Programmer modify it into assignment
instruction as:
Example 2
■ Following steps are performed:
■ Write all parentheses, operands and
operators in a single line: Y + 3 = X (Z + 5)
■ Use of mathematical rules to complete the
equation so that there is one variable on left side.
Y = X (Z + 5) – 3
■ Follow the steps in example 1 to complete
Example 3
Setting Up a Relational Expression
■ A relational expression is used to make
decisions.
X is less than Y + 5
■ The programmer would change its form to
the following:
Example 4
Setting Up a Logical Expression
■ The expression is of the form A OR B.
■ The operands A and B are of logical data
Example 5
Evaluating a Mathematical Expression
■ Consider the expression:
5 * (X + Y) – 4 * Y/(Z+6)
■ The programmer uses any valid values
like:
Example 6
Evaluating a Relational Expression
■ Consider the expression:
A – 2 > B
■ Programmer can use any valid value like:
A = 6 and B = 8
■ The result of any relational expression will
Example 7
Evaluating a Logical Expression
■ Consider the expression:
( A AND B ) OR (C AND A)
■ Programmer can use any valid value like:
Example 8
Evaluating an Equation that uses
both Relational and Logical
Operators
■ Consider the equation:
■ F = NOT ( A < B) AND ( C OR D)
■ Programmer can use any valid value like:
Example 9
■ Developing a table of all possible Resultants
of a Logical Expression
■ The programmer can also figure out the
resultants for all the possible combinations of the values of the variables in an expression.
■ No. of possible combinations = double the
Example 9
■ Let the expression be: NOT A OR B
A B NOT A OR B
T T F
T F F
F T F
Example 10
■ Developing a logical expression from a
given problem.
■ A programmer can form a logical
expression based on some criteria.
■ Identification of the data items on which
Example 10
■ Problem: A large department store has its own
charge card. The policy for a customer to charge an item is that the customer must have a valid charge card and either a balance of less than $500 or a charge amount of less than $50.
■ First list the items:
■ The charge card (logical data type)
■ The balance (numeric data type)
Example 10
■ Second list the condition on the data items:
■ ChargeCard Balance < 500 Amount <50
■ Third put the logical operators and
parentheses:
Example 10
■ Created logical expression can be used in
following ways:
■ In assignment statement:
■ OkToChg = ChargeCard AND (Balance < 500
OR Amount <50)
■ In a decision statement:
■ If ChargeCard AND (Balance < 500 OR Amount
<50) then
Print “ Ok to Charge” Or Else
Part C
Programming Concepts
■ Objectives:
■ List and describe six basic tools used to aid in development of solution to a problem.
■ Use of problem analysis chart to consolidate data for a problem.
■ Use an interactive chart to designate the modules used in the solution to problem.
■ Use an IPO chart to designate the input, processing , module number and output for a solution of a problem.
■ Use of algorithms and flowcharts to develop instructions for solution to problem.
How the Computer Stores Data
■ Data is stored internally in memory
locations.
■ Also stored in external storage when
required in future.
■ There are basically two types of files:
■ Program Files: contains the instructions to tell
computer what to do.
■ Data files: contains the data required to
Communicating with the
Computer
■ Communication with the computer is
achieved through instructions.
■ Syntax refers to the rules governing the
computer operating system, the language and the application.
■ An error is called a bug.
■ A bug must be found and corrected, a
Organizing the Problem
■ Organizational tools will help the computer
to solve problems.
■ Following are the tools:
■ Problem Analysis Chart
■ Structure Chart or Interactivity Chart
■ IPO Chart ■ Algorithms
Analyzing the problem
■ To organize solution , the programmer has to
understand and analyze the requirements of the
problem.
■ To analyze a problem separate it into four parts:
■ The given Data
■ The required results
Analyzing the problem
Given Data Required Results
Section 1: Data given in the problem or provided by the user. These can be known values or general names for data, such as price, quantity, and so forth.
Section 2: Requirements for the output reports. This includes the information needed and the format required.
Processing Required Solution Alternatives
Section 3: List of processing required. This includes equations or other types of processing such as sorting,
searching and so forth.
Section 4: List of ideas for the solution of the problem
Analyzing the problem
■ Calculate the gross pay of employee.
Given Data Required Results
Hours PayRate
Gross Pay
Processing Required Solution Alternatives
GrossPay = Hours * PayRate
1. Define the hours worked and pay rate as
constants.
Developing the Interactivity
(Structure) chart
■ The next step is to divide the processing
into subtasks called modules
■ Connect these modules together to show
the interaction of processing between the modules.
■ Each module is responsible for executing a
Developing the Interactivity
(Structure) chart
■ Control module controls the flow to most
of the other modules.
■ The interactivity chart shows how the
modules interact with each other.
■ The interactive chart shows what will
Interactivity Chart
Module 1
Control Module
Module 2 Module 3 Module 4 Module 5
Interactivity Chart
Payroll Control
Read Calculate Print
Developing the IPO Chart
■ IPO chart extends and organizes the
information in the problem analysis chart.
■ It show in more detail what data item are
IPO Chart
■ IPO chart has four sections:
■ Input: contains all input data from the PAC
■ Processing: all processing (section 3 and 4 of
PAC)
■ Module Reference: the number from the
interactivity chart in which each step processing is completed.
■ Output: required output designated by the
IPO Chart
Input Processing Module
Reference
Output
Hours Worked Pay Rate
1. Enter hours
worked
2. Enter pay rate 3. Calculate Pay
4. Print Pay
Algorithms
■ Developing set of instructions for the
computer
■ To complete all of the algorithms needed
to solve a problem programmer writes the separate set of instruction for each module
■ The algorithms pull the interactivity chart
Drawing the Flowcharts
■ From the algorithms the programmer
develops the flowcharts.
■ Flowcharts shows the flow of the
processing from beginning to the end of a solution.
■ Algorithms and flowcharts are the final
Rules for drawing flowcharts
■ Write the instructions inside the blocks
■ If there is something to remember, write a note
besides a block. (annotated flowchart)
■ A flowchart always start at the top of the page
and flows to the bottom.
■ Use flowchart template to draw the symbols and
straightedge to draw the flowlines.
■ Make the blocks big enough to write instructions ■ Put the module number and the name from the
Algorithms and Flowchart for
Payroll problem
Algorithm Control Module ■ Repeat Process Read Process Calculate Process PrintAlgorithms and Flowchart for
Payroll problem
Algorithm
Read Module
■ Read hours, Pay Rate
Algorithms and Flowchart for
Payroll problem
Algorithm
Calculate Module
■ GrossPay =
HoursWorked * PayRate
■ Exit
Calculate
GrossPay = HoursWorked * PayRate
Exit
Algorithms and Flowchart for
Payroll problem
Algorithm
Print Module
■ Print Pay
■ Exit
Print GrossPay
Exit
Internal and External
Documentation
■ Internal documentation consists of
remarks written with the instructions to explain what is being done in the program.
■ External documentation is made up of the
Testing the solution
■ Test the solution to make sure it meets
the requirements of the user and to check
the errors.