CS 336: Project #1

Project 1: An Introduction to C

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 &)
  3. Download the tarball of Project 1 code using cURL

    curl -O http://cs.colby.edu/courses/S14/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 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!
      

Extensions

Handin

To hand in your project, you will gather all of the necessary files into a proj01 directory in your "turnin" directory (/export/research/srtaylor/cs336/<your_username>):

  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.

Email Stephanie to let her know your project is ready for grading.