problem solving techniques. compiler n is a computer program whose purpose is to take a description...

25
Problem Solving Problem Solving Techniques Techniques

Upload: john-wilkinson

Post on 03-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Problem Solving Problem Solving TechniquesTechniques

Page 2: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

CompilerCompiler

Is a computer program whose purpose Is a computer program whose purpose is to take a description of a desired is to take a description of a desired program coded in a programming program coded in a programming language and generate the instructions language and generate the instructions from the computer from that code.from the computer from that code.

The program description is called The program description is called source code or C code.source code or C code.

Compiled program is called object code Compiled program is called object code or machine codeor machine code

Page 3: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Problem solvingProblem solving

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

Page 4: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Method for Problem Method for Problem SolvingSolving

Requirement SpecificationRequirement Specification AnalysisAnalysis DesignDesign ImplementationImplementation Testing and VerificationTesting and Verification DocumentationDocumentation

Also known as Software development Also known as Software development methods or Software Life Cyclemethods or Software Life Cycle

Page 5: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Requirement SpecificationRequirement Specification

– Eliminated ambiguities in the problem Eliminated ambiguities in the problem statementstatement

– understanding exactly what the problem isunderstanding exactly what the problem is– what needed to solve itwhat needed to solve it– what are the solution should providewhat are the solution should provide– any constraint or special conditionsany constraint or special conditions

how precisely you define the problem how precisely you define the problem is depend on degree of familiarity on is depend on degree of familiarity on the problem.the problem.

Page 6: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

AnalysisAnalysis

identified problem inputs, outputs and identified problem inputs, outputs and collected information about the inputscollected information about the inputs

Formulas or equations to be used.Formulas or equations to be used. If you can acquired a clear If you can acquired a clear

understanding of the problem and understanding of the problem and have a precise requirements have a precise requirements specification usually will make analysis specification usually will make analysis phase easyphase easy

Page 7: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

DesignDesign

developed a series of steps with a developed a series of steps with a logical order which, when applied logical order which, when applied would produce the output of the would produce the output of the problemproblem

using algorithms represented as using algorithms represented as flowchart or pseudocodeflowchart or pseudocode

Page 8: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

ImplementationImplementation

Implementation of the algorithm in a Implementation of the algorithm in a programming language.programming language.

Translate algorithm into a statement of Translate algorithm into a statement of programming languageprogramming language

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

Page 9: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Implementation - Implementation - programming errorsprogramming errors

There are three types of programming There are three types of programming errorserrors– design, syntax and run-time errordesign, syntax and run-time error

design errors occur during the design errors occur during the analysis, design, and implementation analysis, design, and implementation phasesphases– incorrect method, mistakes on translating incorrect method, mistakes on translating

algorithm & design erroneous dataalgorithm & design erroneous data– difficult to detectdifficult to detect

Page 10: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Syntax errorsSyntax errors

Are violations of syntax rules, Are violations of syntax rules, which define how the elements of which define how the elements of a programming language must be a programming language must be writtenwritten– occur during implementation phaseoccur during implementation phase– detected by compilerdetected by compiler– error message displayerror message display

Page 11: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Run-time errorsRun-time errors

Are detected by the computer Are detected by the computer while your program is being while your program is being executed.executed.

Cause by instruction that require Cause by instruction that require the computer to do something the computer to do something illegal such as attempting to store illegal such as attempting to store inappropriate data or divide a inappropriate data or divide a number by zeronumber by zero

Page 12: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

What is a program ?What is a program ?

Is a set of instructions for a Is a set of instructions for a computercomputer

receive data, carry out the receive data, carry out the instructions, and produce useful instructions, and produce useful resultsresults

Page 13: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

AlgorithmsAlgorithms A clear and precise procedure for solving a given A clear and precise procedure for solving a given

problem, and although the word algorithm evolved problem, and although the word algorithm evolved from the name of 8th century Arabian mathematician, from the name of 8th century Arabian mathematician, the idea pre-dates him by over a millennia. In order to the idea pre-dates him by over a millennia. In order to solve a problem on a computer, the corresponding solve a problem on a computer, the corresponding algorithm must exist as a series of well-defined steps. algorithm must exist as a series of well-defined steps. A lot of research in computer science is involved with A lot of research in computer science is involved with finding algorithms which :finding algorithms which :– solve new problems,solve new problems,– solve old problems more efficiently, orsolve old problems more efficiently, or– produce approximate solutions to problems for produce approximate solutions to problems for

which no known algorithm exists.which no known algorithm exists.

Page 14: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Algorithm DesignAlgorithm Design

4 steps must be carried out4 steps must be carried out– understand the problemunderstand the problem– get an idea of how an algorithmic get an idea of how an algorithmic

procedure might solve the problemprocedure might solve the problem– formulate the algorithm and represent formulate the algorithm and represent

