/*bad_vector_pra*/

#include <stdio.h>
#include <math.h>

typedef struct vec_type
{

   double x;
   double y;
   double z;
} vec_t;

void vec_unit(vec_t *vin, vec_t *vout);
double vec_len(vec_t *v);

int main (void)
{

   vec_t vec1 = {-2.0, 2.0, 1.0};

   vec_unit(&vec1, &vec1);
   printf("vec1.x is %lf, vec1.y is %lf, vec1.z is %lf \n", 
           vec1.x, vec1.y, vec1.z);

   return 0;

}

//this function is broken
void vec_unit(vec_t *vin, vec_t *vout)
{
   vout->x = vin->x / vec_len(vin);
   vout->y = vin->y / vec_len(vin);
   vout->z = vin->z / vec_len(vin);
}

double vec_len(vec_t *v1)
{
   double len;     
   len = sqrt( (v1->x * v1->x) + (v1->y * v1->y) + (v1->z * v1->z));
//   printf("length = %f \n", len);   
   return len;
}
