CS 336: Project #1

### 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.
1. Log onto nscc (the head node of the cluster)
```ssh -Y nscc.colby.edu
```
2. Start XEmacs (xemacs &)

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

4. Expand the proj01 code: tar -xf proj01.tar
5. 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.
6. Compile and run the test program:
```make test_my_math
./test_my_math
```
7. 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.
8. 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);
```
9. 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.
10. 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 );
```
11. 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.
12. 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.
1. Include stdlib.h and stdio.h
2. Write a main program that takes in command line arguments:
```int main(int argc, char* argv[]) {
```
3. 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.
4. 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
5. Print Hello, World! to the terminal numHellos times, making sure each greeting is on its own line (i.e. don't forget \n).
6. Test your program by typing make practice_io, fixing any bugs, then running it. .e.g.
```./practice_io 2
```
should yield this output
```Hello, World!
Hello, World!
```
13. 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.
• 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.

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.

Zip up the directory and email it to Stephanie.