vec_t
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.
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.
vector.c
implementation
file that provides the functionality the above Application
Program Interface (API) specifies.
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);
}
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
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
void vec_sum(vec_t,vec_t,vec_t);
uses the last argument to store the result.
tar.gz
archive of your lab01/ directory, including:
README
file containing
Makefile
.h
headers and .c
source)
make clean
before tar
)
submit
notes.