Presentation is loading. Please wait.

Presentation is loading. Please wait.

Similar presentations


Presentation on theme: ""— Presentation transcript:

56 Chapter 6: Problem Solving and Algorithm Design
In general, how are problems solved on a computer? Analysis & Specification Understand the problem Specify what the program needs to do Algorithm Development Formulate sequence of steps for solving problem Test that the steps work for certain key cases Implementation Translate the algorithm into a programming language Test whether the program produces correct results Maintenance Deliver the program and have real users use it Debug and upgrade the program as needed Chapter 6 Problem Solving and Algorithm Design Page 56

57 Problem Solving and Algorithm Design
Algorithms An algorithm is an ordered set of unambiguous, executable steps that ultimately terminate if followed. Ambiguous: Not executable: Lather Chapter 6 Problem Solving and Algorithm Design Page 57 No termination: Rinse Repeat

58 Problem Solving and Algorithm Design
Computer Algorithms In computer programming, an algorithm is the sequence of steps (i.e., the “recipe”) for accomplishing a task. Every step in an algorithm has two basic components: Semantics: The meaning of the step Syntax: The format of the step Semantics Get a value from the user Double that value Return the result to the user Syntax Program DoubleIt; var x, y integer; begin write(“Input your number: ”); read(x); y = 2*x; writeln(“The doubled number is ”, y); end. Chapter 6 Problem Solving and Algorithm Design Page 58

59 Pseudocode Pseudocode is an informal notation for expressing an algorithm. Procedure Sat1231A Set year to 2001 Set month to January Set day to first Saturday in January 2001 While (year < 2021) Do { Increment day by 7 If date is New Year’s Eve Then display year as having a Saturday New Year’s Eve If day > (number of days in month) Then Adjust day by subtracting the number of days in month If month is December Increment year by 1 } Else Increment month by one Example: Which years in have New Year’s Eve on Saturday? Chapter 6 Problem Solving and Algorithm Design Page 59

60 Problem Solving and Algorithm Design
Algorithm Alternatives It’s possible to devise many algorithms to solve the same problem. Procedure Sat1231B Set day_of_week to 12/31/2000 Set year to 2001 While (year < 2021) Do { If year is a leap year Then increment day_of_week by 2 Else increment day_of_week by 1 If day_of_week is Saturday Then display year as having a Saturday New Year’s Eve Increment year by 1 } Alternative pseudocode to determine which years in have New Year’s Eve on Saturday. Both algorithms work, but which is better? Which is easier to code? Which runs more efficiently? Chapter 6 Problem Solving and Algorithm Design Page 60

61 Networking Capability Artificial Intelligence
Program Modularity The software development process usually involves a team of developers, so the software is often designed as a hierarchical system of modules, which can be viewed as easily modified interdependent entities. Video Game Networking Capability Data Security Server Access Customer Billing Game Play Animation Character Animation Special Effects Backgrounds Artificial Intelligence Sound Music Voice Sound Effects Chapter 6 Problem Solving and Algorithm Design Page 61

62 Problem Solving and Algorithm Design
Advantages of Modular Programming Modifiability It’s easier to alter or upgrade the program if changes can be segregated to particular modules Debuggability It’s easier to diagnose and pinpoint problems with the program if it’s split into logically distinct modules Reusability Generic modules can be developed and then reused in other programs, eliminating the need to “reinvent the wheel” Readability It’s easier to read someone else’s program or refresh you memory about your own program if it’s broken into self-contained units Chapter 6 Problem Solving and Algorithm Design Page 62

63 Computer Graphics Modularity Example
Initially, set the program up to calculate the scene with simple lighting. Replace the simple lighting with more sophisticated reflective surfaces. Add a module to smooth out the silhouettes of objects. main generate polygons torus cube teapot sphere cone draw shade polygon pixels main generate polygons torus cube teapot sphere cone draw blend compute reflect pixels main generate polygons torus cube teapot sphere cone draw blend compute reflect pixels curved surfaces Chapter 6 Problem Solving and Algorithm Design Page 63

64 Top-Down Design One common approach for designing programs is the top-down methodology. Design a high-level solution to the programming problem. Consider each complicated subproblem as a separate programming problem. Return to step #1 with each subproblem. Process Payroll Get Input Data Retrieve Timecard Data Retrieve Salary Information Retrieve Dependent Records Retrieve Retirement Plans Retrieve Tax Rates Perform Calculations Compute Gross Pay Compute Deductions Produce Output Generate Paychecks Update YTD Records This approach lends itself to modularity. However, it may impose an artificial hierarchy on the tasks being performed. Chapter 6 Problem Solving and Algorithm Design Page 64

65 Bottom-Up Design A newer approach for designing programs is the bottom-up methodology. Separate each major task in the overall problem into an individual programming problem. Develop cooperative programming solutions to the separate problems. Video Game Engine Physics Engine Fluid Dynamics Engine Particle Systems Engine Collision Detection Engine Math Engine Rotational Calculation Engine Fractal Engine Artificial Intelligence Engine Intelligent Agent Engine Behavior Prediction Engine This approach produces reusable modules. However, those modules may prove difficult to cobble together to solve bigger problems. Chapter 6 Problem Solving and Algorithm Design Page 65


Download ppt ""

Similar presentations


Ads by Google