### Project 1: An Introduction to C (due Friday Feb 12 at midnight)

Let's start writing the code to support a set of mathematical functions. Some of them will be used in future fish-schooling simulation code. We will then write code to practice using input/output functionality in C. I have written these instructions as if you have nothing set up or running on NSCC.- Log onto nscc (the head node of the cluster)
ssh -Y nscc.colby.edu

- Start XEmacs (
`xemacs &`) - Download the tarball of Project 1 code using cURL
`curl -O http://cs.colby.edu/courses/S16/cs336/projects/proj01/proj01.tar` - Expand the proj01 code:
`tar -xf proj01.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, n4, n5, or n6 of nscc (
`ssh n3`) so you are ready to compile and link. Navigate to the`proj01`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 Euclidean distance from (x1,y1) to (x2,y2) float distance(float x1, float y1, float x2, float y2);

- Add a new test function to
`test_my_math.c`that tests your new function. Compile and run`test_my_math`to verify that your new function works for a well-chosen set of inputs. - Write functions that determine whether an int is odd or even. The function prototypes should be
// Return 1 if n is odd. Return 0 if n is not odd. int isOdd( int n ); // return 1 if n is even. Return 0 if n is not even. int isEven( int n );

- Add one or more new test functions to
`test_my_math.c`to test your new functions. To get used to using ints instead of booleans in control statements, write test code that uses if-statements to print whether or not a number is odd/even. - Create a new file
`practice_io.c`. Your goal will be to take in a command line argument that determines how many times you print "Hello, World!" to the terminal.- Include
`stdlib.h`and`stdio.h` - Write a main program that takes in command line arguments:
int main(int argc, char* argv[]) {

- If there aren't enough command line arguments (at least 2 -- one for the filename, one for the first argument), then write a usage statement and return.
- If there are, then use
`atoi`to convert the first argument (`argv[0]`) from a string to an integer. Store it in a variable (e.g.`numHellos` - Print
`Hello, World!`to the terminal`numHellos`times, making sure each greeting is on its own line (i.e. don't forget`\n`). - Test your program by typing
`make practice_io`, fixing any bugs, then running it. .e.g../practice_io 2

should yield this outputHello, World! Hello, World!

- Include
- Create a new set of files
`vector_math.h`and`vector_math.c`. In these files create the data structures and functions to support vector mathematics. Each vector should contain an array of floating point numbers (`float`). It is up to you how to represent each vector (e.g. a struct or two variables containing the size and the data). It is also up to you how you return the answer. Do you require the calling code to allocation the space, and then have your function just fill it in? Or do you have the function allocate the space and then fill it in and return a pointer to it?The operations you need to support are:

- Addition. The operation adds the corresponding entries in two vectors and places them into a third.
- Subtraction. The operation adds the corresponding entries in two vectors and places them into a third.
- Dot product. The operation multiplies the corresponding entries in two vectors, then adds the products. The result is a scalar (i.e. just one float).
- Scalar multiplication. The operator multiplies every entry in the vector by a scalar.

Be sure to write (and turn in)

**test functions**that can be run to demonstrate that your code works.

### Extensions

- Add functions to
`vector_math`to support (and test) additional matrix and/or vector mathematics. Here are common matrix math operations.- Matrix addition
- Dot product: (either two vectors or one matrix and one vector).
- Matrix multiplication: It should perform matrix multiplication (not element-by-element multiplication). It is up to you whether the result is newly allocated by the function or it is simply filled in.

- 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);

- Write a function
`randFloat`that returns a random float value between 0.0 and 1.0. Be sure to demonstrate that it works. In your write-up, be sure to explain your code and the purpose of`srand`.

### Handin

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

Zip up the directory and email it to Stephanie.