75
TMC 1413 Introduction To Programming Lecture 02: Problem Solving & Algorithms

Lecture 02: Problem Solving Algorithms. What is Problem Solving? How to solve a problem? Problem Solving Strategies Solving problem with computer

Embed Size (px)

DESCRIPTION

 What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer -Software Development Method of Problem Solving  Steps in the Software Development Method  Design & representation of algorithms  Programming errors & debugging  Program verification & testing  Program documentation

Citation preview

Page 1: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

TMC 1413Introduction To

ProgrammingLecture 02: Problem

Solving & Algorithms

Page 2: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Problem Solving??

Page 3: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

What is Problem Solving? How to solve a problem? Problem Solving Strategies Solving problem with computer

-Software Development Method of Problem Solving Steps in the Software Development

Method Design & representation of algorithms Programming errors & debugging Program verification & testing Program documentation

We will learn about:

Page 4: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Definition:◦ Problem solving is the process of transforming

the description of a problem into the solution of that problem by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem solving strategies, techniques and tools.

What is Problem Solving?

Page 5: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Problem exist all around us Can be as small as deciding where to have

lunch. Can be as big as saving a country’s faith. But all problem can be solved!! Problem solving is a knowledge building

process.

How to solve a problem?

Page 6: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Step 1: Identify the problem Step 2: Determine the problem solving

strategy to apply Step 3: Design the solution Step 4: Execute the solution Step 5: Evaluate the successfulness of the

solution

Steps to solve a problem

Page 7: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Many Strategies can be use in solving problem.

Most popular type of strategy is the “Art of War” by Sung Zhi (Ancient Chinese Philosopher)

We will learn:◦ Guess and Check (Try an Error)◦ Start at the end (Work Backward)◦ Divide and Conquer◦ Look for a Pattern

Problem Solving Strategies

Page 8: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Simplest strategy available. Just experimenting with the possible solution. A bit time consuming if have a lot of possible

solution But it is very efficient and practical for solving

small or medium problem Example

Prince Carl divided 15 stone games into two piles: games he owns and games his brother owns. He owns 3 more games than his brother. How many games does his brother own?

Guess and Check (Try an Error)

Page 9: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Solution◦ I'll guess his brother owns 8 games.◦ That means Prince Carl owns 11 games. That's a

total of 19 games.◦ My guess is too high.◦ I'll guess again. This time I'll guess his brother

owns 6 games.◦ That means Prince Carl owns 9 games. That's a

total of 15 games.◦ My guess is right.◦ His brother owns 6 games.

Page 10: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Sometime to solve some problem, you may need to undo some of the key actions in the problem.

Also known as bottom-up approach Example:

The castle kitchen servants brought in 4 pies left over from the feast. 12 pies were eaten at the feast. Queen Mab took 2 home with her. How many pies did the servants bring into the feast at the beginning?

Start at The End (Working Backward)

Page 11: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Solution◦ First, I'll account for all the pies that were eaten or

taken home.◦ 12 + 2 = 14◦ Then I'll add the 4 pies that were left over.◦ 14 + 4 + 18◦ Therefore, there must have been 18 pies at the

start of the feast.

Page 12: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Most popular strategy used in problem solving.

Used by Napoleon in his quest to conquer the world.

We often break up large problems into smaller units that are easier to handle.

Divide and Conquer

Page 13: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer
Page 14: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

More advance way to solve a complicated problem

Some problem can be solved by recognizing pattern within the domain.

For example:Daniel arranged loaves of bread on 6 shelves in the bakery. He put 1 loaf on the top shelf, 3 loaves on the second shelf, and 5 loaves on the third shelf. If he continues this pattern, how many loaves did Daniel put on the 6th shelf?

Look for a pattern

Page 15: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Solution

Shelf 1 2 3 4 5 6Loaves 1 3 5 7 9 11

Page 16: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Drawings/modeling Logical Reasoning Extra Information

Other Strategies

TIPS:STRATEGY IS ONLY TO HELP YOU

TO SOLVE PROBLEM. MOST IMPORTANT THING IS KEEP YOUR

MIND COOL AND TRY TO FIND WAYS TO OVERCOME THE

DIFFICULTIES!!

Page 17: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Computer can be programmed to do many complicated tasks at very high speeds and can store large quantities of information. Therefore, we can use computers as a tool in problem solving if one or more of the following conditions for a problem hold true:• It has extensive input;• It has extensive output;• Its method of solution is too complicated to

implement manually;• If done manually, it takes an excessively long

time to solve;• We expect to use the same method of

solution often in the future to solve the same problem with different inputs.

