programming fundamentals --> ch1. problem solving 1 programming fundamentals (750113) ch1....

24
Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Upload: ashlynn-clark

Post on 22-Dec-2015

264 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

1

Programming Fundamentals(750113)

Ch1. Problem Solving

Page 2: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

2

Problem Solving

• Programming is a process of problem solving (Problem Solution by computer)

• Algorithm ? Step-by-step problem-solving process

Solution achieved in finite amount of time

Page 3: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

3

Problem Solving Process

• Phase 1 - Analyze the problem Outline the problem and its requirements Design (algorithm) to solve the problem ( Flow chart, pseudo code) Algorithm tracing

• Phase 2 - Implement the algorithm Implement the algorithm in code (in Programming Language

Program) Verify that the algorithm works

• Phase 3 - Maintenance Use and modify the program if the requirements changes

Page 4: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

4

Analyze the Problem (1)

Outline the problem and its requirements

• Understand the problem• Understand problem requirements

Does program require user interaction? Does program manipulate data? What is the output? are all possible circumstances handled?

• If the problem is complex, divide it into subproblems Analyze each subproblem as above

Page 5: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

5

Analyze the Problem (1)

Design Algorithm1. Flowcharts

2. pseudo-code

Page 6: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

6

Flow Chart Symbols

Start and End

Input / output

Selection

Calculation

Data Flow

• A Flowchart is An algorithm graphical representation. Written as a combination of the following graphical

notations:

Page 7: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

7

• Pseudo-code:<Algorithm name>

// input ? The comment lines “//”

// function?

// Output?

Begin

<data definition>

<actions>

End

Page 8: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

8

Analyze the Problem (1)

Algorithm Tracing Draw flowchart Find all possible paths Check each path with appropriate input data Observed Outputs not conform to the expected ones

error in the algorithm.

Page 9: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

9

Analyze the Problem (2)

Problem Example.Convert a student mark from decimal mode to ABC mode.

Understand problem requirements

Is the problem statement complete and unambiguous?

Does program require user interaction? Input the mark

Does program manipulate data? covert mark

What is the output? The mark converted to A or B or C or error

Is there subproblem? No

Page 10: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

10

Analyze the Problem (2)

Efficiency:- an algorithm may work correctly but be inefficient – by taking

more time and using more resources than required to solve the problem.

- becomes more important for larger programs.

Page 11: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

11

Analyze the Problem (3)

Design Algorithm Algorithm Discovery

• To discover an algorithm is to solve the problem!

1. Working backwards (modification of an old solution)Example: Min of a and b is known. Deduce Max a and b.

2. Look for a related problem that has been solved before (similar solutions reuse)

The precedent algorithm of mark conversion is for single student. It may be generalized to many students

3. Stepwise Refinement (new solution) Break the problem into several sub-problems Solve each subproblem separately Produces a modular structure

Page 12: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

12

Analyze the Problem (3)

•Example. Making tea. Suppose we have a robot which carries out household tasks. We wish to program the robot to make a cup of tea. An initial attempt at an algorithm might be:

1. Put tea leaves in pot

2. Boil water

3. Add water to pot

4. Wait 5 minutes

5. Pour tea into cup

Page 13: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

13

Analyze the Problem(3)

Design Algorithm Algorithm Discovery Strategies Stepwise Refinement

These steps are probably not detailed enough for the robot. We

therefore refine each step into a sequence of smaller steps:

1. Put tea leaves in pot might be refined to

1.1 Open box of tea

1.2 Extract one spoonful of tea leaves

1.3 Tip spoonful into pot

1.4 Close box of tea

Page 14: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

14

Analyze the Problem(3)

Design Algorithm Algorithm Discovery Strategies Stepwise Refinement

2. Boil water might be refined to

2.1. Fill kettle with water

2.2 Switch on kettle

2.3 Wait until water is boiled

2.4 Switch off kettle

…..

5. Pour tea into cup might be refined to

5.1. Pour tea from pot into cup until cup is full

Page 15: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

15

Page 16: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

16

Analyze the Problem(3)

•the designer must know when to stop refining.

•In this example the instruction Switch on kettle is directly executable by the robot, but that Fill kettle with water is not.

•Experience will tell us when a step is directly implementable or not.

•The above algorithm consists of a sequence of steps, executed exactly once and in order

Page 17: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

17

Analyze the Problem(3)•What happens if the tea-box is empty?

Control Requirements: IF• If the tea-box is empty we wish to specify an extra step:

Get new box of tea from cupboard— This step would not be carried out unless the tea-box is empty,We can express this by rewriting step 1.1 as1.1.1. Take tea box from shelf1.1.2. If box is empty then get new box from cupboard1.1.3. Remove lid from box

— Step 1.1.2 expresses both the step to be selected and the

condition under which this selection should be made.

• More complicated conditions can use AND, OR, NOT

Page 18: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

18

Analyze the Problem(3)

•Control requirements: while

Another common requirement is the need for iteration.

— Example. Repeat the mark conversion for a class room of 30 students. Each student having a unique student number from 1 to 30.

Page 19: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

19

Implement the algorithm

Use any programming language (C++) to write a program to solve the problem. Where program is a sequence of statements.

Use any programming language (C++) to write a program to solve the problem. Where program is a sequence of statements.

Page 20: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

20

Computer Stage: after completing the program, the computer must do the following:

- Compilation: the computer compile the program. If it has errors, then an error list is produced to be corrected by the programmer.

- Execution: when the program become don’t has errors, the computer execute it to produce the ……output.

Page 21: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

21

Implement the algorithm

Verify that the algorithm works source of errors

• Many errors made in:- analyzing the problem, - developing an algorithm, and/or - coding the algorithm

Page 22: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

22

Implement the algorithm

• Errors are of three types:. syntax errors. run-time errors. logic errors

• Syntax errors: detected by the C compiler. source code does not conform to one or more of C’s

grammar rules. examples of syntax errors: undeclared variable, …• Often one mistake leads to multiple error messages –

can be confusing

Page 23: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

23

Implement the algorithm

• Run-time errors. detected and displayed by computer during execution.

- Occur when program directs computer to perform illegal operation. Example: int x=y/0;

- will stop program execution and display message

• Logic errors.

- caused by faulty algorithm

- sign of error: incorrect program output- cure: thorough testing and comparison with expected

resultsA logic error is referred to as a bug, so finding logic errors is called debugging.

Page 24: Programming Fundamentals --> Ch1. Problem solving 1 Programming Fundamentals (750113) Ch1. Problem Solving

Programming Fundamentals --> Ch1. Problem solving

24

Conclusions (Cont’d)