CS 336: Project #2

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.
  1. Read sections 1, 2, 5, and 6 of the paper on fish-schooling simulation.
  2. Log onto the head node of nscc
    ssh -Y nscc.colby.edu
  3. Start XEmacs (xemacs &)
  4. Make a directory called proj02 and put copies your project 1 files into it.
  5. Log onto node n3 of nscc (ssh n3) so you are ready to compile and run. Navigate to the proj02 directory.
  6. 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;
  7. 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 swapArraysfunction. 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);

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



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