Solving Problem with Computer

Page 18: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Once using the computer, we need to write a program that consists of instructions corresponding to the steps of the solution such that the computer can interpret and execute them.

In developing the program, debugging it, expanding it in the future if necessary, and using it, we will have to remember certain details of the implementation. Consequently, the program has to be properly documented.

Page 19: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Thus, we use software development method which consists of the following steps:• Requirement specification• Analysis• Design• Implementation• Testing and verification• Documentation

Page 20: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Steps to solve problem Software Development Method

Step 1: Identify the problem Step 1: Requirement specification

Step 2: Identify the possible solution and determine the best applicable solution

Step 2: Analysis

Step 3: Design the solution Step 3: Design

Step 4: Execute the solution Step 4: Implementation

Step 5: Evaluate the successfulness of the solution

Step 5: Testing and verification

Step 6: Documentation

Solving a Problem VS Developing a program

Page 21: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

A documented step-by-step procedure in developing a program

Can operate under different models◦ Waterfall◦ Spiral Waterfall◦ Agile Development◦ Rapid Application Development

Software Development Method

Page 22: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Requirement Specification Analysis Design Implementation & Deployment Testing Documentation

Basic Steps in Software/Application Development

Page 23: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

A task to determine the needs and conditions (including scope) to meet in the new system

Requirements can be obtains through several methods◦ Brainstorming session◦ Survey◦ Interview◦ Observation◦ others

Requirement Specification

Page 24: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

After obtaining the requirements, then the software developer need to study the approach/ method to solve the problem.

Using different problem techniques Determine best solution from given options

Analysis

Page 25: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

A crucial phase before the actual coding process

Several thing need to be consider during the design of a application or software◦ Compatibility◦ Extensibility◦ Fault tolerance◦ Reliability◦ Reusability◦ Robustness◦ Usability

Design

Page 26: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Designing software usually is designing an Algorithm.

An Algorithm is a sequence of a finite number of steps arranged in a specific logical order, which, when executed, produce the solution for a problem.

Page 27: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

• An algorithm must satisfy some requirements:– Unambiguousness

– It must not be ambiguous. In most cases we develop an algorithm so that it can be translated into program to be executed by computers. Computers cannot cope with ambiguous. Therefore, every step in an algorithm must be clear as to what it is supposed to do and how many times it is expected to be executed.

Page 28: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

– Generality– It must have generality. A procedure that prints the

message “One inch is 2.54 cm” is not an algorithm; however, one that converts a supplied number of inches to centimeters is an algorithm.

– Correctness– It must be correct and must solve the problem for

which it is designed. – Finiteness

It must execute its steps and terminate in finite time. An algorithm that never terminates is unacceptable.

Page 29: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Usual tools in designing and documenting algorithms◦ Pseudocode

a semiformal, English-like language (or other language that can be understand by human being) with a limited vocabulary that can be used to design and describe algorithms.

◦ Flowchart a graph consisting of geometrical shapes that are

connected by flow lines.

Page 30: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

A pseudocode can be used for:◦ Designing algorithms◦ Communicating algorithms to users◦ Implementing algorithms as programs◦ Debugging logic errors in program◦ Documenting programs for future maintenance and expansion

purposes

To succeed in accomplishing its objectives, a pseudocode must meet these requirements:◦ Have a limited vocabulary◦ Be easy to learn◦ Produce simple, English-like narrative notation◦ Be capable of describing all algorithms, regardless of their

complexity

Pseudocode

Page 31: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

31

An algorithm can be written in pseudocode using six (6) basic computer operations:

A computer can receive information. Typical pseudocode instructions to

receive information are:Read nameGet nameRead number1, number2

How to write Pseudocode

Page 32: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

32

A computer can output (print) information. Typical pseudocode instructions are:

Print nameWrite "The average is", ave

A computer can perform arithmetic operationTypical pseudocode instructions:

Add number to totalTotal = Total + NumberAve = sum/total

How to write Pseudocode

Page 33: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

33

A computer can assign a value to a piece of data:

To assign/give data an initial value:Initialize total to zeroSet count to 0

To assign a computed value: Total = Price + Tax

How to write Pseudocode

Page 34: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

34

A computer can compare two (2) pieces of information and select one of two alternative actions.

Typical pseudocode e.g.

If number < 0 then add 1 to neg_number else add one to positive number end-if

How to write Pseudocode

Page 35: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

35

A computer can repeat a group of actions.

Typical pseudocode e.g. Repeat until total = 50 read number write number add 1 to total end-repeat

OR while total < = 50 do:

read numberwrite number

end-while

