Lab 1: vec_t

Objectives

To construct a 3D vector library by filling in code for the provided vector functions. The vector type (typedef double vec_t[3];) along with the function prototypes should be placed in a header (.h) file while the required functions (methods) be defined in the implementation (.c) file.

Assignment

  1. Using the definition of a 3D vector given in class,
    
    	typedef double vec_t[3];
    	
    write a C vector library that offers the following functions:
    void    vec_sum(vec_t,vec_t,vec_t);
    void    vec_diff(vec_t,vec_t,vec_t);
    double  vec_dot(vec_t,vec_t);
    double  vec_len(vec_t);
    void    vec_scale(double,vec_t,vec_t);
    void    vec_unit(vec_t,vec_t);
    void    vec_copy(vec_t,vec_t);
    void    vec_cross(vec_t,vec_t,vec_t);
    void    vec_project(vec_t,vec_t,vec_t);
    int     vec_read(FILE*,vec_t v1);
    void    vec_print(FILE*,char*,vec_t);
    	
    The above two pieces of code are the entire contents of the vector.h interface file.
  2. Write the corresponding vector.c implementation file that provides the functionality the above Application Program Interface (API) specifies.
  3. You code should be such that the following main.c file works unaltered:
    
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    
    #include "vector.h"
    
    int main()
    {
            vec_t   v1 = {3.0, 4.0, 5.0};
            vec_t   v2 = {4.0, -1.0, 2.0};
            vec_t   v3;
            double  v;
    
      vec_print(stdout, "v1", v1);
      vec_print(stdout, "v2", v2);
    
      vec_diff(v1, v2, v3);
      vec_print(stdout, "v2 - v1 = ", v3);
    
      v = vec_dot(v1, v2);
      printf("v1 dot v2 is %8.3lf \n", v);
    
      v = vec_len(v1);
      printf("Length of v1 is %8.3lf \n", v);
    
      vec_scale(1 / v, v1, v3);
      vec_print(stdout, "v1 scaled by its 1/ length:", v3);
    
      vec_unit(v1, v1);
      vec_print(stdout, "unit vector in v1 direction:", v1);
    
      return(0);
    }
    	
  4. Use the following Makefile to compile the vector library into a libvec.a library that other programs can use (be careful where tabs are required!).
    
    CC      = gcc
      
    INCLUDE = -I.
    
    CFLAGS  = -g
    
    LDFLAGS = \
      -L. \
      -L/usr/lib    
    
    LDLIBS  = \
      -lvec \
      -lc -lm
    
    .c.o:
    	$(CC) -c $(INCLUDE) $(CFLAGS) $<
    
    OBJS =
    
    all: libvec main
      
    libvec: libvec.a
    libvec.a: vector.o
    	ar rcs $@ $?
    	ranlib $@
    
    main: main.c main.o $(OBJS) libvec.a
    	$(CC) $(CFLAGS) $(INCLUDE) -o $@ $@.o $(OBJS) $(LDFLAGS) $(LDLIBS)
    
    clean:
    	rm -f *.o core
    	rm -f *.a
    	rm -f main
    	rm -f *.ps *.pdf
    	
  5. Sample output:
    v1   3.000   4.000   5.000
    v2   4.000  -1.000   2.000
    v2 - v1 =    1.000  -5.000  -3.000
    v1 dot v2 is   18.000 
    Length of v1 is    7.071 
    v1 scaled by its 1/ length:   0.424   0.566   0.707
    unit vector in v1 direction:   0.424   0.566   0.707
    	

Hints

  1. Note that by convention, functions such as
    
    	void    vec_sum(vec_t,vec_t,vec_t);
    	
    uses the last argument to store the result.

Turn in

Turn in all of your code, in one tar.gz archive of your lab01/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Lab description
    4. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    5. Lessons learned, identified interesting features of your program
    6. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .c source)
  4. object code (do a make clean before tar)

How to hand in

See submit notes.