+ All Categories
Home > Documents > Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Date post: 29-Dec-2015
Category:
Upload: crystal-booker
View: 224 times
Download: 8 times
Share this document with a friend
Popular Tags:
39
Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process
Transcript
Page 1: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Computer ProgrammingTCP1224

Chapter 2Beginning the Problem-Solving Process

Page 2: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Week 1 Summary• Programs: step-by-step instructions that tell a computer

how to perform a task

• Programmers use programming languages to communicate with the computer

▫ First programming languages were machine languages

▫ High-level languages can be used to create procedure-oriented programs or object-oriented programs

• Algorithm: step-by-step instructions that accomplish a task (not written in a programming language)

▫ Algorithms contain one or more of the following control structures: sequence, selection, and repetition

2

Page 3: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Week 1 Summary• 3 main elements of programming

▫Sequence structure: process the instructions, one after another, in the order listed

▫Repetition structure: repeat one or more instructions until some condition is met

▫Selection structure: directs the computer to make a decision, and then to select an appropriate action based on that decision

3

Page 4: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Week 2 Objectives• Explain the problem-solving process used to

create a computer program

• Analyze a problem

• Complete an Input-Process-Output (IPO) chart (not Initial Public Offering)

• Plan an algorithm using pseudocode and flowcharts

• Desk-check (pen and paper check) an algorithm

4

Page 5: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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

5

Page 6: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Problem Solving• In this lecture, we will:

▫ Explore the thought process followed when solving problems. All of us have a different way of solving things, take for example a simple 8 x 9 problem.

Some of us will memorize it.

Some of use will solve it by 8 x 10 80 – 8 72

Some of use will do 8 x 5 + 8 x 4 (‘coz we can’t remember our multiplication tables).

Some of us will go through those “songs” for multiplication tables during our primary school days.

6

Page 7: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Problem Solving• In this lecture, we will:

▫Learn how to use a similar process to create a computer solution to a problem

Called a computer program.

Each of us will have a different approach, so sometimes programming is called an Art as some believe it isn’t a science!

We start with analyzing the problem, finding a solution and then only creating the program.

7

Page 8: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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

8

Page 9: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Solving Everyday Problems(1st attempt)

9

Page 10: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Solving Everyday Problems(modification)

10

Page 11: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Creating Computer Solutions to Problems•Analysis tools: IPO charts,

pseudocode/algorithm, flowcharts

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

•A coded algorithm is called a program

11

Page 12: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Creating Computer Solutions to Problems (continued)

12

Page 13: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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

▫ Meaning, what does the customer want, or what are the answers to your problem. That’s the most important part.

13

Page 14: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Analyzing the Problem

•Always search first for the output (continue)

▫From knowing what you want for the answer, you can then determine what are the questions you need to ask (input)

▫And how you are going to use (process) the input to obtain the output.

14

Page 15: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Analyzing the Problem (continued)

• Consider you have the following simple requirements about what Sarah Martin wants.

15

Page 16: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

IPO Charts

•Use an IPO chart to organize and summarize the results of a problem analysis

▫IPO: Input, Processing, and Output

16

Page 17: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Analyzing the Problem (continued)•Reduce the amount of information you

need to consider in your analysis:

17

Page 18: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

IPO Charts (continued)

•First, the output …

18

Page 19: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

IPO Charts (continued)

19

•Then the input …

Page 20: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

IPO Charts Problem Analyzing•Then you work out the processing

required.

20

Page 21: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Analyzing the Problem (continued)• But as mentioned, most requirements are

incomplete!

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

21

Page 22: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Analyzing the Problem (continued)•Distinguish between information that is

missing and information that is implied:

22

Page 23: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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

23

Page 24: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Planning the Algorithm (Con’t)

•Pseudocode is a tool programmers use to help them plan an algorithm

▫Short English statements

▫Indentation is important (as mentioned last week).

24

Page 25: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Planning the Algorithm (continued)

25

Page 26: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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▫Diamond: selection symbol

Represents decision

26

Page 27: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Planning the Algorithm (continued)

27

Page 28: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Planning the Algorithm (continued)•A problem can have more than one

solution:

28

Page 29: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Hints for Writing Algorithms

29

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

Page 30: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Hints for Writing Algorithms

30

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

Page 31: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Desk-Checking the Algorithm

31

Page 32: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Desk-Checking the Algorithm (con’t)

32

Page 33: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Desk-Checking the Algorithm (con’t)• 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 (in reality, you MUST) test an algorithm with invalid data

▫ Users may make mistakes when entering data

▫ A good programmer can foresee what type of mistakes users make.

33

Page 34: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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

34

Page 35: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

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

35

Page 36: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Homework• Pass up the following as homework by next Tuesday's lab

session.

▫ California and Vermont are just names of places.

▫ Gallons is about <4 litres (about 3.785 litres).

▫ Gas is petrol (in US slang).

• Having the book may help here. You just need to do the flowchart for the algorithm section, that’s all …

36

Page 37: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

The Gas Mileage Problem

37

Page 38: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

The Gas Mileage Problem (continued)•After planning the algorithm, you desk-

check it:

38

Page 39: Computer Programming TCP1224 Chapter 2 Beginning the Problem-Solving Process.

Reading

•Chapter 3– Completing the Problem-Solving Process and Getting Started with C++

39


Recommended