Upload
dinah-parrish
View
258
Download
1
Tags:
Embed Size (px)
Citation preview
Chapter 6: Problem Solving Chapter 6: Problem Solving andandAlgorithm DesignAlgorithm Design
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 11
In general, how are problems solved on a In general, how are problems solved on a computer?computer?
AlgorithmsAlgorithms
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 22
An An algorithmalgorithm is an ordered set of unambiguous, is an ordered set of unambiguous, executable steps that ultimately terminate if executable steps that ultimately terminate if followed.followed.
AmbiguoAmbiguous:us:
Not Not executable:executable:
No No terminationtermination
::
• LatherLather• RinseRinse• RepeatRepeat
Computer AlgorithmsComputer Algorithms
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 33
In computer programming, an algorithm is the In computer programming, an algorithm is the sequence of steps (i.e., the “recipe”) for sequence of steps (i.e., the “recipe”) for accomplishing a task.accomplishing a task.Every step in an algorithm has two basic Every step in an algorithm has two basic
components:components:1.1. SemanticsSemantics: The meaning of the step: The meaning of the step2.2. SyntaxSyntax: The format of the step: The format of the step
PseudocodePseudocode
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 44
Pseudocode is an informal notation for expressing Pseudocode is an informal notation for expressing an algorithm.an algorithm.Procedure Sat1231AProcedure Sat1231A
Set Set yearyear to 2001 to 2001 Set Set monthmonth to January to January Set Set dayday to first Saturday in January 2001 to first Saturday in January 2001 While (While (yearyear < 2021) Do < 2021) Do {{ Increment day by 7Increment day by 7 If date is New Year’s EveIf date is New Year’s Eve Then display Then display yearyear as having a Saturday New as having a Saturday New Year’s EveYear’s Eve If If dayday > (number of days in > (number of days in monthmonth)) ThenThen {{ Adjust Adjust dayday by subtracting the number by subtracting the number of days in of days in monthmonth If If monthmonth is December is December ThenThen {{ Increment Increment yearyear by 1 by 1 Set Set monthmonth to January to January }} ElseElse Increment Increment monthmonth by one by one }} }}
ExampleExample: : Which years in Which years in
2001-2020 2001-2020 have New have New
Year’s Eve on Year’s Eve on Saturday?Saturday?
Algorithm AlternativesAlgorithm Alternatives
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 55
It’s possible to devise many algorithms to solve It’s possible to devise many algorithms to solve the same problem.the same problem.
Procedure Sat1231BProcedure Sat1231B Set Set day_of_weekday_of_week to 12/31/2000 to 12/31/2000 Set Set yearyear to 2001 to 2001 While (While (yearyear < 2021) Do < 2021) Do {{ If If yearyear is a leap year is a leap year Then increment Then increment day_of_weekday_of_week by 2 by 2 Else increment Else increment day_of_weekday_of_week by 1 by 1 If If day_of_weekday_of_week is Saturday is Saturday Then display Then display yearyear as having a as having a Saturday New Year’s EveSaturday New Year’s Eve Increment Increment yearyear by 1 by 1 }}
Alternative Alternative pseudocode to pseudocode to
determine which determine which years in 2001-years in 2001-2020 have New 2020 have New Year’s Eve on Year’s Eve on
Saturday.Saturday.
BothBoth algorithms work, but which is algorithms work, but which is betterbetter??
Which is easier to code?Which is easier to code?
Which runs more Which runs more efficiently?efficiently?
Program ModularityProgram Modularity
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 66
The software development process The software development process usually involves a team of usually involves a team of developers, so the software is developers, so the software is often designed as a hierarchical often designed as a hierarchical system of modules, which can be system of modules, which can be viewed as easily modified viewed as easily modified interdependent entities.interdependent entities.
Advantages of Modular Advantages of Modular ProgrammingProgramming
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 77
Computer Graphics Modularity Computer Graphics Modularity ExampleExample
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 88
mainmain
generatgeneratee
polygonspolygonsgeneratgenerat
eetorustorus
generatgeneratee
cubecubegeneratgenerat
eeteapotteapotgeneratgenerat
eespherespheregeneratgenerat
eeconecone
drawdrawpolygonspolygons
shadeshadepolygonpolygon
drawdrawpixelspixels
mainmain
generatgeneratee
polygonspolygonsgeneratgenerat
eetorustorus
generatgeneratee
cubecubegeneratgenerat
eeteapotteapotgeneratgenerat
eespherespheregeneratgenerat
eeconecone
drawdrawpolygonspolygons
blendblendpolygonspolygons
computecomputereflectreflect
drawdrawpixelspixels
mainmain
generategeneratepolygonspolygons
generategeneratetorustorus
generategeneratecubecube
generategenerateteapotteapot
generategeneratespheresphere
generategenerateconecone
drawdrawpolygonspolygons
blendblendpolygonspolygons
computecomputereflectreflect
drawdrawpixelspixels
computecomputecurvedcurvedsurfacessurfaces
Initially, set the program up Initially, set the program up to calculate the scene with to calculate the scene with
simple lighting.simple lighting.
Replace the simple lighting Replace the simple lighting with more sophisticated with more sophisticated
reflective surfaces.reflective surfaces.
Add a module to smooth out Add a module to smooth out the silhouettes of objects.the silhouettes of objects.
Top-Down DesignTop-Down Design
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 99
One common approach for designing programs is One common approach for designing programs is the the top-downtop-down methodology. methodology.1.1.Design a high-Design a high-
level solution level solution to the to the programming programming problem.problem.
This approach This approach lends itself to lends itself to modularity.modularity.
2.2.Consider each Consider each complicated complicated subproblem as a subproblem as a separate separate programming programming problem.problem.
3.3.Return to Return to step #1 with step #1 with each each subproblem.subproblem.
However, it may impose an However, it may impose an artificial hierarchy on the tasks artificial hierarchy on the tasks being performed.being performed.
Bottom-Up DesignBottom-Up Design
Chapter 6Chapter 6Problem Problem
Solving and Solving and Algorithm Algorithm
DesignDesignPage Page 1010
A newer approach for designing programs is the A newer approach for designing programs is the bottom-upbottom-up methodology. methodology.1.1.Separate each major Separate each major
task in the overall task in the overall problem into an problem into an individual individual programming programming problem.problem.
This approach This approach produces reusable produces reusable modules.modules.
2.2.Develop Develop cooperative cooperative programming programming solutions to the solutions to the separate problems.separate problems.
However, those modules may However, those modules may prove difficult to cobble together prove difficult to cobble together to solve bigger problems.to solve bigger problems.