an introduction to programming with c++ fifth edition chapter 2 beginning the problem-solving...

32
An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

Upload: miranda-millison

Post on 01-Apr-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++

Fifth Edition

Chapter 2Beginning the Problem-Solving Process

Page 2: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 2

Objectives

• Explain the problem-solving process used to create a computer program

• Analyze a problem

• Complete an IPO chart

• Plan an algorithm using pseudocode and flowcharts

• Desk-check an algorithm

Page 3: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 3

Concept Lesson

• Problem Solving

• Solving Everyday Problems

• Creating Computer Solutions to Problems

• Analyzing the Problem

• Planning the Algorithm

• Desk-Checking the Algorithm

• The Gas Mileage Problem

• Summary

Page 4: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 4

Problem Solving

• In this lesson, you will:– Explore the thought process followed when solving

problems– Learn how to use a similar process to create a

computer solution to a problem• Called a computer program

Page 5: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 5

Solving Everyday Problems

• First step in solving a problem: analyze it– E.g., problem of being hungry

• Next, you plan, review, implement, evaluate, and modify (if necessary) the solution– E.g., if you are still hungry

Page 6: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

Solving Everyday Problems (continued)

An Introduction to Programming with C++, Fifth Edition 6

Page 7: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 7

Solving Everyday Problems (continued)

Page 8: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 8

Creating Computer Solutions to Problems

• Analysis tools: IPO charts, pseudocode, flowcharts

• To desk-check or hand-trace, use pencil, paper, and sample data to walk through algorithm

• A coded algorithm is called a program

Page 9: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

Creating Computer Solutions to Problems (continued)

An Introduction to Programming with C++, Fifth Edition 9

Page 10: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 10

Analyzing the Problem

• Analyze a problem to:– Determine the goal of solving it

• Output

– Determine the items needed to achieve that goal• Input

• Always search first for the output

Page 11: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

Analyzing the Problem (continued)

An Introduction to Programming with C++, Fifth Edition 11

Page 12: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 12

IPO Charts

• Use an IPO chart to organize and summarize the results of a problem analysis– IPO: Input, Processing, and Output

Page 13: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

IPO Charts (continued)

An Introduction to Programming with C++, Fifth Edition 13

Page 14: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

IPO Charts (continued)

An Introduction to Programming with C++, Fifth Edition 14

Page 15: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 15

Analyzing the Problem (continued)

• First, reduce the amount of information you need to consider in your analysis:

Page 16: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

Analyzing the Problem (continued)

• Worse than having too much information is not having enough information to solve problem:

An Introduction to Programming with C++, Fifth Edition 16

Page 17: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 17

Analyzing the Problem (continued)

• Distinguish between information that is missing and information that is implied:

Page 18: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 18

Planning the Algorithm

• Algorithm: set of instructions that will transform the problem’s input into its output– Record it in the Processing column of the IPO chart

• Processing item: intermediate value used by algorithm when processing input into output

• Pseudocode is a tool programmers use to help them plan an algorithm– Short English statements

Page 19: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 19

Planning the Algorithm (continued)

Page 20: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 20

Planning the Algorithm (continued)

• Flowcharts are also used to plan an algorithm– Use standardized symbols– Symbols connected with flowlines– Oval: start/stop symbol– Rectangle: process symbol

• Represents tasks such as calculations– Parallelogram: input/output symbol

• Represents I/O tasks

Page 21: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 21

Planning the Algorithm (continued)

Page 22: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 22

Planning the Algorithm (continued)

• A problem can have more than one solution:

Page 23: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 23

Hints for Writing Algorithms

This problem specification is almost identical to the one shown earlier in Figure 2-4

Page 24: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 24

Hints for Writing Algorithms (continued)

You may use a portion of a previous solution to solve current problem

Page 25: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 25

Desk-Checking the Algorithm

Page 26: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 26

Desk-Checking the Algorithm (continued)

Page 27: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 27

Desk-Checking the Algorithm (continued)

• Valid data is data that the programmer is expecting the user to enter

• Invalid data is data that he or she is not expecting the user to enter

• You should test an algorithm with invalid data– Users may make mistakes when entering data

Page 28: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 28

The Gas Mileage Problem

Page 29: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 29

The Gas Mileage Problem (continued)

• After planning the algorithm, you desk-check it:

Page 30: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 30

Summary

• Problem-solving typically involves analyzing the problem, and then planning, reviewing, implementing, evaluating, and modifying (if necessary) the solution

• Programmers use tools (IPO charts, pseudocode, flowcharts) to help them analyze problems and develop algorithms– During analysis, you determine the output and input– During planning, you write the steps that will transform

the input into the output

Page 31: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 31

Summary (continued)

• After the analysis and planning, you desk-check the algorithm– Follow each of the steps in algorithm by hand

• Coding refers to translating the algorithm into a language that the computer can understand

• Before writing an algorithm, consider whether you have already solved a similar problem

Page 32: An Introduction to Programming with C++ Fifth Edition Chapter 2 Beginning the Problem-Solving Process

An Introduction to Programming with C++, Fifth Edition 32

Application Lesson: Using the First Steps in the Problem-Solving Process

• Lab 2.1: Stop and Analyze

• Lab 2.2:– Create a program that the salesclerks can use to

calculate and display the required number of rolls

• Lab 2.3:– The program must now display the number of single

and double rolls of wallpaper

• Lab 2.4: Desk-Check Lab

• Lab 2.5: Debugging Lab