How to write Pseudocode

Page 36: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

StartRead status

if status is equal to 1 print “on”

ElseIf

status is equal to 0print “ Off”

elseprint “Error in status code”

end_if

end

Example of pseudocode

Page 37: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Algorithm in Real LifeConsider the following ….

Problem: Baking a CakeHow to solve:

1.Start2.Preheat the oven at 180oC3.Prepare a baking pan4.Beat butter with sugar5.Mix them with flour, eggs and essence vanilla6.Pour the dough into the baking pan7.Put the pan into the oven8.End

Page 38: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

  

Problem: Prepare a Breakfast

1. Start2. Prepare a Breakfast3. End

Page 39: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

1. Start2. Prepare a Breakfast2.1. Prepare a tuna sandwich2.1.1 Take 2 slices of bread2.1.2 Prepare tuna paste 2.2. Prepare some chips2.2.1 Cut potatoes into slices2.2.2 Fry the potatoes2.3. Make a cup of coffee2.3.1 Boil water2.3.2 Add water with sugar and coffee3. End

Page 40: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

What is the connection between these real life processes and algorithm?

Something to ponder …

Page 41: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Algorithm• A specific and step-by-step set of instructions for

carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at some point

• 2 types of algorithm representation will be explained:o Flowcharto Pseudocode

Page 42: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Flowchart is another technique used in designing and representing algorithms.

It is an alternative to pseudocode; whereas a pseudocode description is verbal while a flowchart is graphical in nature.

Flowchart

Page 43: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Sub-module / Function

(Barred Rectangle)

Flow DirectionInput/Output

ConnectorProcess

DecisionStart/Stop

RepresentationSymbol

RepresentationSymbol

Page 44: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

44

This symbol shows where a program begins and ends (program or module).

• Labeling: When used to identify the beginning and end of a whole program, the beginning terminal is labeled with start and the ending terminal is labeled with end or stop. When the flowchart is for a module, the beginning terminal is labeled with the module name and the ending terminal is labeled with return or exit.

Page 45: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

45

Used to show tasks such as calculations, assignment statements, incrementing, etc.

Labeling: Processes are labeled with the statement the task to be performed, for example:

totalSales = subTotal + pstAmount + gstAmountArea = Length * Width firstName = "Sally"

Area = Length * Width

Page 46: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

46

Used to show an operation that brings data into a program, or an operation that sends data out of the program.

For example, getting values from the user or printing something on the screen.

Labeling: Input/output symbols are labeled with the statement that receives the input or generates the output, which could also include opening files and devices. Examples:get stdNumber

get stdNumber

Page 47: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

47

Used to identify a point in the program where a condition is evaluated. Conditions are used in if statements and looping.

• Labeling: Decisions are labeled with the condition that they are testing, for example:numRecords > 10foundMatch = true

numRecords > 10

Page 48: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

48

Used to connect two segments of flowchart that appear on the same page.

This is done when your flowchart runs to the bottom of the page and you're out of room: end it with an on-page connector and then place another on-page connector in a free spot on the page, and continue the flowchart from that connector.

• Labeling: On-page connectors are labeled with upper-case letters. The connector at the end of a segment of flowchart will match the connector that identifies the rest of the flowchart.

• For example, when you run out of room, end the flowchart with a connector labeled "A". The flowchart continues at the matching connector also labeled "A".

Page 49: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

49

Used to specify a function/module call or a group of related statements.

Example Start

Submodule

End

Submodule

Return

Page 50: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

50

A

A

Page 51: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

51

Sequence structure

statement_1 statement_2 statement_n…

Selection structure

condition

then-part

else-part

condition

then-part

Yes

Yes

No

No

-------------------------------------------------------------------

Page 52: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

52

Repetition structure

condition

loop-bodyYes

No

Page 53: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

53

The benefits of flowcharts are as follows:CommunicationFlowcharts are better way of communicating the logic of a system to all concerned.

Effective analysisWith the help of flowchart, problem can be analyzed in more effective way.

Proper documentationProgram flowcharts serve as a good program documentation, which is needed for various purposes.

Efficient CodingThe flowcharts act as a guide or blueprint during the systems analysis and program development phase.

Proper DebuggingThe flowchart helps in debugging process.

Efficient Program MaintenanceThe maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

Page 54: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Are the steps in the algorithm discussed earlier specific enough to be executed by computer?

Something to ponder …

Page 55: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Problem Solving Process

Input Process Output

Page 56: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Example 1Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.

• Quantity• Price_per_kg

PricePrice = Quantity * Price_per_kg

Input Process Output

