Part 0:  preparation

• Do all of the loop and function exercises.

• Copy the following files from ftp://ftp.cs.princeton.edu/pub/cs126/mandel to an empty directory.

• Here's a high-level view of the organization of your program.
```#include <stdio.h>

/*********************************************************************
*  Computes Mandelbrot iterates (r, s) associated with input (x, y).
*  Returns number of iterations until r^2 + s^2 >= 4 or 255,
*  whichever comes first.
*********************************************************************/
int mandel(double x, double y) {
// do Mandelbrot iterations
}

/*********************************************************************
*  Reads in n, xmin, ymin, width, height from stdin.
*  Outputs turtle graphics image of Mandelbrot set in grayscale.
*  Zooms in on Mandelbrot set and plots an n x n grid of points.
*********************************************************************/
int main(void) {

// for each point (x, y) to be plotted (use nested for loops)
//     - call mandel(x, y) function to calculate # iterations
//     - compute grayscale value
//     - scale point to fit in 512 x 512 box
//     - print turtle graphics command via printf()

return 0;
}
```

•  Part 1:   Mandelbrot function

• First, write the function mandel(). Be sure this is completely debugged before continuing. As a check, the point (0.125, 0.75) is not in the set, since after the 6th iteration, r*r + s*s > 4. Thus, your mand function should return the integer 6.
 # 0 1 2 3 4 5 6 r 0.125 -0.421875 -0.575928 0.45501 -0.303564 -1.95998 3.94524 s 0.75 0.9375 -0.041016 0.797244 1.47551 -0.145822 1.32161

• Be sure to update the variables r and s simultaneously, using the old values of r and s. Otherwise, your plot will look like a "squished" Mandelbrot set.
• Be sure your function is returning the proper number of iterations, and is not committing an "off-by-one" error. This can wreak havoc later on. The iteration count should always be between 0 and 255. Your function should return 0 for (x, y) = (2.0, 2.0) and 255 for (0.0, 0.0). It's important to check the boundary conditions (0 and 255) since this is where errors are most likely to occur.

 Part 2:   Gray-scale image

• Read in the values n, xmin, ymin, width, height from the mand4.txt data file using scanf(). To redirect the input from a file instead of the keyboard, use the following command:
`a.out < mand4.txt`

Print out their values using printf() to make sure you did this properly.

• Before trying to plot the points, first write nested loops to print out the x and y coordinates of the points you will soon be plotting. The mand4.txt file data says to plot a 4-by-4 grid with lower left endpoint (-1.5, -1.0) and upper right endpoint (0.5, 1.0). Using this data file, you should get the following:
```(-1.25,  0.75) (-0.75,  0.75) (-0.25,  0.75) ( 0.25,  0.75)
(-1.25,  0.25) (-0.75,  0.25) (-0.25,  0.25) ( 0.25,  0.25)
(-1.25, -0.25) (-0.75, -0.25) (-0.25, -0.25) ( 0.25, -0.25)
(-1.25, -0.75) (-0.75, -0.75) (-0.25, -0.75) ( 0.25, -0.75)  ```
• Note it doesn't matter in which order you enumerate the points - we chose to plot from upper left to upper right, and then down. Check that your code works properly with other data files as well. If it doesn't, try to see why not. It can be more subtle than you might think.
• Now call the mandel() function and print out the number of iterations for each point. For mand4.txt you should get
```(-1.25,  0.75):   2   (-0.75,  0.75):   3   (-0.25,  0.75):  21   ( 0.25,  0.75):   4
(-1.25,  0.25):   8   (-0.75,  0.25):  12   (-0.25,  0.25): 255   ( 0.25,  0.25): 255
(-1.25, -0.25):   8   (-0.75, -0.25):  12   (-0.25, -0.25): 255   ( 0.25, -0.25): 255
(-1.25, -0.75):   2   (-0.75, -0.75):   3   (-0.25, -0.75):  21   ( 0.25, -0.75):   4 ```
• Next, translate and rescale the x and y coordinates appropriately, so that they are between 0 and 512. Use pencil and paper (and some basic algebra) to determine the correct translation and scaling factors. You should get the following.
```( 64.0, 448.0):   2   (192.0, 448.0):   3   (320.0, 448.0):  21   (448.0, 448.0):   4
( 64.0, 320.0):   8   (192.0, 320.0):  12   (320.0, 320.0): 255   (448.0, 320.0): 255
( 64.0, 192.0):   8   (192.0, 192.0):  12   (320.0, 192.0): 255   (448.0, 192.0): 255
( 64.0,  64.0):   2   (192.0,  64.0):   3   (320.0,  64.0):  21   (448.0,  64.0):   4 ```
Again, check that your code works properly with other data files.
• Now figure out the length of each side of the rectangle to be plotted. It should depend on `n`. Also, convert the iteration count t into a gray-scale value using the formula 1.0 - t/255.0. You are now ready to produce the turtle graphics output.
```C 0.992 0.992 0.992  F  64.000  64.000  S 128.000
C 0.969 0.969 0.969  F  64.000 192.000  S 128.000
C 0.969 0.969 0.969  F  64.000 320.000  S 128.000
C 0.992 0.992 0.992  F  64.000 448.000  S 128.000

C 0.988 0.988 0.988  F 192.000  64.000  S 128.000
C 0.953 0.953 0.953  F 192.000 192.000  S 128.000
C 0.953 0.953 0.953  F 192.000 320.000  S 128.000
C 0.988 0.988 0.988  F 192.000 448.000  S 128.000

C 0.918 0.918 0.918  F 320.000  64.000  S 128.000
C 0.000 0.000 0.000  F 320.000 192.000  S 128.000
C 0.000 0.000 0.000  F 320.000 320.000  S 128.000
C 0.918 0.918 0.918  F 320.000 448.000  S 128.000

C 0.984 0.984 0.984  F 448.000  64.000  S 128.000
C 0.000 0.000 0.000  F 448.000 192.000  S 128.000
C 0.000 0.000 0.000  F 448.000 320.000  S 128.000
C 0.984 0.984 0.984  F 448.000 448.000  S 128.000
```

