Lecture 18: Algorithms and Design
Monday, April 04, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 5.10 - 5.11
Homework Due: Homework 08
Homework Assigned: Homework 09
Classwork Assigned: None
Topics Covered:
- Quiz 3 on Monday - Topics
- Homework 8 Solution and Solution with extra credit
- Classwork 10 Solution and Solution with extra credit
- Algorithms
- Algorithms are a series of steps used to accomplish a goal
- Each of our programs is an implementation of an algorithm
- Algorithms take a set of inputs, and will generate a set of outputs
- Part of computer science is understanding a description of a problem, and developing an algorithm to solve the problem
- Design of algorithms
- How should you approach designing an algorithm?
- Requirements
- Most important part is understanding your requirements
- Requirements - what does your algorithm need to do?
- If there are questions on the requirements, ask them early
- Read through all of a problem, and make sure you understand what the program should do, before you start
- Unlike a computer language, English is imprecise
- Top-down design (section 3.3 of book)
- Start with high level view of problem
- Break problem up into smaller sub-problems
- Break sub-problems up into smaller sub-sub-problems
- Continue until you have a problem you know how to easily solve
- Implement the solution to the easy problem
- Now go back up a step, and use the solution to the easy problem to solve the larger problem
- Example from our homework 08:
Problem: Draw a diamond consisting of stars Sub-problem: Print a line of spaces and stars Sub-Sub-Problem: Print a number of spaces Sub-Sub-Sub-Problem: Print one space
- The flow of how the resulting algorithm will work:
/* Draw diamond */ loop over number of lines to print for our diamond { /* Draw a line */ loop over number of spaces to print { print a space } loop over number of stars to print { print a star } print newline }
- Note: Look for similar code and try to make then functions
- Example above: printing a character a set number of times
- You probably want to test the low level solutions before you use them to solve higher level problems
- Limiting problem space
- Also look at limiting problem space to make the problem easier
- I got the top of diamond working, then tried to get bottom correct
- In classwork to print sin wave, I got positive amplitude working, then worked on negative amplitude
- Ignore the hard parts until the easy part is done
- Leave comments in your code to remind you what you still need to do
- An example to design in class
- Problem: Write a program to calculate the minimum score that you must get in future assignments to get a given grade in the class
- ...