Problem Solving Chapter 2
What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating
What does a programmer do? n Understand the problem specification n Develop a detailed logical plan (algorithm) for solving the problem at hand n Translate the algorithm into a programming language n Create test data to determine the correctness of their algorithm/program n Test & debug the algorithm/program n Document the program
Levels of algorithm development n Global - hierarchy diagram n Local - flowchart n Whatever you do…you MUST… RESIST THE URGE TO CODE!!!
Structured program design n Also called top-down or modular programming n Idea is: –sketch a global, modular solution hierarchy diagram or pseudocode –refine each module independently flowcharts or pseudocode n There are many advantages to this approach
In Visual Basic (or any language that supports building GUI’s) n An event procedure (or any user- defined procedure) is a module. n In development, the programmer’s focus is the GUI - its design drives all programming decisions & design
A well-written program is... n Modular (structured) n Well-designed (good algorithm) n Easy to follow –code is “clean” –simple –uses meaningful names & symbols –documented
Global design n Hierarchy diagrams
Local design - flowcharts Start/Stop block Processing block Decision block Input/Output block
Logical constructs n There are just three logical constructs used in well-designed flowcharts & structured programs: –sequence (Fig. 2.1, p. 36) –selection (Fig , p ) –iteration (Fig , p )
Sequential execution n A series of I/O & processing blocks, with the flow of execution passing straight from the top (start block) to the bottom (stop block) - no twists or turns n Why is this limiting?
Conditional branching n The flow of execution takes different turns, on the basis of certain conditions being met… n What is a condition?
start stop Read x,y x > y? yesno Write y Write x
Three types of if statements... n If - then - else –Compare two numbers x & y and display the larger of the two. n Null else –If y <> 0, divide x by y and put the quotient in z (otherwise, do nothing at all) n Nested –Make further comparisons
The assignment statement variable = expression n variable is a symbolic name that represents a memory location n expression is any combination of variables and/or constants connected by arithmetic operators +,-,*,/,**,^
Let’s do some examples…….