←to practical programming

Problems "vector"

Consider an implementation of n-dimensional vectors using the structure

typedef struct {int size; double* data;} vector;
  1. Implement a set of functions to deal with these vectors. The declarations must be kept in the header file vector.h,

    #ifndef HAVE_VECTOR_H /* for multiple includes */
    #define HAVE_VECTOR_H
    
    typedef struct {int size; double* data;} vector;
    
    vector* vector_alloc       (int n);     /* allocates memory for size-n vector */
    void    vector_free        (vector* v);                      /* frees memory */
    void    vector_set         (vector* v, int i, double value); /* v_i ← value */;
    double  vector_get         (vector* v, int i);              /* returns v_i */
    double  vector_dot_product (vector* u, vector* v);   /* returns dot-product */
    
    /* optional */
    void vector_print    (char* s, vector* v);    /* prints s and then vector */
    void vector_set_zero (vector* v);             /* all elements ← 0 */
    int  vector_equal    (vector* a, vector* b); /* 1, if equal, 0 otherwise */
    void vector_add      (vector* a, vector* b); /* a_i ← a_i + b_i */
    void vector_sub      (vector* a, vector* b); /* a_i ← a_i - b_i */
    void vector_scale    (vector* a, double x);   /* a_i ← x*a_i     */
    /* */
    
    #endif
    

    The implementations of the functions must be in the file vector.c. You should #include"vector.h" in vector.c to allow the compiler to check the declarations. Like this,

    #include<stdio.h>
    #include<assert.h>
    #include"vector.h"
    
    vector* vector_alloc(int n){
      vector* v = malloc(sizeof(vector));
      (*v).size = n;
      (*v).data = malloc(n*sizeof(double));
      if( v==NULL ) fprintf(stderr,"error in vector_alloc\n");
      return v;
    }
    
    void vector_free(vector* v){ free(v->data); free(v);} /* v->data is identical to (*v).data */
    void vector_set(vector* v, int i, double value){
    	assert( 0 <= i && i < (*v).size )
    	(*v).data[i]=value;
    }
    double vector_get(vector* v, int i){
    	assert( 0 <= i && i < (*v).size )
    	return (*v).data[i];
    }
    /* ... */
    
  2. Implement a main function that tests your functions, for example,
    #include "vector.h"
    #include "stdio.h"
    #include "stdlib.h"
    #define RND (double)rand()/RAND_MAX
    
    int main()
    {
    	int n = 5;
    
    	printf("\nmain: testing vector_alloc ...\n");
    	vector *v = vector_alloc(n);
    	if (v == NULL) printf("test failed\n");
    	else printf("test passed\n");
    
    	printf("\nmain: testing vector_set and vector_get ...\n");
    	double value = RND;
    	int i = n / 2;
    	vector_set(v, i, value);
    	double vi = vector_get(v, i);
    	if (double_equal(vi, value)) printf("test passed\n");
    	else printf("test failed\n");
    
    	printf("\nmain: testing vector_add ...\n");
    	vector *a = vector_alloc(n);
    	vector *b = vector_alloc(n);
    	vector *c = vector_alloc(n);
    	for (int i = 0; i < n; i++) {
    		double x = RND, y = RND;
    		vector_set(a, i, x);
    		vector_set(b, i, y);
    		vector_set(c, i, x + y);
    	}
    	vector_add(a, b);
    	vector_print("a+b should   = ", c);
    	vector_print("a+b actually = ", a);
    
    	if (vector_equal(c, a)) printf("test passed\n");
    	else printf("test failed\n");
    
    	vector_free(v);
    	vector_free(a);
    	vector_free(b);
    	vector_free(c);
    
    	return 0;
    }