it as a programit as a program– evaluate the program for accuracy and evaluate the program for accuracy and

for its potential as a tool for solving for its potential as a tool for solving other problems.other problems.

Page 15: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

PseudocodingPseudocoding Is a semiformal, English-like language with Is a semiformal, English-like language with

a limited vocabulary that can be used to a limited vocabulary that can be used to design and describe algorithmsdesign and describe algorithms

can be used forcan be used for– designing algorithmsdesigning algorithms– communicating algorithms as programscommunicating algorithms as programs– implementing algorithms as programsimplementing algorithms as programs– debugging logic errors in programdebugging logic errors in program– documenting programs for future maintenance documenting programs for future maintenance

and expansion purposesand expansion purposes

Page 16: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

PseudocodePseudocode

Have limited vocabularyHave limited vocabulary be easy to learnbe easy to learn produce simple, English -like produce simple, English -like

narrative notationnarrative notation be capable of describing all be capable of describing all

algorithms, regardless of their algorithms, regardless of their complexitycomplexity

Page 17: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Coventions For Coventions For PseudocodingPseudocoding

Should includes keywords that describe operations Should includes keywords that describe operations and some appropriate, English-like descriptions of and some appropriate, English-like descriptions of operandsoperands

each pseudocode should be written on a separate line. each pseudocode should be written on a separate line. If a statement requires more than one line, the If a statement requires more than one line, the continuation lines should be indentedcontinuation lines should be indented

the statement that make up a sequence control should the statement that make up a sequence control should be start with compute, set or initializebe start with compute, set or initialize

statements in a sequence structure can be grouped statements in a sequence structure can be grouped into a block by enclosing them between the keywords into a block by enclosing them between the keywords begin and end. Such statement should be indented.begin and end. Such statement should be indented.

Terminate each if statement with the keyword end_ifTerminate each if statement with the keyword end_if

Page 18: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Coventions For Coventions For PseudocodingPseudocoding

For the repetition control structure, use the For the repetition control structure, use the while statement and indent the loop-body. while statement and indent the loop-body. Terminate each while statement with the Terminate each while statement with the keyword end_whilekeyword end_while

All words in pseudocode statement must be as All words in pseudocode statement must be as easy as possible for nonprogrammers to easy as possible for nonprogrammers to understandunderstand

Page 19: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

FlowchartFlowchart

Flowchart is simply a graphical method for Flowchart is simply a graphical method for representing the program’s flow, and is representing the program’s flow, and is language independent;language independent;

Each box in a flowchart represents a Each box in a flowchart represents a particular type of instruction, action or data particular type of instruction, action or data store, and arrows are drawn between the store, and arrows are drawn between the boxes to show hoe the program flow occurs boxes to show hoe the program flow occurs between statements. Sometimes the between statements. Sometimes the arrows are labeled with text to show arrows are labeled with text to show whether a decision was true or false.whether a decision was true or false.

Page 20: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Basic Control & Looping Basic Control & Looping ConceptsConcepts

If …then…elseIf …then…else while endwhile end do ….whiledo ….while for...nextfor...next loop …untilloop …until

Page 21: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Examples of Problem Examples of Problem SolvingSolving

Page 22: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Obtain 2 numbers from Keyboard, Obtain 2 numbers from Keyboard, compute the sun of the numbers and compute the sun of the numbers and

display the resultsdisplay the results

Input the first numberInput the first number Input the second numberInput the second number Add the two numbersAdd the two numbers Output the sumOutput the sum

Page 23: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Obtain two numbers from the keyboard, Obtain two numbers from the keyboard, and determine and display which (if and determine and display which (if

either) is the larger of the two numberseither) is the larger of the two numbers

Input the first number from the keyboardInput the first number from the keyboard

Input the second number from the keyboardInput the second number from the keyboard

If the first number is greater than the second numberIf the first number is greater than the second numberprint itprint it

Else if the second number is greater than the first Else if the second number is greater than the first numbernumber

print itprint it

ElseElse

print a message starting that the numbers are equalprint a message starting that the numbers are equal

End IfEnd If

Page 24: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

Obtain a series of +ve number from the Obtain a series of +ve number from the keyboard, and determine and display the keyboard, and determine and display the sum of the numbers. Assume that the user sum of the numbers. Assume that the user types the sentinel value -1 to indicate “end types the sentinel value -1 to indicate “end of data entry”of data entry”

dodo

Input a value from the keyboardInput a value from the keyboard

while value is < 0 and value not = -1while value is < 0 and value not = -1

While the input value is While the input value is not equal to -1 not equal to -1

add the number to the running totaladd the number to the running total

dodo

input the next numberinput the next number

while value < 0 and not equal = -1while value < 0 and not equal = -1

End WhileEnd While

Print the sumPrint the sum

Page 25: Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language

More examples…will More examples…will continue on next lecturecontinue on next lecture