Page 57: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Flowchart: Calculate Price of Apples

InputQuantity

Start

Price Quantity * Price_per_kg

InputPrice_per_kg

OutputPrice

End

Page 58: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

void main() {

scanf(“%d”,&quantity);

scanf(“%d”,&price_per_kg);

price = quantity*price_per_kg;

printf(“%d”, price);

}

C Program: Calculate Price of Apples

It’s not complete! Declare the variables…

Page 59: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

In this phase, we implement the algorithm in a programming language.

During implementation we translate each step of the algorithm into a statement in that particular language and end up with a computer program.

Implementation

A computer program is a sequence of a finite number of statements expressed in a programming language in a specific logical order that, when executed, produce the solution for a problem.

Page 60: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

60

Programming Errors • Another challenge that awaits is program debugging.

Debugging is defined as the process of finding and correcting errors in computer programs.

• No matter how careful you are as a programmer, most programs you write will contain errors. Either they won’t compile or they won’t execute properly.•This situation is something that happens very frequently to every programmer. You should take program debugging as a challenge, develop your debugging skills, and enjoy the process.

There are three types of programming errors:

• Syntax errors• Logic errors• Run-time errors

Page 61: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

61

Syntax error:◦ is violation of syntax rule, which define how the elements of a

programming language must be written.◦ They occur during the implementation phase and are detected by the

compiler during the compilation process.◦ Another name for syntax error is compilation error.

Logic error:◦ occur during the analysis, design, and implementation phases.◦ We may choose an incorrect method of solution for the problem to be

solved, mistakes in translating an algorithm into a program or design erroneous data for the program.

Run-time error:◦ Are detected by the computer while the program is being executed.◦ They are caused by program instructions that require the computer to do

something illegal, such as attempting to store inappropriate data or divide a number by zero.

◦ When a run-time error is encountered, the computer produces an error diagnostic message and terminates the program execution.

Page 62: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

To fix the program error, programmer will do debugging task by using debugger

Debugging process can be very frustrating especially involving huge amount of coding

Good to have good programming style and documentation in coding!

Debug

Page 63: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Debugger◦ is a computer program that is used to test

and debug other programs. ◦ Usually comes together with compilers◦ Debugger will help programmer by provide list of

error in compiling the program◦ Still depends on programmers to solve the

problem!!

Page 64: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Debugging technique◦ Probe

Put an external variable inside the program and try to force the program to output result in the middle.

Good to trace modular logic error or data error◦ Trace

Back-tracking or front-tracking Go step-by-step (or line-by-line) to detect error. Good in tracking logic error and

Page 65: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

65

Testing and Verification• In this phase, the main objective is to convince yourself and

eventually your clients that the program will do what it is expected to do. In other words, you will want to verify that your program is correct.

Program verification is the process of ensuring that a program meets user requirements.

• One of the techniques that can be used for program verification is program testing.

Program testing is the process of executing a program to demonstrate its correctness.

• A program must be tested using a sufficiently large sample of carefully designed test data sets such that every logical path in the program is traversed at least once.

• You must continue to test your program until you are sure that all statements in it are functioning correctly.

Page 66: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

66

Program documentation consists of these elements:◦ A concise requirements specification◦ Descriptions of problem inputs, expected outputs,

constraints, and applicable formula◦ A pseudocode and flowchart for its algorithm◦ A source program listing◦ A hardcopy of a sample test run of the program◦ A user’s guide explaining to non-programmer users how the

program should be used. Documentation can and should be done within the coding

itself by using comments!!

Documentation

Page 67: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

End of Lecture 2

Yes !! That’s all? What’s next???

INTRODUCTION TO C on the way …

Page 68: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

1.Design the algorithm for making a pot of tea

Example of Problem Solving

Page 69: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Pseudocode:Start

Fill a kettle with waterBoil the water in the kettlePut the tea leaves in the potPour boiling water in the pot

End

Answer:

Page 70: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Flowchart Start

Fill a kettle with water

Boil the water in the kettle

Put the tea leaves in the pot

Pour boiling water in the pot

End

Page 71: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

2. Design the algorithm for calculating the perimeter of a circle

Page 72: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Pseudocode:Start

Input RadiusCalculate perimeter = 44/7 * RadiusPrint Perimeter

End

Answer:

Page 73: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Flowchart

Start

Perimeter = 44/7 * Radius

Input Radius

Print Perimeter

End

Page 74: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Any Questions?

Page 75: Lecture 02: Problem Solving  Algorithms.  What is Problem Solving?  How to solve a problem?  Problem Solving Strategies  Solving problem with computer

Thank You