Download Problem Solving and Algorithms

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Problem Solving
ƒ We do it all the time
ƒ Approaches:
Problem Solving
and
Algorithms
ƒ Less successful
ƒ Grope blindly toward a solution
ƒ Fail to complete a chain or reasoning
ƒ Successful
ƒ Begin with what is understood
ƒ Draw on other information sources
ƒ Problem Solving in CS
ƒ Algorithmic thinking
SJAllan 2005
Algorithm
Problem Solving and Algorithms
2
Computers and Algorithms
ƒ A detailed sequence of actions
ƒ A computer carries out an algorithm to
(instructions) performed to accomplish
some task
process information
ƒ Named after an Iranian mathematician,
Al-Khawarizmi
ƒ This algorithm is called a program
ƒ Languages for expressing algorithms
SJAllan 2005
Problem Solving and Algorithms
3
SJAllan 2005
Problem Solving and Algorithms
4
1
Requirements of an Algorithm
Useful Methodology
ƒ Be well-ordered
ƒ Be unambiguous
ƒ Halt in a finite amount of time
ƒ Be “doable”
ƒ Produce a result
SJAllan 2005
Problem Solving and Algorithms
ƒ Assumptions?
ƒ Top down design
ƒ Stepwise refinement
5
SJAllan 2005
Problem Solving and Algorithms
Steps in Program Development
Step 1: Analyze the Problem
1.
2.
3.
4.
5.
6.
7.
ƒ Output
ƒ Input
ƒ Given Values
ƒ Processing
SJAllan 2005
Analyze the problem
Outline the solution
Develop an algorithm
Test the algorithm
Code the algorithm
Run and test the program
Document and maintain the program.
Problem Solving and Algorithms
7
SJAllan 2005
Problem Solving and Algorithms
6
8
2
Example
Perform Step 1 for the following problem
statement:
Write an algorithm that accepts the length and
width of a rectangular city lot, the length and
width of the house built on that lot, and computes
and displays the time needed to mow the lawn at
the rate of 2 square meters per minute.
Step 2: Outline the Solution
ƒ Top-down design
ƒ Stepwise refinement
ƒ Consider order
ƒ What is done first, second, etc.?
ƒ Is there any repetition?
ƒ Are there any situations where we need to
select from alternate choices?
SJAllan 2005
Example
Refer to the results for the following problem
statement of Step 1 and perform Step 2:
Write an algorithm that accepts the length and
width of a rectangular city lot, the length and
width of the house built on that lot and
computes and displays the time needed to mow
the lawn at the rate of 2 square meters per
minute.
Problem Solving and Algorithms
10
Step 3: Design an Algorithm
ƒ Constrained by what a computer can do
NOTE: As you gain more experience,
you may find yourself combining
Steps 2 and 3.
SJAllan 2005
Problem Solving and Algorithms
12
3
Step 3: Algorithm Design (cont.)
Step 3: Algorithm Design (cont.)
Variables
CONTROL STRUCTURES
ƒ Storage locations in memory
ƒ Address
ƒ Contents
ƒ Destructive write
ƒ Non-destructive read
ƒ Naming conventions
SJAllan 2005
Problem Solving and Algorithms
ƒ Sequence
ƒ Selection
ƒ Repetition
13
Step 3: Algorithm Design (cont.)
SJAllan 2005
Pseudo code keywords for:
ƒ Input
ƒ Read – from a file
ƒ Get – from the keyboard
ƒ Output
ƒ Print, Display – to the monitor
ƒ Write – to a file
ƒ Prompts and Labels
ƒ Keywords
ƒ Operators
ƒ Punctuation
Problem Solving and Algorithms
14
Step 3: Algorithm Design (cont.)
ƒ Elements of our pseudocode:
SJAllan 2005
Problem Solving and Algorithms
15
SJAllan 2005
Problem Solving and Algorithms
16
4
Step 3: Algorithm Design (cont.)
Example
ƒ Assigning a value to a variable
Refer to the results for the following
problem statement of Steps 1 and 2 and
perform Step 3 :
ƒ Initialization and/or Assignment
variable = value
Set variable to value
Initialize variable to value
Write an algorithm that accept the length and
width of a rectangular city lot, the length and
width of the house built on that lot and
computes and displays the time needed to
mow the lawn at the rate of 2 square meters
per minute.
ƒ Mathematical Operators
+ − ∗ / (binary)
− (unary)
SJAllan 2005
Problem Solving and Algorithms
17
Step 3: Algorithm Design (cont.)
Step 3: Algorithm Design (cont.)
Order Of Execution
Truth Tables
ƒ Conditions
ƒ Boolean values – true or false
ƒ Relational Operators
Value of
Condition 1
Value of
Condition 2
Condition 1
And
Condition 2
True
True
True
True
False
False
False
True
False
False
False
< >
==
<= >= !=
ƒ Complex Conditions
ƒ Logical Operators
And – Or – Not
False
SJAllan 2005
Problem Solving and Algorithms
19
SJAllan 2005
Problem Solving and Algorithms
20
5
Step 3: Algorithm Design (cont.)
Step 3: Algorithm Design (cont.)
Truth Tables
Truth Tables
Value of
Condition 1
Value of
Condition 2
True
True
Condition 1
Or
Condition 2
True
True
False
True
False
True
True
False
False
False
SJAllan 2005
Problem Solving and Algorithms
21
SJAllan 2005
Value of
Condition 1
True
Not Condition 1
False
True
False
Problem Solving and Algorithms
22
Step 3: Algorithm Design (cont.)
Step 3: Algorithm Design
Selection Structure: two alternatives
Selection Structure: one alternative
If condition Then
thing(s) to be done when
the condition is true
Else
thing(s) to be done when
the condition is false
End If
(cont.)
If condition Then
thing(s) to be done when
the condition is true
End If
6
Step 3: Algorithm Design
(cont.)
Selection Structure: multiple
alternatives
If condition 1 Then
thing(s) to be done when
condition 1 is true…
Else If condition 2 Then
thing(s) to be done when
condition 2 is true
Else If condition 3 Then
thing(s) to be done when
condition 3 is true
Example
The Dollar Dress Store is having a sale
where the price of any item previously
marked down is reduced by 30%. Design
an algorithm that asks for the price of an
item and prompts for entry of Yes if the
item was previously marked down. The
algorithm then calculates the cost of the
item, calculates 6.25% sales tax, and
prints out a bill.
Step 3: Algorithm Design
(cont.)
Selection Structure: multiple
alternatives (cont.)
…
Else If condition n Then
thing(s) to be done when
condition n is true
Else
thing(s) to be done when none of the
previous conditions are true
End If
Example
Rah-Rah University has a scholarship
available for a single mother who is a fulltime student. Design an algorithm that
reads a student’s name, marital status (M
for married, N for not married), gender (M
for male, F for female), student status (F
for full-time, P for part-time), and number
of children from a file and displays the
student’s name and an appropriate
message concerning eligibility for the
scholarship.
7
Repetition Structures
Counted Loop
ƒ Body of the loop
ƒ Counted Loops
ƒ Condition-controlled Loops
SJAllan 2005
Problem Solving and Algorithms
Example
Design an algorithm that adds the
integers from 1 to 100 and displays the
sum on screen.
Repeat numeric value Times
statement(s) to be repeated
End Repeat
29
Example
Change the algorithm so that it
determines the sum of the integers
between two numbers entered by the
user.
8
Condition-Controlled Loop (cont.)
Condition-controlled Loop
While condition
ƒ Loop condition
ƒ Loop Control Variable (LCV)
ƒ Identify LCV(S) and condition(s)
ƒ Prime the loop
ƒ Change value of LCV inside loop
statements to be repeated as long as the
condition is true
End While
ƒ Sentinel value
SJAllan 2005
Example
Design an algorithm that prompts for and
receives pairs of integer values from a user
and displays their sum, product, and average
on the screen. Entry of a pair of zero values
signals the end of data entry. The algorithm
also prints the number of pairs entered.
Problem Solving and Algorithms
34
Multiple and Nested Structures
ƒ
ƒ
ƒ
ƒ
ƒ
ƒ
ƒ
Multiple Ifs – End Ifs
Multiple Repeats and/or Whiles
If – End Ifs and Repeats and/or Whiles
If – End If inside Then and/or inside Else
If – End If inside Repeat or While
Repeat or While inside Repeat or While
Repeat or While inside Then and/or inside Else
SJAllan 2005
Problem Solving and Algorithms
36
9
Visualization of Proper
Multiple/Nested Structures
Example
Revise the previous algorithm so that an
asterisk is displayed beside the sum of any
pair that is greater than 200.
NO!
Example
Example
A file of student records contains name, gender
(M or F), age (in years), and marital status (S for
single or M for married). Design an algorithm
that reads through the file until a name of Quit is
read and computes the numbers of married men,
single men, married women, and single women.
Output these counts to a file called
StudentSummary. Output the names of any
single men over 30 to a file called
OlderSingleMen.
A company needs a program to process the payroll for its 15
employees.The algorithm reads an employee’s number, pay
rate, and the number of hours worked. The algorithm
computes the employee’s pay and prints it along with the
input data. According to the company’s rules, no employee is
paid for more than 60 hours and the maximum hourly rate is
$25.00. If more than 35 hours are worked, then payment for
the overtime hours worked is calculated at time-and-a-half. If
the hours worked or hourly rate for an employee is out of
range, then the input data and an appropriate message is
printed and no pay is calculated.
10
Step 4: Test the Algorithm
Step 5: Code the algorithm
ƒ Design a set of test data
ƒ Compute the results by hand
ƒ Step through the algorithm
ƒ Programming Languages
ƒ Machine language
ƒ High-level languages – C++, Java
ƒ Compilers
ƒ Play the role of the computer
ƒ Keep track of the values stored in the
variables (hand trace)
ƒ Syntax
ƒ Compare results to hand-calculated
results
SJAllan 2005
Problem Solving and Algorithms
41
Step 6:
Run and Test the Program
42
ƒ Documentation
ƒ Syntax Errors
ƒ Run-time Errors
ƒ Logic Errors
ƒ Internal (comments)
ƒ User (external)
ƒ Maintenance
ƒ Test data
Problem Solving and Algorithms
Problem Solving and Algorithms
Step 7:
Document and Maintain the
Program
ƒ Errors
SJAllan 2005
SJAllan 2005
43
SJAllan 2005
Problem Solving and Algorithms
44
11