CS 336: Project #1

### An Introduction to C

#### Fish Schooling Exercises

Let's start writing the code for the fish-schooling example. We begin with the math routines necessary to run the simulations. I have written these instructions as if you have nothing set up or running on NSCC.
1. Read sections 1, 2, 5, and 6 of the paper on fish-schooling simulation.
2. Log onto nscc n1
```ssh -Y nscc.colby.edu
```
3. Start XEmacs (xemacs &)

curl -O http://cs.colby.edu/courses/S11/cs336/projects/proj01/fish_seq.tar

5. Expand the fish_seq code: tar -xf fish_seq.tar
6. Open the files in XEmacs and browse through them. Notice the prototypes in the header file, the various definitions in my_math.c, and the structure of the test functions in test_my_math.c.
7. Compile and run the test program:
```make test_my_math
./test_my_math
```
8. Log onto node n3 of nscc (ssh n3) so you are ready to compile and link. Navigate to the fish_seq directory.
9. Add a function distance to my_math (i.e. put the prototype in the .h file and the definition in the .c file) and an appropriate test function in test_my_math.c. The function prototype should be:
```// return the distance from (x1,y1) to (x2,y2)
float distance(float x1, float y1, float x2, float y2);
```
Don't forget to compile and run before going on to the next step!
10. Add a function max to my_math and an appropriate test function. The function prototype should be:
```// Return the maxium value in the array (of length N)
float max(float *array, int N);
```
Note the helper functions createAscendingArray and createDescendingArray in test_my_math.c. They create arrays and fill them with data. Use these functions to make your test-writing simpler (see test_min for hints).
11. Add a function sum to my_math and an appropriate test function. The function prototype should be:
```// Return the sum of the N floats in the array.
float sum(float *array, int N);
```
12. Add a function mean to my_math and an appropriate test function. The function prototype should be:
```// Return the mean value of N floats in the array.
float mean(float *array, int N);
```
13. Add a function swapArrays to my_math and an appropriate test function. This function is meant to swap to arrays of floats, i.e. to float * values. Recall that we need to be sent the address of the value we are swapping, so the type we need to pass in is float **. The function prototype should be:
```// swap the pointers a and b
void swapArrays(float **a, float **b);
```

#### General Exercises

Let's spend some time making sure we understand what is going on in the above code and figuring out what can go wrong if we aren't careful where we tread. So, let's write additional functions in test_my_math.c that are intended to break the program.
1. Draw the memory before and after two float arrays are swapped. As in class, assume the variables are stored on the stack and the contents are stored on the heap. Assume that before the swap, array a contains 1,2,3 and array b contains 4,5,6.
2. Write a function that "tests" one of your math functions, but which gives the wrong value for the length N. What goes wrong? What can you infer from the memory?
3. Tell me how much time you spent on this project outside of class.

### Extensions

• Add a function findAngle to my_math and an appropriate test function. The function prototype should be:
```// Find the angle between vectors (x1,y1) and (x2,y2)
// Returns a positive number between 0 and pi.
float findAngle(float x1, float y1, float x2, float y2);
```
• Add a function determinant to my_math and an appropriate test function. The function prototype should be:
```// return the determinant of
// | a b |
// | c d |
float determinant(float a, float b, float c, float d);
```

### Handin

To hand in your project, you will gather all of the necessary files into a proj01 directory in your turnin directory:

1. Create a named README.txt. Your project write-up should be in README.txt. For each exercise above, either answer the question or indicate which code file contains the solution.
2. You should hand in all code necessary to run your solutions. Place all necessary .h, .c, and Makefile files in the proj01 directory. Stephanie will probably want to compile and run the code. It should be possible to do so without looking for any more files.