### Project 2: Arrays in C

#### Fish Schooling Exercises

This week we will add to the set of mathematical functions we developed last week. Next week, they become part of the fish-schooling code. As additional preparation for fish-schooling, we will also read the journal article about the results of Li et al.- Read sections 1, 2, 5, and 6 of the paper on fish-schooling simulation.
- Log onto the head node of nscc
ssh -Y nscc.colby.edu

- Start XEmacs (
`xemacs &`) - Make a directory called
`proj02`and put copies your project 1 files into it. - Log onto node n3 of nscc (
`ssh n3`) so you are ready to compile and run. Navigate to the`proj02`directory. - Write some support functions for (in
`test_my_math.c`) testing. I suggest you add at least`createAscendingArray`and`createDescendingArray`. They create arrays and fill them with data. Use these functions to make your test-writing simpler (see`test_min`for hints). Here is the code for`createAscendingArray`.float *createAscendingArray(int N) { float *ret = malloc(sizeof(float)*N); int i; for (i=0; i<N; i++) ret[i] = (float)i; return ret; }

- For each of the following functions, put the function prototype in
`my_math.h`, implement the function in`my_math.c`, and add an appropriate test function to`test_my_math.c`.- Add a
`countNans`function:// return the number of Nans in the array. int countNans(float *array, int N);

- Add a
`max`function:// Return the maxium value in the array (of length N) float max(float *array, int N);

- Add a
`sum`function:// Return the sum of the N floats in the array. float sum(float *array, int N);

- Add a
`mean`function:// Return the mean value of N floats in the array. float mean(float *array, int N);

- Add a
`std`function:// Return the standard deviation of N floats in the array. // Note: This uses the Bessel correction. float std(float *array, int N);

- Add a
`swapArrays`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);

- Add a

#### Additional 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.

- 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.

### Extensions

- Write a function that creates a random array of floats within a specified range. The prototype could look like this:
// Create an array of length N // Fill the array with random float between lower_bound // and upper_bound. // Return the array. float *createRandomArray( float lower_bound, float upper_bound, int N );

- Write a function that creates a random array of floats, computes its mean and standard deviation, then writes the results to a text file. For example the contents of the file could be
6 2.024287 2.184500 0.892127 -0.019574 1.014021 2.651947 mean 1.4579 std 0.9975

where the first line specifies the number of elements in the array, the second line speficies the elements in the array, and the third and fourth lines are self-explanatory. - Write a function that creates random arrays in a loop. For each random array, compute the mean and standard and deviation, then write that information to a file. The contents of the file might look like this:
1.4579 0.997 2.2010 0.8588

for a program that creates two random arrays. The first column is the mean of the array and the second column is the standard deviation. - Demonstrate the use of a struct (use a typedef struct) on both the stack and the heap.

### Handin

To hand in your project, you will gather all of the necessary files into a `proj02` 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 proj02 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.