Week 7 Tutorial

Assignment 0 Debrief

Well done everyone for such a great effort in the first assignment. Take a moment to think about what you have been doing. Did you learn anything from the process of doing the assignment (ie disregard the actual content of the assignment - did you have any insights into the act of programming?)

Discuss as a class: If you could go back in time and do the assignment again, what would you do differently?

Abstraction

Abstraction is possibly the most important skill you will develop as a programmer. Abstraction is about being able to contain complex process or algorithm into a single unit of code and within that unit break the process up into a collection of small, easy to understand, functions and data structures.

The large unit should be easy to understand as a whole on a more abstract level, but each component, and each single function, should be so simple that understanding it is equally easy.

The skill of abstraction will allow you to build programs far more complex and capable than assignment 0, using the just the tools you have learned in this course.

One of the most important rules from the course style guide is that every function should have one specific purpose. You should be able to describe what a function does with one simple statement.

If that statement does not sound like a simple function to implement, break it up into steps and make a function for each step.

No function should be long or complex. A good measure of complexity is how many parts a control flow diagram has. If it has many parts, break it up into stages and each stage into its own function. Similar stages can probably be implemented with a single version of a function.

Discuss with your class some strategies you can use to keep your code from becoming complex and strategies for making sure that your code is easy to understand. Discuss how you might be able to tell if your code is becoming too complex or hard to understand.

How could you improve your assignment code using the strategies you discussed?

Australia’s Funniest Home Flow Control Fragments

In pairs write some code fragments using the control flow blocks you have learnt so far: including functions, if/else blocks, and while loops.

Include an equal mix of logic, style, and syntax errors, and correct fragments.

(It’s better to write a lot of small code fragments with one or two errors each rather than one or two code fragments with a lot of errors each.)

Swap your fragments with another group and see which errors you can spot. Discuss together in your combined group of four and then pick the best ones to test the whole tutorial class. Prizes, fame, glory.

Assignment 1 - Mandelbrot

The next assignment is to build a Mandelbrot Set viewer, similar to almondbread.

You will complete the assignment with your current lab pair.

Make sure you and your partner understand how to calculate the mandelbrot set, and know what the two .h files you have to implement and what the implemented functions need to do.

One of the functions you need to implement is

int escapeSteps(double x, double y);

What should the output be for the following x,y inputs (assuming MAX_ITERATIONS is 256).

0, 0

-2, -2

-1 1

Discuss how this function is useful for creating your Mandelbrot viewer.

More Details: The Mandelbrot Set

The Mandelbrot Set is a complex system, that arises from a very simple algorithm.

Any point in 2-dimensional complex space is either in the Mandelbrot Set or not in the Mandelbrot Set.

To determine if a complex number \(\mathbf{z}\) we can use the following loop:

If the loop ever exits, then the complex number \(\mathbf{z}\) is not in the Mandelbrot Set.

This issue with this algorithm is that it can loop infinitely and it’s hard to tell if a number is in the set or not without waiting for an infinite amount of time.

Instead, we approximate the Mandelbrot Set by only looping a certain number of times. If, after that number of times, \(|\mathbf{w}| < 2\) is still true, we assume it is in the set.

Another interesting property of the Mandelbrot Set occurs when you take the number of loops it takes for a given point to show that it is not in the set. That is, how many times did the loop have to run before \(|\mathbf{w}| < 2\) was false. If we take that number and plot it as an image where each pixel is turned into a complex number we get something similar to what is shown on almondbread.

For Assignment 1, you will need to:

More details of the assignment will be available on WebCMS3.