Algorithms III TopDown Design CMSC 104 1 Pseudocode

  • Slides: 17
Download presentation
Algorithms III Top-Down Design CMSC 104 1

Algorithms III Top-Down Design CMSC 104 1

Pseudocode and Then Some We can write an algorithm for finding the average of

Pseudocode and Then Some We can write an algorithm for finding the average of two integers: Get an integer, num 1, from the user Get an integer, num 2, from the user sum = num 1 + num 2 average = sum / 2 Print “The average of “ num 1 “ and ” num 2 “ is ” average. l Steps 1, 2 & 5 will make use of code found CMSC 104 in the run-time library. l 2

Investigating Steps 1 & 5 Get an integer, num 1, from the user Print

Investigating Steps 1 & 5 Get an integer, num 1, from the user Print “The average = “ average. l Are these really single steps? l Each of them is really a sequence of steps, a subprogram. Two kinds of subprograms: CMSC 104 o Functions - an answer is returned to the main program. o Procedures - no answer returned to the main program 3

Functions and Procedures In our example, the function that gets a value from the

Functions and Procedures In our example, the function that gets a value from the user and the procedure that prints out the answer were already written. l Many times it is necessary for us to write our own procedures or functions. l CMSC 104 4

More Problem Solving If we look at the problem as a whole; it may

More Problem Solving If we look at the problem as a whole; it may seem impossible, because it is so complex. l Complex problems can be solved using a Top-Down approach aka Stepwise refinement l o Break the problem into parts o Then break the parts into parts o Soon each of the parts will be easy to do CMSC 104 5

Advantages of Top-down Design l l l CMSC 104 Breaking the problem down into

Advantages of Top-down Design l l l CMSC 104 Breaking the problem down into parts helps us to clarify what needs to be done. At each step of refinement, the new parts become less complicated and therefore easier to figure out. What if you’ve broken a problem down as much as you can and you still don’t know? o Missing information o More detail is needed 6

A Practical Example l Scenario: o We own a home improvement company o We

A Practical Example l Scenario: o We own a home improvement company o We do painting, roofing and basement waterproofing o A section of town has recently had flooding o We want to send out waterproofing pamphlets to our customers in that area CMSC 104 7

Our Algorithm Get the customer list from a file l Sort the list according

Our Algorithm Get the customer list from a file l Sort the list according to zipcode l Make a new file of only the customers with the zipcode 21222 from the sorted customer list l Print an envelope for each customer in the new file l CMSC 104 8

Single steps ? None of our steps are single steps l Employ Top-Down Design

Single steps ? None of our steps are single steps l Employ Top-Down Design l Start with the large idea and break into large parts l Each part will end up being a function or a procedure l CMSC 104 9

Pseudocode for our Main Program Call Get Customer List l Call Sort on customer

Pseudocode for our Main Program Call Get Customer List l Call Sort on customer list by zipcode l Call Make Target List from customer list using only zipcode 21222 l Call Print Envelopes using target list l CMSC 104 10

The Next Step Write algorithms for each of the functions or procedures. l These

The Next Step Write algorithms for each of the functions or procedures. l These may include steps that are really several steps, too. l Give names to these new functions and procedures. l Write algorithms for them. l Continue this process until done. l CMSC 104 11

Structured Programs l l CMSC 104 Top-down design will be used for all of

Structured Programs l l CMSC 104 Top-down design will be used for all of our programming projects This is the standard way of writing programs. Programs produced using this method and using only the 3 kinds of structures, sequential, selection and repetition are called structured programs. Structured programs are easier to modify later and also easier for other programmers to understand. 12

Another Example l CMSC 104 Problem: Write a program that draws this picture of

Another Example l CMSC 104 Problem: Write a program that draws this picture of a house. 13

The Algorithm Draw the outline of the house l Draw the chimney l Draw

The Algorithm Draw the outline of the house l Draw the chimney l Draw the door and doorknob l Draw the windows l CMSC 104 14

Similarities All three windows are identical. l They are simply in different positions. l

Similarities All three windows are identical. l They are simply in different positions. l We can write a function called Draw. Window that will draw one window at a position we specify and call it 3 times with the three different positions. l Another benefit of Top-down design is reuseability. We are reusing the code for Draw. Window. l CMSC 104 15

The Pseudocode Call Draw. House. Outline giving a position l Call Draw. Chimney giving

The Pseudocode Call Draw. House. Outline giving a position l Call Draw. Chimney giving position l Call Draw. Door giving position l Call Draw. Window giving position #1 l Call Draw. Window giving position #2 l Call Draw. Window giving position #3 l CMSC 104 16

Problem Solving Techniques l We have investigated three problem solving techniques: • Multiple attacks

Problem Solving Techniques l We have investigated three problem solving techniques: • Multiple attacks • Working backwards • Top-down design CMSC 104 17