• Be absolutely sure that your program produces only turtle graphics commands. Do not prompt the user for the five input parameters - just read them in. Also, don't forget to remove any debugging printf() statements.
• To read a value of type double, put the letter l in front of the f: scanf("%lf", &my_double);
To print it, don't use the l, e.g., printf("%f", my_double);
• Because of integer arithmetic 2 / 32 is computed to be 0. To get a floating point approximation, you can use (1.0 * 2) / 32.

 Part 3:  Color image

In this part, you will create a glorious color image of the Mandelbrot set.

• This program is very similar to gray-scale one, so use the file mandgray.c as a starting point. In Unix, you can copy it to mandcolor.c with the command:
`cp mandgray.c mandcolor.c`
• Declare three 256-element arrays of doubles, say red[], green[], and blue[], and read in the color values stored in the data file using scanf(). Remember that C array indices start at 0.

• To test your code, print out the color values in a table.

• If it works, remove this debugging code; otherwise your turtle graphics output won't view properly.
• Once you have done this, it should not take much effort to finish off the color version. Here is the desired output when run on mand4.txt
```C 0.000 0.734 0.000  F  64.000  64.000  S 128.000
C 0.750 0.859 0.750  F  64.000 192.000  S 128.000
C 0.750 0.859 0.750  F  64.000 320.000  S 128.000
C 0.000 0.734 0.000  F  64.000 448.000  S 128.000

C 0.734 0.734 0.000  F 192.000  64.000  S 128.000
C 0.500 0.500 0.000  F 192.000 192.000  S 128.000
C 0.500 0.500 0.000  F 192.000 320.000  S 128.000
C 0.734 0.734 0.000  F 192.000 448.000  S 128.000

C 0.859 0.359 0.234  F 320.000  64.000  S 128.000
C 0.000 0.000 0.000  F 320.000 192.000  S 128.000
C 0.000 0.000 0.000  F 320.000 320.000  S 128.000
C 0.859 0.359 0.234  F 320.000 448.000  S 128.000

C 0.734 0.000 0.000  F 448.000  64.000  S 128.000
C 0.000 0.000 0.000  F 448.000 192.000  S 128.000
C 0.000 0.000 0.000  F 448.000 320.000  S 128.000
C 0.734 0.000 0.000  F 448.000 448.000  S 128.000
```
• If mandcolor.c crashes but mandgray.c does not, be sure that you are not committing an off-by-one error that leads to an array out-of-bounds access.

Kevin Wayne