An Introduction to C
Fish Schooling ExercisesLet'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.
- Read sections 1, 2, 5, and 6 of the paper on fish-schooling simulation.
- Log onto nscc n1
ssh -Y nscc.colby.edu
- Start XEmacs (xemacs &)
- Download the tarball of fish-schooling code using cURL
curl -O http://cs.colby.edu/courses/S11/cs336/projects/proj01/fish_seq.tar
- Expand the fish_seq code: tar -xf fish_seq.tar
- 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.
- Compile and run the test program:
make test_my_math ./test_my_math
- Log onto node n3 of nscc (ssh n3) so you are ready to compile and link. Navigate to the fish_seq directory.
- 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!
- 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).
- 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);
- 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);
- 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 ExercisesLet'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.
- 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.
- 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?
- Tell me how much time you spent on this project outside of class.
- 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);
To hand in your project, you will gather all of the necessary files into a proj01 directory in your turnin directory:
- 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.
- 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.
Email Stephanie to let her know your project is ready for grading.